E. Bus Video System(找刚开始车上,满足的人数,好题,细节多)

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 yxy−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 (1n1000,1w109)(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 (106ai106)(−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 0011 or 22 passengers.

In the second example initially in the bus could be 112233 or 44 passengers.

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


题意:输入 n,m,n为有n站,m为公交车的容量,下面一行有n个数,为n站上下车的情况,看看刚开始车上可以有多少种情况,满足 给出 n 站的数据,要是没有输出0,要是有,输出 多少种情况;

思路:这道题细节多,找出车上的上限和下限,在看看有没有交叉的情况,一定要注意交叉的情况

代码中有解释:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define Max 100010
#define INF 0x3f3f3f3f
#define ll long long 
ll a[Max];
int main()
{
	int i,j,n,m;
	while(~scanf("%d%d",&n,&m))
	{
		int x;
		int ma = -INF;
		int sum = 0;
		int flag = 1;
		int mi = INF;
		for(i = 0;i<n;i++)
		{
			scanf("%d",&x);
			sum += x;
			ma = max(ma,sum);   
			mi = min(mi,sum);	// 
		}
		// ma  找出来最大,当 ma为非负时,刚开始在车上的人数,上限不能超过 m - ma;
		// mi 找出最小,当 mi 为非正时,更开始在车上的人数,下限不能低于 m-(-mi);  
		// 当 上限和下限 有超过两个交叉的话,那么 刚开始车上的人数,没有满足的,输出0;
		// 有一个数字交叉的话,那么就只有一个数字满足,就是交叉的数字,输出1;
		// 当没有交叉时,直接求有几个数字满足就行了;
		// 一定要注意交叉的情况; 
		if(mi < -m||ma > m)
			flag = 0;
		if(mi<0&&ma>0&&-mi+ma>m)
		{
			flag = 0;
		}
		if(!flag)
		{
			printf("0\n");
			continue;
		}
		int k = m;
		if(mi<=0)
		{
			k = m + mi + 1;
		}
		if(ma>=0&&mi<=0)
		{
			k = k - ma;
		}
		else if(ma>=0)
		{
			k = k - ma + 1;
		}
		printf("%d\n",k);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/obsorb_knowledge/article/details/80313997