【資料結構1-1】線性表 [NOIP2016 普及組] 海港
阿新 • • 發佈:2021-11-08
建立佇列從而達到延緩處理的效果。
題解
因為要統計近24小時之間的資料,我們沒有辦法對當前的資料操作從而達到統計未來24h之內的國籍,但是我們可以通過佇列暫時儲存該資料,等到24h後再處理當前的資料。也即,佇列中一直儲存近24h的資料,我們用這個佇列“篩子”去從前往後篩不同的24h從而得到每24h的結果。
AC程式碼
#include<bits/stdc++.h> using namespace std; int flag[100010]={0}; struct Node{ Node(int a,int b):t(a),x(b){}; Node(){}; int t,x; }now; queue<Node> q; queue<int> ans; int main(){ ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n,k,t,x; int cnt=0; cin>>n; for(int i=0;i<n;i++){ cin>>t>>k; while(!q.empty() && q.front().t+86400<=t){ now=q.front(); flag[now.x]--; if(flag[now.x]==0) cnt--; q.pop(); } for(int j=0;j<k;j++){ cin>>x; flag[x]++; q.push(Node(t,x)); if(flag[x]==1){ cnt++; } } ans.push(cnt); } while(!ans.empty()){ cout<<ans.front()<<endl; ans.pop(); } return 0; }