Codeforces Round #481 (Div. 3) E. Bus Video System

E. Bus Video System

Example 1
input
3 5
2 1 -3
output
3

Example 2
input
2 4
-1 1
output
4

Example 3
input
4 10
2 4 1 2
output
2

题目大意:

车上只会显示y-x(y是离开站时的人数,x时到站前的人数),问第一站的人数可以有几种

分析:

我们先分析下这个数学问题的式子,设a[i]=y[i]-x[i]……①
x[i+1]=y[i],这是显然易见的
然后我们将①从1到n项累加
可得Σa[i]=y[n]-x[1]……②
然后我们就可以通过②式来求每一站的最多可以站多少人,最少可以站多少人
x[1]=y[n]-Σa[i],y[n]∈[0,w]
最后将每一站的取值范围取交集,就是所要的答案区间,但是要考略到这个集合不存在的情况

  

code:

#define debug
#include<bits/stdc++.h>
#define pb push_back
#define dbg(x) cout<<#x<<" = "<<(x)<<endl;
#define lson l,m,rt<<1
#define cmm(x) cout<<"("<<(x)<<")";
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll>PLL;
typedef pair<int,ll>Pil;
const ll INF = 0x3f3f3f3f;
const ll inf=0x7fffffff;
const double eps=1e-8;
const int maxn =1e6+10;
const int N = 510;
const ll mod=1e9+7;
const ll MOD=1e9;
//------
//define
ll sum[maxn];
ll a[maxn]; 
//solve
void solve() {
	int n,m;
	while(cin>>n>>m){
		for(int i=1;i<=n;i++){
			cin>>a[i];
			sum[i]=sum[i-1]+a[i];
		}
		ll l=0,r=m;
		for(int i=1;i<=n;i++){
			r=min(r,m-sum[i]);
			l=max(l,0-sum[i]);
		}
		cout<<max(0ll,r-l+1)<<endl;
	}
}

int main() {
	ios_base::sync_with_stdio(false);
#ifdef debug
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	cin.tie(0);
	cout.tie(0);
	solve();
	/*
		#ifdef debug
			fclose(stdin);
			fclose(stdout);
			system("out.txt");
		#endif
	*/
	return 0;
}

  

 

转载于:https://www.cnblogs.com/visualVK/p/9038454.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值