AcWing467. 海港

 输入样例:

3 
1 4 4 1 2 2 
2 2 2 3 
10 1 3

输出样例:

3
4
4

解析: 

        滑动窗口,用队列维护每个乘客的时间和国籍,当新加入一个时间的乘客时,队头弹出24小时前的所有乘客,并且cnt数组记录当前统计的乘客国籍。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,k,x;
int cnt[N],res;
queue<pair<int,int>>q;
int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
    	scanf("%d%d",&m,&k);
    	while(q.size()&&q.front().first<=m-86400){	//弹出24小时前的乘客 
    		int x=q.front().second;
			q.pop();
			if(cnt[x]==1) res--;
			cnt[x]--;	
		}
    	for(int j=0;j<k;j++){						//加上当前的乘客 
    		scanf("%d",&x);
    		if(cnt[x]==0) res++;					//统计国籍 
    		cnt[x]++;
    		q.push({m,x});
		}
		cout<<res<<endl;
	}
    return 0; 
} 

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇

)">
下一篇>>