跳到正文

题解

CF367D Sereja and Sets

ziyistudy约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
获赞
4
题目
CF367D

版本

  1. v1当前

前言

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

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

正言

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

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

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

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

C++
#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;
}
原文