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