Codeforces-898D:Alarm Clock(双端队列)
2018-01-14 15:22
543 查看
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 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。
#include<bits/stdc++.h>
using namespace std;
const int MAX=2e6;
const int MOD=1e9+7;
typedef __int64 ll;
int a[MAX];
int main()
{
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+n+1);
deque<int>p;
int ans=0;
for(int i=1;i<=n;i++)
{
p.push_front(a[i]);
while(p.size()>=k&&p.front()-p.back()>=m)p.pop_back();
if(p.front()-p.back()<m&&p.size()>=k)
{
p.pop_front();
ans++;
}
}
cout<<ans<<endl;
return 0;
}
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 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。
#include<bits/stdc++.h>
using namespace std;
const int MAX=2e6;
const int MOD=1e9+7;
typedef __int64 ll;
int a[MAX];
int main()
{
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+n+1);
deque<int>p;
int ans=0;
for(int i=1;i<=n;i++)
{
p.push_front(a[i]);
while(p.size()>=k&&p.front()-p.back()>=m)p.pop_back();
if(p.front()-p.back()<m&&p.size()>=k)
{
p.pop_front();
ans++;
}
}
cout<<ans<<endl;
return 0;
}
相关文章推荐
- codeforces 898 D. Alarm Clock【贪心 + 模拟】
- [置顶] CodeForces 455D Serega and Fun (分块+双端队列)
- SPOJ - ADAQUEUE ,双端队列简单运用!
- BZOJ2330 差分约束之浅谈数学不等式拓扑逻辑顺序转化为图论中队列实现广度优先搜索的最长路之双端队列Bellman-Ford算法及入队次数判断自环
- C++ STL学习笔记三 deque双端队列容器
- 【双端队列】滑动的窗户 window
- codeforces 835B The number on the board (优先队列,贪心)
- HDU 5929(双端队列)
- UVa210_Concurrency Simulator_双端队列的使用
- deque双端队列容器
- 双端队列(deque)--课堂跳了的内容
- 【HDU5929 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 H】【打表找规律 队列模拟】Basic Data Structure 双端栈下连续1和0做nand的结果
- STL - deque(双端队列)
- hdu 5929 Basic Data Structure (双端队列)
- BZOJ 2457: [BeiJing2011]双端队列 贪心
- 【杭电OJ】--1052-田忌赛马(贪心,双端队列)
- 双端队列
- 经典算法题每日演练——第十九题 双端队列
- NOJ1167丑陋数&双端队列
- 双端队列(deque)数组实现