codeforces 978E - Bus Video System(数学思想去做)

该博客讨论了如何使用数学思想解决codeforces上的一道题目——关于公交车视频监控系统记录乘客数量变化的问题。博主给出了输入输出示例,并解释了如何根据题目条件推断出初始站台可能的乘客人数范围,以及满足条件的解法。当解为负数时,表示不存在符合条件的初始人数,输出0。
摘要由CSDN通过智能技术生成

E. Bus Video System

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops.

If xx is the number of passengers in a bus just before the current bus stop and yy is the number of passengers in the bus just after current bus stop, the system records the number y−xy−x. So the system records show how number of passengers changed.

The test run was made for single bus and nn bus stops. Thus, the system recorded the sequence of integers a1,a2,…,ana1,a2,…,an (exactly one number for each bus stop), where aiai is the record for the bus stop ii. The bus stops are numbered from 11 to nn in chronological order.

Determine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to ww (that is, at any time in the bus there should be from 00 to ww passengers inclusive).

Input

The first line contains two integers nn and ww (1≤n≤1000,1≤w≤109)(1≤n≤1000,1≤w≤109) — the number of bus stops and the capacity of the bus.

The second line contains a sequence a1,a2,…,ana1,a2,…,an (−106≤ai≤106)(−106≤ai≤106), where aiai equals to the number, which has been recorded by the video system after the ii-th bus stop.

Output

Print the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to ww. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0.

Examples

input

Copy

3 5
2 1 -3

output

Copy

3

input

Copy

2 4
-1 1

output

Copy

4

input

Copy

4 10
2 4 1 2

output

Copy

2

Note

In the first example initially in the bus could be 00, 11 or 22 passengers.

In the second example initially in the bus could be 11, 22, 33 or 44 passengers.

In the third example initially in the bus could be 00 or 11

 passenger

大意:共N站,汽车每站变化ai人。问刚开始汽车上人数有多少种可能?

思路:用数学思想去做就很简单。先假设刚开始汽车上没人,用b [i]表示每站过后的人数。再排序 b [i]。 b[1],b[n]分别表示所有站点最多和最少的人数。假设有x人只需满足  0<=x<=w,-b[1]<=x<=w-b[n]即可。若求解负数则不满足输出0.

下面附上AC代码

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAXN 1000100
int a[MAXN],b[MAXN];
int main()
{int n,w;
while(scanf("%d%d",&n,&w)!=EOF)
{int i,f,p,q;
b[0]=0;
for(i=1;i<=n;i++)
{scanf("%d",&a[i]);
b[i]=a[i]+b[i-1];}
sort(b+1,b+n+1);
q=min(w,w-b[n]);
p=max(0,-b[1]);
f=q-p+1;
if(f>0)printf("%d\n",f);
else printf("%d\n",0);
}
return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值