[Codeforces958F2]Lightsabers (medium)(思维)

Description

题目链接

Solution

设一个l指针指向当前数列左边,从左往右扫描一遍,将当前颜色记录,

当所有颜色都得到后,进行判断,如果当前l指向的颜色大于需要的颜色,l后移一位,然后更新答案

Code

#include <cstdio>
#include <set>
#define N 200010
using namespace std;

set<int> q;
int n,m,col[N],cnt[N],k[N],Ans=1e9,sum,l=1,r;

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int main(){
	freopen("in.txt","r",stdin);
    n=read(),m=read();
    for(int i=1;i<=n;col[i++]=read());
    for(int i=1;i<=m;++i){
    	if(k[i]=read()) q.insert(i);
    	sum+=k[i];
	}
	for(r=1;r<=n;++r){
		if(++cnt[col[r]]==k[col[r]]) q.erase(col[r]);
		if(q.empty()){
			while(l<=r&&cnt[col[l]]>k[col[l]]) cnt[col[l++]]--;
			Ans=min(Ans,r-l+1-sum);
		}
	}
	if(Ans==1e9) Ans=-1;
	printf("%d\n",Ans);
    return 0;
}

 

posted @ 2018-04-16 20:05  void_f  阅读(378)  评论(0编辑  收藏  举报