问题 E: Airport Bus
时间限制: 2 Sec 内存限制: 256 MB
提交: 105 解决: 50
[提交] [状态] [讨论版] [命题人:admin]
题目描述
Every day, N passengers arrive at Takahashi Airport. The i-th passenger arrives at time Ti.
Every passenger arrived at Takahashi airport travels to the city by bus. Each bus can accommodate up to C passengers. Naturally, a passenger cannot take a bus that departs earlier than the airplane arrives at the airport. Also, a passenger will get angry if he/she is still unable to take a bus K units of time after the arrival of the airplane. For that reason, it is necessary to arrange buses so that the i-th passenger can take a bus departing at time between Ti and Ti+K (inclusive).
When setting the departure times for buses under this condition, find the minimum required number of buses. Here, the departure time for each bus does not need to be an integer, and there may be multiple buses that depart at the same time.
Constraints
2≤N≤100000
1≤C≤109
1≤K≤109
1≤Ti≤109
C, K and Ti are integers.
输入
The input is given from Standard Input in the following format:
N C K
T1
T2
:
TN
输出
Print the minimum required number of buses.
样例输入
复制样例数据
5 3 5
1
2
3
6
12
样例输出
3
提示
For example, the following three buses are enough:
A bus departing at time 4.5, that carries the passengers arriving at time 2 and 3.
A bus departing at time 6, that carries the passengers arriving at time 1 and 6.
A bus departing at time 12, that carries the passenger arriving at time 12.
思路:就是一个贪心或者说是模拟,要么车满,要么时间到了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int N=100000+5;
int t[N];
int n,k,c;
int main()
{
int p=1,cnt=1,ti;
scanf("%d%d%d",&n,&c,&k);
for(int i=1;i<=n;i++)
scanf("%d",&t[i]);
sort(t+1,t+n+1);
ti=t[1]+k;
for(int i=2;i<=n;i++)
{
if(t[i]>ti||p==c)//时间过了或者车满了
{
cnt++;
ti=t[i]+k;
p=1;
}
else
p++;
}
printf("%d",cnt);
}