Codeforces Round #451 (Div. 2) - D. Alarm Clock(贪心)
2017-12-18 22:34
381 查看
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
4000minute 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 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.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
input
output
input
output
input
output
input
output
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.
题意:
每个闹钟响k秒,给你每个闹钟响的时间,如果响的闹钟数>=m时就会醒来。
求不让小明醒来的最少关闹钟的数量。
POINT:
把闹钟的持续时间[l,r]类似线段树的区间更新一样放入树状数组。
比如他在[1,3]响, 那就在1加1,4减1,那么求2时刻只要求query(2)就行。
然后就一个个贪心的关闹钟,首先排个序。
如果当前i-1这个闹钟的l[i-1]时刻不是k-1个闹钟在响,那么当前i这个闹钟一定可以放,就别搜了,直接放好跳过。
不然就搜一下,看能不能放。从l[i]到r[i-1]搜一下就行。
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <vector>
#include <map>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = (2e6+44);
int sum[maxn];
int lowbit(int x)
{
return x&-x;
}
int w,n;
void add(int x,int p)
{
for(int i=x;i<=w;i+=lowbit(i)){
sum[i]+=p;
}
}
int query(int x)
{
int ans=0;
for(int i=x;i>=1;i-=lowbit(i))
{
ans+=sum[i];
}
return ans;
}
int l[maxn];
int main()
{
int m,k;
scanf("%d %d %d",&n,&m,&k);
w=0;
for(int i=1;i<=n;i++){
scanf("%d",&l[i]);
w=max(w,l[i]+m);
}
sort(l+1,l+1+n);
if(k==1){
printf("%d\n",n);
return 0;
}
add(l[1],1);
add(l[1]+m,-1);
int ans=0;
for(int i=2;i<=n;i++){
add(l[i],1);
int flag=1;
if(query(l[i-1])!=k-1){
add(l[i]+m,-1);
continue;
}
for(int j=l[i];j<=l[i-1]+m;j++){
if(query(j)>=k){
flag=0;
ans++;
break;
}
}
if(!flag){
add(l[i],-1);
}
else
add(l[i]+m,-1);
}
printf("%d\n",ans);
}
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
4000minute 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 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.
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.
题意:
每个闹钟响k秒,给你每个闹钟响的时间,如果响的闹钟数>=m时就会醒来。
求不让小明醒来的最少关闹钟的数量。
POINT:
把闹钟的持续时间[l,r]类似线段树的区间更新一样放入树状数组。
比如他在[1,3]响, 那就在1加1,4减1,那么求2时刻只要求query(2)就行。
然后就一个个贪心的关闹钟,首先排个序。
如果当前i-1这个闹钟的l[i-1]时刻不是k-1个闹钟在响,那么当前i这个闹钟一定可以放,就别搜了,直接放好跳过。
不然就搜一下,看能不能放。从l[i]到r[i-1]搜一下就行。
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <vector>
#include <map>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = (2e6+44);
int sum[maxn];
int lowbit(int x)
{
return x&-x;
}
int w,n;
void add(int x,int p)
{
for(int i=x;i<=w;i+=lowbit(i)){
sum[i]+=p;
}
}
int query(int x)
{
int ans=0;
for(int i=x;i>=1;i-=lowbit(i))
{
ans+=sum[i];
}
return ans;
}
int l[maxn];
int main()
{
int m,k;
scanf("%d %d %d",&n,&m,&k);
w=0;
for(int i=1;i<=n;i++){
scanf("%d",&l[i]);
w=max(w,l[i]+m);
}
sort(l+1,l+1+n);
if(k==1){
printf("%d\n",n);
return 0;
}
add(l[1],1);
add(l[1]+m,-1);
int ans=0;
for(int i=2;i<=n;i++){
add(l[i],1);
int flag=1;
if(query(l[i-1])!=k-1){
add(l[i]+m,-1);
continue;
}
for(int j=l[i];j<=l[i-1]+m;j++){
if(query(j)>=k){
flag=0;
ans++;
break;
}
}
if(!flag){
add(l[i],-1);
}
else
add(l[i]+m,-1);
}
printf("%d\n",ans);
}
相关文章推荐
- Codeforces Round #451 (Div. 2)-898A. Rounding 898B.Proper Nutrition 898C.Phone Numbers(大佬容器套容器) 898D.Alarm Clock(超时了,待补坑)(贪心的思想)
- Codeforces Round #451 (Div. 2) D - Alarm Clock 双指针+贪心
- Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque 贪心
- Codeforces Round #340 (Div. 2)-A. Elephant(贪心)
- codeforces #262 DIV2 C题Present(二分+贪心)
- Codeforces Round #436 (Div. 2) D. Make a Permutation! 思维+贪心
- Codeforces Round #402 (Div. 2)C. Dishonest Sellers (贪心)
- Codeforces Round #423 (Div. 2) D High Load 贪心,树
- Codeforces Beta Round #75 (Div. 2) / 92B Binary Number(贪心)
- Codeforces Round #342 (Div. 2) B. War of the Corporations 贪心
- 【贪心】Codeforces Round #177 (Div. 1) C - Polo the Penguin and XOR operation
- Codeforces Round #303 (Div. 2) A - Equidistant String(贪心)
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)-C. Constellation(贪心+向量平行判断)
- Codeforces Round #334 (Div. 2) C. Alternative Thinking(规律+贪心)
- Codeforces Round #258 (Div. 2) . Sort the Array 贪心
- Codeforces Round #180 (Div. 2) D. Fish Weight 贪心
- CodeForcesdiv1:995C - Leaving the Bar(随机算法+贪心)
- Codeforces #274 (Div. 2) C. Exams(贪心:结构体排序)
- Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 贪心、字符串
- Codeforces Round #323 (Div. 2) C GCD Table 582A (贪心)