Problem Description
In a public bath, there is a shower which emits water for T seconds when the switch is pushed.
If the switch is pushed when the shower is already emitting water, from that moment it will be emitting water for T seconds. Note that it does not mean that the shower emits water for T additional seconds.
N people will push the switch while passing by the shower. The i-th person will push the switch ti seconds after the first person pushes it.
How long will the shower emit water in total?
Constraints
- 1≤N≤200,000
- 1≤T≤109
- 0=t1<t2<t3<,…,<tN−1<tN≤109
- T and each ti are integers.
Input
Input is given from Standard Input in the following format:
N T
t1 t2 ... tNOutput
Assume that the shower will emit water for a total of X seconds. Print X.
Example
Sample Input 1
2 4
0 3Sample Output 1
7
Three seconds after the first person pushes the water, the switch is pushed again and the shower emits water for four more seconds, for a total of seven seconds.Sample Input 2
2 4
0 5Sample Output 2
8
One second after the shower stops emission of water triggered by the first person, the switch is pushed again.Sample Input 3
4 1000000000
0 1000 1000000 1000000000Sample Output 3
2000000000
Sample Input 4
1 1
0Sample Output 4
1
Sample Input 5
9 10
0 3 5 7 100 110 200 300 311Sample Output 5
67
题意:有一个开关,当按下开关后的 T 秒内会一直放水,当在放水状态时,如果有人再次按下开关,那么停止放水,并从按下的那一刻起的 T 秒会再次一直放水,给出 n 个人按压开关的时间,问总共流出多少水
思路:简单模拟
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int main(){
int n,t;
scanf("%d%d",&n,&t);
int res=0;
int remain=0;
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
res+=(x+t);
res-=max(x,remain);
remain=x+t;
}
printf("%d\n",res);
return 0;
}