C - Sentou(思维)

 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值