2020暑期牛客多校第二场G.Greater and Greater(bitset+思维构造)

题目链接:https://ac.nowcoder.com/acm/contest/5667/G
解题思路:
bitset用法 :Bitset
大佬博客解析
代码里面也有一些说明,可以参考

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<bitset>
#include<vector>
using namespace std;
const int maxn=150010;
const int maxm=40010;
#define  P pair<int,int> 
int n,m;
bitset<maxn> ans,res;
vector<P>  a,b;
int main(){
	scanf("%d%d",&n,&m);
	int tmp;
	for(int i=1;i<=n;i++){
		scanf("%d",&tmp);
		a.push_back(P(tmp,i));
	}
	for(int i=1;i<=m;i++){
		scanf("%d",&tmp);
		b.push_back(P(tmp,i));
	}
	sort(a.begin(),a.end(),greater<P>());    //按first从小到大排序
	sort(b.begin(),b.end(),greater<P>());
	ans.set();     //将ans所有位置为1
	int p=0;
	//因为a和b两个数组都是按从大到小顺序排序
	//所以p比前面的大,自然也比后面的大,不需要更新直接保留就行
	for(int i=0;i<m;i++){
		while(p<n&&a[p].first>=b[i].first)
			res.set(a[p++].second);        //把第a[p++].second位置为1
		ans&= (res>>(b[i].second-1));      //因为要措开,所以根据原来的位置i前移i-1,更新答案
	}
	printf("%d\n",ans.count());            //计算ans中1的位数,因为如果这一位数为1,说明存在一种情况使得a子串各个位大于等于b子串
	return 0;
}

猜你喜欢

转载自blog.csdn.net/littlegoldgold/article/details/107492590