题目A--Competition - 20181010

题目

/*
Vasya has recently got a job as a cashier at a local store.His day at work is L minutes long.Vasya has already memorized n regular customers,the i-th of which comes after ti minutes after the beginning of the day,and his service consumes li minutes.It is guaranteed that no customer will arrive while Vasya is servicing another customer.

Vasya is a bit lazy,so he likes taking smoke breaks for a minutes each.Those breaks may go one after another,but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss.What is the maximum number of breaks Vasya can take during the day?

Input
The first line contains three integers n, L and a (0≤n≤105, 1≤L≤109, 1≤a≤L).

The i-th of the next n lines contains two integers ti and li (0≤ti≤L−1, 1≤li≤L).
It is guaranteed that ti+li≤ti+1 and tn+ln≤L.

Output
Output one integer  — the maximum number of breaks.

Examples

Input
2 11 3
0 1
1 1
Output
3

Input
0 5 2
Output
2

Input
1 3 2
1 2
Output
0

Note
In the first sample Vasya can take 3 breaks starting after 2, 5 and 8 minutes after the beginning of the day.

In the second sample Vasya can take 2 breaks starting after 0 and 2 minutes after the beginning of the day.

扫描二维码关注公众号,回复: 3739064 查看本文章

In the third sample Vasya can't take any breaks.
*/

题意:判断时间间隔大小

思路:一个时间轴,给定n个起始时间以及所需时间,求时间轴上的间隙能满足多少次休息需求。

       PS:(1)特判n=0,n=1,时的计算状态;

             (2)注意在第一段时间之前的间隙,以及最后一段时间之后的间隙;

AC代码:

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;

int main(){
    long long n,L,a;
    //vector<long long> com;
    //vector<long long> ser;
    long long num=0;

    scanf("%lld%lld%lld",&n,&L,&a);
    long long com1,ser1,com2,ser2;

    //特殊情况n=0
    if(n==0){
        num+=L/a;
        printf("%lld",num);
        return 0;
    }

    scanf("%lld%lld",&com1,&ser1);

    //开始时有时间吸烟
    if(com1>=a){
        num+=com1/a;
    }

    //特殊情况n=1;
    if(n==1){
        num+=(L-(com1+ser1))/a;
        printf("%lld",num);
        return 0;
    }

    for(int i=1;i<n;i++){
        scanf("%lld%lld",&com2,&ser2);
        //两者(上次服务完成后与下一顾客到达前)之间有时间吸烟
        if((com2-(com1+ser1))>=a){
            num+=(com2-(com1+ser1))/a;
        }
        //更新“前,后”数据;
        com1=com2;
        ser1=ser2;
    }
    if((L-(com2+ser2))>=a){
        num+=(L-(com2+ser2))/a;
    }
    //printf("????????");
    printf("%lld",num);
    return 0;

}

(虽然是水题,但是自己读自己想自己敲的感觉好好呀~)

猜你喜欢

转载自blog.csdn.net/sodacoco/article/details/83005116