蓝桥杯 第九届 日志 统计


标题:日志统计

小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有N行。其中每一行的格式是:

ts id

表示在ts时刻编号id的帖子收到一个"赞"。

现在小明想统计有哪些帖子曾经是"热帖"。如果一个帖子曾在任意一个长度为D的时间段内收到不少于K个赞,小明就认为这个帖子曾是"热帖"。

具体来说,如果存在某个时刻T满足该帖在[T, T+D)这段时间内(注意是左闭右开区间)收到不少于K个赞,该帖就曾是"热帖"。

给定日志,请你帮助小明统计出所有曾是"热帖"的帖子编号。

【输入格式】
第一行包含三个整数N、D和K。
以下N行每行一条日志,包含两个整数ts和id。

对于50%的数据,1 <= K <= N <= 1000
对于100%的数据,1 <= K <= N <= 100000
0 <= ts <= 100000 0 <= id <= 100000

【输出格式】
按从小到大的顺序输出热帖id。每个id一行。

【输入样例】
7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3

【输出样例】
1
3


资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

注意:
main函数需要返回0;
只使用ANSI C/ANSI C++ 标准;
不要调用依赖于编译环境或操作系统的特殊函数。
所有依赖的函数必须明确地在源文件中 #include <xxx>
不能通过工程设置而省略常用头文件。

提交程序时,注意选择所期望的语言类型和编译器类型。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <string>
 7 #include <deque>
 8 #include <vector>
 9 #include <set>
10 using namespace std;
11 #define ll long long 
12 const int N= 5e5+1000;
13 ll n,d,k;
14 ll ts,id;
15 vector<ll>ve[N];
16 set<ll>s;
17 set<ll>::iterator it;
18 bool check(ll id)
19 {
20     ll l = ve[id].size();
21     if(l<k)  return false;
22     sort(ve[id].begin(),ve[id].end());//按时间顺序 
23     ll st=0,en=0,cnt=0;
24     while(st<=en&&en<l){
25         cnt++;
26         if(cnt>=k){
27             if(ve[id][en]-ve[id][st]<d) return true;//注意题目明确了[) 
28             else{
29                 cnt--;
30                 st++;
31             }
32         }
33         en++;
34     }
35     return false;
36 }
37 int main()
38 {
39     scanf("%lld%lld%lld",&n,&d,&k);
40     for(ll i=0;i<n;i++) {
41         scanf("%lld%lld",&ts,&id);
42         s.insert(id);
43         ve[id].push_back(ts);
44     }
45     for(it=s.begin();it!=s.end();it++){
46         ll id = *it;
47         if(check(id)){
48             printf("%lld
",id);
49         }
50     }
51     return 0;
52 }
原文地址:https://www.cnblogs.com/tingtin/p/10572670.html