2 solutions

  • 3
    @ 2024-1-3 13:41:41

    题意

    输入 nn 个船,从第 ii 艘船到的时间开始,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;
    }
    
  • 3
    @ 2023-12-23 16:47:38
    #include<bits/stdc++.h>
    using namespace std;
    int s,i,n,t,k,r,w[100001],x[300002],y[300002];
    main(){
        cin>>n; while(n--){cin>>t>>k;
            while(k--){
                y[++r]=t;cin>>x[r];
                if(!w[x[r]])s++;w[x[r]]++;
            }
            while(t-y[i]>=86400) if(!--w[x[i++]]) s--;
            cout<<s<<"\n";
        }
    }
    
  • 1

Information

ID
961
Time
1000ms
Memory
125MiB
Difficulty
8
Tags
# Submissions
125
Accepted
18
Uploaded By