洛谷 P2058 海港 题解

Analysis

这道题是我们今天考试的一道题,考试时只想到了暴力70分的做法,实际上正解是用队列来维护国籍的种类就行了。

 1 #include<cstdio>
 2 #include<queue>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 inline int read() 
 8 {
 9     int x=0;
10     bool f=1;
11     char c=getchar();
12     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
13     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
14     if(f) return x;
15     return 0-x;
16 }
17 inline void write(int x)
18 {
19     if(x<0){putchar('-');x=-x;}
20     if(x>9)write(x/10);
21     putchar(x%10+'0');
22 }
23 int n,t,m,x;
24 int temp_nation[1000005];
25 int ans;
26 struct node
27 {
28     int s,t;
29 };
30 queue<node> q;
31 node h;
32 int main()
33 {
34     n=read(); 
35     for(int i=1;i<=n;i++)
36     {
37         t=read();m=read();
38         while(!q.empty())
39         {
40             h=q.front();
41             if(h.t+86400<=t)
42             {
43                 temp_nation[h.s]--;
44                 if(temp_nation[h.s]==0) ans--;
45                 q.pop();
46                 continue;
47             }
48             break;
49         }
50         for(int j=1;j<=m;j++)
51         {
52             x=read();
53             h.s=x;
54             h.t=t;
55             q.push(h);
56             temp_nation[x]++;
57             if(temp_nation[x]==1) ans++;
58         }
59         write(ans);
60         printf("\n");
61     }
62     return 0;
63 }
请各位大佬斧正(反正我不认识斧正是什么意思)
posted @ 2019-07-29 14:04  handsome_zyc  阅读(447)  评论(0编辑  收藏  举报