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

本文介绍了一种解决特定闹钟问题的方法,通过排序和滑动窗口技术确定为满足条件需关闭的最少闹钟数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Alarm Clock
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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·1051 ≤ 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.

题目大意:有n个会在ai时间响的闹铃,现在要关闭最少个数闹铃,使其在m内不多于k个闹铃会响,最少关几个

解题思路:对闹铃进行排序,在m范围内看看是否有多于k个闹铃会响

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;

int n,m,k;
int a[200005];
int check[1000005];
int i,cnt;
int main()
{
	cin>>n>>m>>k;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+1+n);
	queue<int> qua;
	for(i=1;i<=n;i++)
	{
		while(!qua.empty()&&a[i]-qua.front()>=m) {
			qua.pop();
		}
		if(qua.size()>=k-1)
		{
			cnt++;
		}
		else
			qua.push(a[i]);
	}
	cout<<cnt<<endl;
 return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值