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).
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.
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.
3 5 2 1 -3
3
2 4 -1 1
4
4 10 2 4 1 2
2
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.
思路:记一个最大可能人数MAX,记录一个最小可能人数MIN,答案就是ans=MAX-MIN+1.如果ans<0.说明有冲突。
#include "iostream" #include "algorithm" using namespace std; const int Max=1e3+10; int sum[Max]; int main() { ios::sync_with_stdio(false); int n,w,x,MAX=0,MIN=0xfffffff; cin>>n>>w; for(int i=1;i<=n;i++){ cin>>x; sum[i]=sum[i-1]+x;//假设车最开始没人,到i站时,车上的人数 MAX=max(sum[i],MAX); MIN=min(sum[i],MIN); } MAX=w-MAX;//车上最多有MAX人,那么,初始人数不多与w-MAX MIN=MIN>0?0:-MIN;//如果最少人数为负数,那么初始人数不少于 -MIN,如果为正数,则不少于0即可 if(MAX-MIN+1<0)//判断是否有冲突 cout<<"0"<<endl; else cout<<MAX-MIN+1<<endl; return 0; }