AcWing 1238. 日志统计

原题链接

考察:双指针

思路:

        很明显的双指针裸题,当i往前一步的时候,j要保持在与i在D的距离外.

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <set>
 5 using namespace std;
 6 const int N = 100010;
 7 typedef pair<int,int> PII;
 8 PII p[N];
 9 int score[N];
10 set<int> s;
11 int main()
12 {
13     int n,d,k;
14     scanf("%d%d%d",&n,&d,&k);
15     for(int i=1;i<=n;i++) scanf("%d%d",&p[i].first,&p[i].second);
16     sort(p+1,p+n+1);
17     for(int i=1,j=1;i<=n;i++)
18     {
19         int id = p[i].second;
20         score[id]++;
21         while(j<=n&&p[i].first-p[j].first>=d)
22         {
23             int t = p[j].second;
24             score[t]--;
25             j++;
26         }
27         if(score[id]>=k) s.insert(id);
28     }
29     for(auto it:s) printf("%d\n",it); 
30     return 0;
31 }
参考y总代码写的

我自己的思路两个while...for循环被我完全无视

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <set>
 5 using namespace std;
 6 const int N = 100010;
 7 typedef pair<int,int> PII;
 8 PII p[N];
 9 int score[N];
10 set<int> s;
11 int main()
12 {
13 //    freopen("in.txt","r",stdin); 
14     int n,d,k;
15     scanf("%d%d%d",&n,&d,&k);
16     for(int i=1;i<=n;i++) scanf("%d%d",&p[i].first,&p[i].second);
17     sort(p+1,p+n+1);
18     for(int i=1,j=1;i<=n;)
19     {
20         while(j<=n&&p[j].first-p[i].first<d)
21         {
22             int id = p[j].second;
23             score[id]++;
24             j++;
25             if(score[id]>=k) s.insert(id);
26         }
27         while(i<=n&&p[j].first-p[i].first>=d)
28         {
29             int id = p[i].second;
30             score[id]--;
31             i++;
32         }
33         if(j>n&&p[j-1].first-p[i].first<d) break;
34     }
35     for(auto it:s) printf("%d\n",it); 
36     return 0;
37 }
繁琐的代码

 

posted @ 2021-02-25 15:51  acmloser  阅读(67)  评论(0编辑  收藏  举报