题解:P5583 「SWTR-1」Ethan and Sets

· · 题解

Part 0: 准备工作

  1. 所有 d 个数字必须包含。
  2. 其它数字(即不是这 d 个之一的数字,记为 \text{dis})的总个数最小。
  3. 如果有多个区间满足条件 1,2,取“保留区间”的权值和 \sum t 最大的。

输出满足要求的 L 和 R,只需一组即可。

切入点

首先,来看看暴力枚举的思路: :::info[枚举的思路]{open} 先枚举端点 L,R,扫一遍区间,得到区间里有多少喜欢的数和不喜欢的数,并计算权值和 \sum t。 按照题目意思,先按不喜欢的数字个数更新,再按 \sum t 更新。最后输出答案。 ::: 怎么分别求喜欢的、不喜欢的个数呢?对于不喜欢的,因为只需要统计总个数,不用去重,直接累加每一集合的 \text{dis} 次数即可。
而对于喜欢的数,由于要去重统计,显得麻烦一些。在这里提供一种 std::bitset<> 的实现:先用 bitset 存储“这个集合有哪些数”,再算一遍 a|b 就是“在集合 a 或者在集合 b”中的所有数字。(说白了,bitset 的或运算就是集合运算里“并集 \cup”的平替。)
而对“喜欢的数字是否包含在集合的所有数字里”,就是问 \text{like}\subset \text{all} 是否成立。如果它成立,我们就可以得到 \text{all}\cup \text{like}=\text{all},“翻译”成 bitset 的操作,即 (all|like)==all(注意运算优先级!!!)。
上面的实现方法可以沿用,接下来看怎么优化枚举。

找正解

枚举右端点 R,每次向左展左端点 L 时,进行以下操作:

  1. 如果我们已经取到合法的区间了:
    1. 当前的 L 是否可以取?判断标准是:第 L 个集合内是否有不喜欢的元素?
    2. 如果没有,那么直接取,拿走这个集合的权值(不要白不要啊)。
    3. 如果有,那么加上不喜欢的数对答案没有好处,果断退出。
  2. 否则,进行下面的操作:
  3. 取走这个集合,更新“累计结果”(出现的所有数字、当前不喜欢的数字个数,以及权值和)。
  4. 喜欢的数已经取满了:标记“已取到合法区间”(循环退出的出口在第 1 条)。

看似还是 O(n) 的算法,实际上跑不满所有循环。加上 bitset 比较优秀的常数,过这道题还是足够了。

Part 3: 代码

:::success[AC Code]

#include<iostream>
#include<cstdio>
#include<bitset>
#define int long long
const int MAXN=3e3,MAXM=1e3,INF=1e10+987;
//someone tell Vedal there is a problem with my AI.
struct Set{//每一个集合
    int t,num,dis;
    std::bitset<MAXM+5>hav;//包含的数字
};
int n,m,d;
Set st[MAXN+5];
std::bitset<MAXM+5>like,cur;
int dislike,ansl,ansr,mindis=INF,maxmag;
void Init(),PreSolve(),Input(),Solve(),Answer(),AC();
signed main(){
    AC();
    return 0;
}
void Init(){
    std::ios::sync_with_stdio(false),
    std::cin.tie(0),std::cout.tie(0);
    #ifndef ONLINE_JUDGE
    std::freopen("FLIE.in" ,"r",stdin );
    std::freopen("FLIE.out","w",stdout);
    #endif
}
void AC(){
    Init();
    int T=1;
    //std::cin>>T;
    while(T--){
        PreSolve();
        Input();
        Solve();
        Answer();
    }
}
void PreSolve(){}
void Input(){
    std::cin>>n>>m>>d;
    int p;
    for(int i=1;i<=d;i++){
        std::cin>>p;
        like[p]=1;
    }
    for(int i=1;i<=n;i++){
        std::cin>>st[i].t>>st[i].num;
        for(int j=1;j<=st[i].num;j++){
            std::cin>>p;
            st[i].hav[p]=1;
            if(!like[p]) st[i].dis++;//同步更新喜欢&不喜欢
        }
    }
}
void Solve(){
    int l,r;
    for(r=1;r<=n;r++){
        bool flag=0;
        cur^=cur;//异或自己,清空cur
        int dis=0,mag=0,res=0;//dislike,magic,and result(l)
        for(l=r;l;l--){
            if(flag){
                if(st[l].dis) break;
                mag+=st[l].t;
            }else{
                cur|=st[l].hav;
                mag+=st[l].t;
                dis+=st[l].dis;
                if((cur|like)==cur) flag=1;
            }
        }
        if(flag){
            if(dis<mindis)
                ansl=l+1,ansr=r,mindis=dis,maxmag=mag;
            else if(dis==mindis&&mag>maxmag)
                ansl=l+1,ansr=r,maxmag=mag;
        }
    }
}
void Answer(){
    if(!ansl) std::cout<<-1;
    else std::cout<<ansl<<' '<<ansr;
}

:::