https://atcoder.jp/contests/arc073/tasks/arc073_a
Problem Statement
In a public bath, there is a shower which emits water for TT 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 TT seconds. Note that it does not mean that the shower emits water for TT additional seconds.
NN people will push the switch while passing by the shower. The ii-th person will push the switch titi seconds after the first person pushes it.
How long will the shower emit water in total?
Constraints
- 1≤N≤200,0001≤N≤200,000
- 1≤T≤1091≤T≤109
- 0=t1<t2<t3<,...,<tN−1<tN≤1090=t1<t2<t3<,...,<tN−1<tN≤109
- TT and each titi are integers.
Input
Input is given from Standard Input in the following format:
NN TT t1t1 t2t2 ... tNtN
Output
Assume that the shower will emit water for a total of XX seconds. Print XX.
Sample Input 1 Copy
Copy
2 4 0 3
Sample Output 1 Copy
Copy
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 Copy
Copy
2 4 0 5
Sample Output 2 Copy
Copy
8
One second after the shower stops emission of water triggered by the first person, the switch is pushed again.
Sample Input 3 Copy
Copy
4 1000000000 0 1000 1000000 1000000000
Sample Output 3 Copy
Copy
2000000000
Sample Input 4 Copy
Copy
1 1 0
Sample Output 4 Copy
Copy
1
Sample Input 5 Copy
Copy
9 10 0 3 5 7 100 110 200 300 311
Sample Output 5 Copy
Copy
67
题意:
第一行两个数表示n个人洗澡只有一个喷头,每个人只能洗t时间;
第二行n个数表示每个人在第一个人洗澡的第几时间起洗澡,0=t1<t2<t3<,...,<tN−1<tN≤10^9
求喷头一共喷水多长时间。
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[200009];
ll n,t;
int main()
{
cin>>n>>t;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
ll sum=t;
ll e=t;
for(int i=2;i<=n;i++)
{
if(a[i]>e)
{
sum+=t;
e=a[i]+t;
}
else
{
sum+=(a[i]+t-e);
e=a[i]+t;
}
}
cout<<sum<<endl;
}