Codeforces Round #451 (Div. 2) D. Alarm Clock

Posted starry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #451 (Div. 2) D. Alarm Clock相关的知识,希望对你有一定的参考价值。

Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.

Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn‘t consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.

Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.

Input

First line contains three integers nm and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya‘s waking up.

Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.

Output

Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.

Examples
input
3 3 2
3 5 1
output
1
input
5 10 3
12 8 18 25 1
output
0
input
7 7 2
7 3 4 1 6 5 2
output
6
input
2 2 2
1 3
output
0
Note

In first example Vitalya should turn off first alarm clock which rings at minute 3.

In second example Vitalya shouldn‘t turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.

In third example Vitalya should turn off any 6 alarm clocks.

 

贪心+模拟题,WA了好多次55555

贪心的话每次删除最后的一个 使的任意 [l,r] > m,当小于m时就删除第l个元素,cnt代表的是[l,r]之间有多少个数,vis 1代表已经删除了。

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1e6+10;
 4 int vis[N], a[N];
 5 int main() {
 6     int n, m, k;
 7     cin >> n >> m >> k;
 8     for(int i = 0; i < n; i ++) cin >> a[i];
 9     sort(a,a+n);
10     int l = 0, r = 0, ans = 0, cnt = 1;
11     while(r < n) {
12         while(a[r]-a[l]+1 > m) {
13             if(vis[l] == 0) cnt--;
14             l++;
15         }
16         if(cnt >= k) {
17             ans ++;
18             vis[r] = 1;
19             cnt--;
20         } 
21         r++;
22         cnt++;
23     }
24     cout << ans << endl;
25     return 0;
26 }

 

n, m, k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
vis = []
for i in range(n):
    vis.append(0)
ans = l = r = 0
cnt = 1
while(r < n):
    while(a[r]-a[l]+1 > m):
        if(vis[l] == 0):
            cnt -= 1
        l += 1
    if cnt >= k:
        ans += 1
        vis[r] = 1;
        cnt -= 1
    r += 1
    cnt += 1
print(ans)

 

以上是关于Codeforces Round #451 (Div. 2) D. Alarm Clock的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #451 (Div. 2) ABC

Codeforces Round #451 (Div. 2) D. Alarm Clock

Codeforces Round #451 (Div. 2) DAlarm Clock

Codeforces Round #451 Div. 2 C D E

Codeforces Round #451 (Div. 2) F Restoring the Expression

Codeforces Round #451 (Div. 2)F. Restoring the Expression 字符串hash