• LQB2018b08 日志统计


    小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有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一行。






    一开始觉得这个题不算太难,因我我以为时间都是顺序的,,,顺序的花有的数甚至不用存

    在网上学得方法....取尺法
    (https://blog.csdn.net/nka_kun/article/details/79780858)谢大佬!!!



    本来想用map计数,结果后来想想,,,用个vector取个大小就可啊!!!!!!(呜呜呜呜呜呜呜呜呜呜呜)
    中间的judge堪称绝妙,,,,双指针遍历,,,,,(我想我有时间的话会搞双指针的专题的)
     1 #include <bits/stdc++.h>
     2 #define mem(a, b) memset(a, b, sizeof(a))
     3 #define mod 1000000007
     4 using namespace std;
     5 typedef long long ll;
     6 const int maxn = 1e5 + 5;
     7 const double esp = 1e-7;
     8 const int ff = 0x3f3f3f3f;
     9 map<int, int>::iterator it;
    10 
    11 int n, d, k;
    12 vector<int> t[maxn];
    13 int ans[maxn];
    14 
    15 bool judge(int x)
    16 {
    17     int len = t[x].size();
    18     if (len < k)
    19         return false;
    20 
    21     sort(t[x].begin(), t[x].end());
    22 
    23     int l = 0, r = 0;
    24     while (r < len)
    25     {
    26         if (r - l + 1 >= k) // r-l+1为点赞数
    27         {
    28             if (t[x][r] - t[x][l] < d) //注意是小于
    29                 return true;
    30             else
    31                 l++;
    32         }
    33         r++;
    34     }
    35 
    36     return false;
    37 }
    38 
    39 int main()
    40 {
    41     cin >> n >> d >> k;
    42 
    43     int maxId = 0;
    44     for (int i = 1; i <= n; i++)
    45     {
    46         int ts, id;
    47         scanf("%d %d", &ts, &id);
    48         t[id].push_back(ts);
    49         maxId = max(maxId, id);
    50     }
    51 
    52     int cnt = 0;
    53     for (int i = 0; i < maxn; i++)
    54         if (judge(i))
    55             ans[++cnt] = i;
    56 
    57     for (int i = 1; i <= cnt; i++)
    58         printf("%d
    ", ans[i]);
    59 
    60     return 0;
    61 }
    
    
    
     
  • 相关阅读:
    redhat 关机注销命令详解
    CentOS网络配置详解
    Red Hat 6网络配置笔记
    H3C三层交换机S5500初始配置+网络访问策略
    python 发邮件 ,转载:https://mp.weixin.qq.com/s/kmNZ04MlDve4AmCCOoT2HA
    解决不能右键查看元素的问题, 转载:https://mp.weixin.qq.com/s/V_fpPN62Kdf0bz6zgFpVCg
    这几点鲜有人知的爬虫技巧,让你爽歪歪 转载:https://mp.weixin.qq.com/s/52luElhn4nRBZCdQMGEhnw
    一个反爬 JS 逆向分析的例子 转载:https://mp.weixin.qq.com/s/2luhB-AhMIzxVh6rPERzCA
    ssh 端口转发 转载:https://mp.weixin.qq.com/s/uesOCt9gmdST-HpwYTKsIw
    爬虫视频
  • 原文地址:https://www.cnblogs.com/zhmlzhml/p/13752089.html
Copyright © 2020-2023  润新知