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

时间: 2024-08-30 14:00:44

Codeforces Round #481 (Div. 3) E. Bus Video System的相关文章

Codeforces Round #481 (Div. 3)

Codeforces Round #481 (Div. 3) 链接 http://codeforces.com/contest/978 A. Remove Duplicates 题意 抹除重复元素 留下最后一个不相同元素 输出 分析 逆序将第一个出现的放在vector中, 逆序输出即可 #include<bits/stdc++.h> using namespace std; #define pb push_back #define sd(n) scanf("%d",&

Codeforces Round #436 (Div. 2) C. Bus

Codeforces Round #436 (Div. 2) C. Bus A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the pointx = a, immediately turns back and then moves to the point x = 0. After re

Codeforces Round #484 (Div. 2) B. Bus of Characters(markdowm版)

Codeforces Round #484 (Div. 2) B. Bus of Characters B. Bus of Characters time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output In the Bus of Characters there are nn rows of seat, each having 22

Codeforces Round #491 (Div. 2) E - Bus Number + 反思

E - Bus Number 最近感觉打CF各种车祸.....感觉要反思一下, 上次读错题,这次想当然地以为18!肯定暴了longlong 而没有去实践, 这个题我看到就感觉是枚举每个数字的个数,但是我觉得算得时候会爆longlong 就想用大数,但是我去看别人交的全部都是C++,就感觉是不是有别的方法, 想了半天感觉时间来不及了就强行上了个java,结果时间来不及... 以后写题首先要读清楚题目,其次不要想当然,要去实践!!!!!!!!!!! 真的很烦. import java.math.Bi

Codeforces Round #196 (Div. 2) B. Routine Problem

screen 尺寸为a:b video 尺寸为 c:d 如果a == c 则 面积比为 cd/ab=ad/cb (ad < cb) 如果b == d 则 面积比为 cd/ab=cb/ad  (cb < ad) 如果不相等时 如果a/b > c/d,则ad/bd > cb/db 则(ad > cb) screen尺寸可为 ad:bd, video的尺寸可为 cb:db 面积比为:cb*db/ad*bd = cb/ad (ad > cb) 如果a/b < c/d,则a

Codeforces Round #261 (Div. 2)

A. Pashmak and Garden 题意:已知两个顶点的坐标,如果能推断出另外两个顶点则输出(special judge).如果这两个顶点不是构成正方形的两个顶点, 则输出-1. 水题,1A,不多说. #include<cstdio> #include<iostream> #include<cstring> #include<cmath> using namespace std; int main() { int x1,x2,y1,y2,flag,x

Codeforces Round #436 (Div. 2)【A、B、C、D、E】

Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张卡片上都写有一个数,两个人每人选一个数,每人可以拿的卡片必须写有是自己选的数,问能否选择两个数使得两个人每人拿的卡片数一样多并且能拿光卡片.[就是看输入是不是只有两种数字] //:第一遍我看成字符串包含有选的数字也能拿,,这样写着居然过了..水题水题.. 1 #include<cstdio> 2

Codeforces Round #616 (Div. 2)解题报告

Codeforces Round #616 (Div. 2)解题报告 A. Even But Not Even 找两个奇数就行了. #include<bits/stdc++.h> using namespace std; void solve() { int n; string s; cin >> n >> s; string ans = ""; for(int i = 0; i < n; i++) { if(int(s[i] - '0')%2

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i