1238. 日志统计

题目链接

1238. 日志统计

小明维护着一个程序员论坛。现在他收集了一份”点赞”日志,日志共有 N 行。

其中每一行的格式是:

ts id

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

现在小明想统计有哪些帖子曾经是”热帖”。

如果一个帖子曾在任意一个长度为 D 的时间段内收到不少于 K 个赞,小明就认为这个帖子曾是”热帖”。

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

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

输入格式

第一行包含三个整数 N,D,K

以下 N 行每行一条日志,包含两个整数 tsid

输出格式

按从小到大的顺序输出热帖 id

每个 id 占一行。

数据范围

1KN105,
0ts,id105,
1D10000

输入样例:

7 10 2 0 1 0 10 10 10 10 1 9 1 100 3 100 3

输出样例:

1 3

解题思路

哈希

可以将 id 作为键,ts 构成的数组作为值构建一个哈希表,遍历所有 id 里的时刻,再二分结束时刻,判断里面有赞的个数是否不少于 k

  • 时间复杂度:O(nlogn)

滑动窗口,双指针

先将 tsidts 作为第一关键字排序,维护一个长度为 d 的窗口,右指针每次移动时判断当前 id 是否满足题意

  • 时间复杂度:O(nlogn)

代码

  • 哈希
// Problem: 日志统计 // Contest: AcWing // URL: https://www.acwing.com/problem/content/1240/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=1e5+5; int n,d,k,t,id; unordered_map<int,vector<int>> mp; bool res[N]; int main() { cin>>n>>d>>k; for(int i=0;i<n;i++) { cin>>t>>id; mp[id].pb(t); } for(auto [x,y]:mp) { sort(y.begin(),y.end()); int m=y.size(); for(int i=0;i<m;i++) { int pos=lower_bound(y.begin(),y.end(),y[i]+d)-y.begin(); if(pos-1-i+1>=k) { res[x]=true; break; } } } for(int i=0;i<N;i++) if(res[i])cout<<i<<'\n'; return 0; }
  • 滑动窗口
// Problem: 日志统计 // Contest: AcWing // URL: https://www.acwing.com/problem/content/description/1240/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=1e5+5; int n,d,k,t,id,res[N],cnt[N]; PII a[N]; int main() { cin>>n>>d>>k; for(int i=1;i<=n;i++) cin>>a[i].fi>>a[i].se; sort(a+1,a+1+n); for(int i=1,j=1;i<=n;i++) { t=a[i].fi,id=a[i].se; cnt[id]++; while(t-a[j].fi>=d)cnt[a[j].se]--,j++; if(cnt[id]>=k)res[id]=true; } for(int i=0;i<N;i++) if(res[i])cout<<i<<'\n'; return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/15889729.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(72)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示