CF367D Sereja and Sets

· · 题解

前言

模拟赛看到这题,想了超久。

首先 m\leq 20 肯定是状压,可是第一眼看到的是单调队列或者什么,但都和状压联系不上,于是:

正言

我们考虑集合 ans 为最后选择的/集合的编号/的集合(手动断句)。因为每 d 个数都至少有一个被选择,则对于集合 V=\left \{a_i,...,a_{i+d-1} \right \},必然有 V\bigcap ans \ne \emptyset 。否则相当于这连续 d 个位置没有被选择,与题目矛盾。

显然相交根本不好做,不如考虑不相交。即若: V\bigcap ans = \emptyset ,则这个 ans 是不合法的,需要排除。进一步地,应有 ans \subseteq \complement V(可以证明 ans 大小不会超过 \complement V 的大小)。

这样,我们可以对于每个集合 V,标记其 \complement V,当然因为 ans 也可以是其子集,所以不妨用高维后缀和往前退一下即可。

由于 m\leq 20,我们状压 V。最后我们统计合法的 ans 的 1 的个数的最小值即可(代表选了多少个不同集合)。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll MOD=998244353;
// head
const int N=1e5+5,M=22;
int col[N];
int sum[M];
bool flag[1<<M];
signed main() 
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n,m,d;cin>>n>>m>>d;
    for(int i=0;i<m;i++){
        int s;cin>>s;
        for(int j=0;j<s;j++) {int tmp;cin>>tmp;--tmp;col[tmp]=i;}
    }
    for(int i=0;i<d-1;i++) sum[col[i]]++;
    for(int i=d-1;i<n;i++){
        sum[col[i]]++;
        int now=0;
        for(int j=0;j<m;j++){
            if(!sum[j]) now|=(1<<j);
        }
        flag[now]=1;
        sum[col[i-d+1]]--;
    }
    int ans=1e9;
    for(int i=(1<<m)-1;i>=0;i--) {
        if(flag[i]) {
            for(int j=0;j<m;j++) if(i&(1<<j)) flag[i^(1<<j)]=1;
        }
    }
    for(int i=(1<<m)-1;i>=0;i--){
        if(!flag[i]){
            ans=min(ans,(int)__builtin_popcount(i));
        }
    }
    cout<<ans<<endl;
}