1. 程式人生 > >Codeforces Round #514 (Div. 2) A. Cashier (模擬)

Codeforces Round #514 (Div. 2) A. Cashier (模擬)

題意:一個收銀員每天上L個小時的班,為了緩解壓力當他閒的時候,他每過a分鐘需要想要抽一次煙,但是在顧客來的時候不能抽菸,他知道顧客啥時候來,並且來幾分鐘,問他能抽多少次煙。

題解:一開始沒什麼思路,打算用桶排序之類的瞎搞,但是後來發現思路錯了(題意也理解錯了)。

正確的思路是,把每兩個客人中間的間隔時間用來抽菸,即 (time[ i ]. t - ( time [ i - 1 ]. t + time [ i - 1 ].l )) / a

然後 特殊處理一下 兩個邊界情況即可。

程式碼如下:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
struct node{
	long long l,t;
}a[maxn];

bool cmp(node a,node b){
	return a.t < b.t; 
}
int main(){
	int n,L,len;
	scanf("%d%d%d",&n,&L,&len);
	for(int i = 1 ; i <= n ; i ++)
		scanf("%d%d",&a[i].t,&a[i].l);
	a[++ n].t = L;
	sort(a + 1,a + 1 + n,cmp); 
	long long ans = 0;
	if(n == 1){
		ans += L/len;
	}
	else {
		for(int i = 2; i <= n ; i ++){
			ans +=(a[i].t-(a[i-1].t+a[i-1].l))/len;
		}
		ans += a[1].t/len;
	}
	printf("%d\n",ans);
	return 0;
}