https://cn.vjudge.net/contest/245783#problem/E
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 0
0or 11 passenger.
题意:现有n个公交车站,给出每个公交车站上下车的人数变化,正为上车,负为下车,公交车的最大容量为w。问,最开始时,公交车的可能人数有多少种。如果任何人数都与后面给出的上下情况冲突的话,输出0.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//思路:用数学思想去做就很简单。先假设刚开始汽车上没人,用aa [i]表示每站过后的人数。再排序 //aa[i]。
// aa[1],aa[n]分别表示所有站点最多和最少的人数。
// 假设有x人只需满足 0<=x<=w,-aa[1]<=x<=w-aa[n]即可。
// 若求解负数则不满足输出0.
int aa[200005];
int main()
{
ll n,w;
cin>>n>>w;
for(int i=1;i<=n;i++)
{
int k;//k是车站上的人数的变化量
cin>>k;//假设一开始是没有人在车上
aa[i]=aa[i-1]+k;//求前缀和
}
sort(aa+1,aa+1+n);
ll maxn=min(w,w-aa[n]);//maxn代表所有可能的最大值,这一步排除了最大值大于w的可能性
ll minn=max(0,-aa[1]);//如果*min_element(aa+1,aa+1+n)小于0,就说明一开始就要有人,人数
//为其绝对值,如果是正数那就取0,因为不管怎么变化最小值都是正数说明一开始是可以没有人的
if(maxn-minn+1>0)
cout<<maxn-minn+1<<endl;
else
cout<<0<<endl;
return 0;
}