2 solutions
-
3
题意
输入 个船,从第 艘船到的时间开始,24 小时(86400秒)内有多少个不同国籍的人到了海港
PS1:第 1 秒到的船,不计第 86401 秒到的船
PS2:到达时间的顺序是升序的
思路
结构体:存这个人的国籍和到达时间
队列:存人
如果队列里有人,就检查这些人有没有超时(24 小时外),有就淘汰掉(处理答案)
输入人,存好国籍和到达时间,推进队列,国籍人数加 1。如果这个人是 24 小时内第一个到海港的人,答案加 1
PS:答案要继承下去
代码
#include <bits/stdc++.h> using namespace std; struct people{ int t,g; // t 是时间,g 是国籍 }h; queue<people> p; // p 存人 int g[100005]; // g 是国籍桶 int cnt; int main(int argc, char **argv){ int n; cin >> n; while (n--){ int t,m; cin >> t >> m; while(!p.empty()){ h = p.front(); if (h.t + 86400 <= t){ // 如果这个人在 24 小时外 g[h.g]--; // 这个国籍的人数减 1 if (!g[h.g]) cnt--; // 这个国籍没人了,答案减 1 p.pop(); // 把这个人扔掉 continue; // 避免 break } break; // 如果在 24 小时内,后面的人就不用看了 } for (int i = 0;i < m;i++){ int x; // 国籍 cin >> x; h.g = x; h.t = t; p.push(h); // 把这个人扔进队列 g[x]++; // 这个国籍的人加 1 if (g[x] == 1) cnt++; // 如果这个人是 24 小时内这个国籍第一个出现的,就把答案加 1 } printf("%d\n",cnt); // 注意答案不能清零,继承下去 } return 0; }
- 1
Information
- ID
- 961
- Time
- 1000ms
- Memory
- 125MiB
- Difficulty
- 8
- Tags
- # Submissions
- 125
- Accepted
- 18
- Uploaded By