D. Alarm Clock time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputadmin管理员组文章数量:1565292
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.
InputFirst line contains three integers n, m 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.
OutputOutput minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples input3 3 2 3 5 1output
1input
5 10 3 12 8 18 25 1output
0input
7 7 2 7 3 4 1 6 5 2output
6input
2 2 2 1 3output
0Note
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.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int cnt[maxn], a[maxn];
int main(){
int n, m, k;
cin>>n>>m>>k;
for(int i = 1; i <= n; ++i){
scanf("%d", &a[i]);
}
sort(a + 1, a + 1 + n);
int l = 1, r = 1, ans = 0, cur = 1;
memset(cnt, 0, sizeof(cnt));
while(r <= n){
while(a[r] - a[l] + 1 > m){
if(cnt[l] == 0) cur--;
l++;
}
if(cur >= k){
ans++;
cnt[r] = 1;
cur--;
}
r++;
cur++;
}
cout<<ans<<endl;
}
/*
题意:
2e5个数,表示时间,连续的m分钟内不能有超过k个数,问最少要去掉多少个数。
思路:
贪心即可,排序后从小往大扫,当不得不去掉当前数时,就去掉,然后用左边的指针维护一下时间范围,
不超过m分钟。
*/
本文标签: 贪心指针codeforcesDivClock
版权声明:本文标题:Codeforces Round #451 (Div. 2) D. Alarm Clock (贪心+双指针) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1725887016a1047136.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论