日志统计--蓝桥杯--vector

/*
标题:日志统计

小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有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 */
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
using namespace std;
int n,d,k;
vector<int>p;//存id
vector<int>pp[10005];//相当于结构体struct node
                                //{
                                //   int t;
                                //}pp[10005];
int main()                      
{
    cin>>n>>d>>k;
    for(int i=0;i<n;i++)
    {
        int id,t;
        cin>>t>>id;
        p.push_back(id);
        pp[id].push_back(t);
    }
    sort(p.begin(),p.end());
    p.erase(unique(p.begin(),p.end()),p.end());//去重,删去重复元素
    for(int i=0;i<p.size();i++)//对每一个id进行分析判断
    {
        int temp=p[i],zan=0,star,end1;
        sort(pp[temp].begin(),pp[temp].end());
        star=0,end1=pp[temp].size()-1;
        zan=pp[temp].size();
        while(star<end1)
        {
            if(pp[temp][end1]-pp[temp][star]<d&&zan>=k)
            {
              cout<<temp<<endl;
              break;
            }
            else//还有一种情况,就是起点到终点的时间大于d,但是中间有符合条件的区间,所以还要逐渐缩小区间判断
            {
              if(pp[temp][star+1]-pp[temp][star]>pp[temp][end1]-pp[temp][end1-1])
                star++;
              else
                end1--;
            }   
        }
    }
}
 

猜你喜欢

转载自www.cnblogs.com/-citywall123/p/10494937.html