Bus Video System CodeForces - 978E (思维)
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 aiaiis 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
3 5
2 1 -3
3
2 4
-1 1
4
4 10
2 4 1 2
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 00 or 11 passenger.
题意:
给你一个含有n个整数的数组,每一个数a[i]代表汽车在站i时,车上增多了a[i]个人,如果a[i]为负,代表减少了人数。
并告诉你这个汽车的最大承载力为w个人,
请你判断初始时汽车上有多少个人,才满足整个数组的情况,。
如果某一个情况,车上的人数为负,或者人数大于w,那么说明这个数组时不合理的,。这时请输出0
思路:
可以抽象为,求这个数组的前缀和数组中的最大值和最小值,。只要最大值不大于容量,再判断下最低值的绝对值不大于容量。就可以说明是合理的。
然后可以的方案数中初始的人数一定是连续的,那么这些人数中的最大值是min(w-maxsum,w) ,即不让过程中容量大于w的最大值。
最小值是max(0,-1*minsum),然后最大值减去最小值+1就是答案了。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = ; while (b) {if (b % )ans = ans * a % MOD; a = a * a % MOD; b /= ;} return ans;}
inline void getInt(int* p);
const int maxn = ;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
// HFUU-QerM
// 21:49:59
ll n;
ll w;
ll a[maxn];
int main()
{
//freopen("D:\common_text\code_stream\in.txt","r",stdin);
//freopen("D:\common_text\code_stream\out.txt","w",stdout);
gbtb;
cin >> n >> w;
repd(i, , n)
{
cin >> a[i];
}
ll f = -1e18;
ll g = 1e18;
ll v = 0ll;
repd(i, , n)
{
v += a[i];
f = max(f, v);
g = min(g, v);
}
// db(f);
// db(g);
if ((abs(f)) > w || abs(g) > w)
{
cout << << endl;
} else
{
ll s = w - f;
ll x = 0ll;
// db(s);
s=min(s,w);
if (g < )
{
x = - * g;
}
// db(x);
if (x > s)
{
cout << << endl;
} else
{
cout << s - x + 1ll << endl;
}
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Bus Video System CodeForces - 978E (思维)的更多相关文章
- cf978E Bus Video System
The busses in Berland are equipped with a video surveillance system. The system records information ...
- Codeforces 978E:Bus Video System
题目链接:http://codeforces.com/problemset/problem/978/E 题意 一辆公交车,在每站会上一些人或下一些人,车的最大容量为w,问初始车上可能有的乘客的情况数. ...
- CF978E Bus Video System【数学/前缀和/思维】
[链接]: CF [分析]: 设上车前人数 x ,中途最大人数为 x+max ,最小人数为 x+min (max≥0,min≤0) 可得不等式组 x+max≤w, x+min≥0 整数解个数为 max ...
- pygame.error: video system not initialized
在pygame写游戏出现pygame.error: video system not initialized 源代码 import sysimport pygamedef run_game(): py ...
- Codeforces 424A (思维题)
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- CodeForces - 417B (思维题)
Crash Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
- Error Correct System CodeForces - 527B
Ford Prefect got a job as a web developer for a small company that makes towels. His current work ta ...
- Codeforces 1060E(思维+贡献法)
https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...
- Queue CodeForces - 353D (思维dp)
https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...
随机推荐
- MySQL并行复制的一个坑
早上巡检数据库,发现一个延迟从库的sql_thread中断了. Last_SQL_Errno: 1755 Last_SQL_Error: Cannot execute the current even ...
- JavaScript -- 时光流逝(九):Window 对象、Navigator 对象
JavaScript -- 知识点回顾篇(九):Window 对象.Navigator 对象 1. Window 对象 1.1 Window 对象的属性 (1) closed: 返回窗口是否已被关闭. ...
- Java实现鼠标随机移动
---恢复内容开始--- 以前在公司工作的时候,电脑限制重重,不允许改锁屏时间,又不允许下载和安装软件. 需要在家办公support的时候,又没有什么事,但还是必须在线,所以就写了个小程序让鼠标自己随 ...
- 团队作业——Alpha冲刺
团队作业--Alpha冲刺 时间安排及内容要求 时间 内容 11.1-11.16 12次 Scrum 11.16-11.20 测试报告 与 用户反馈 11.21-11.24 展示博客 11.25 课堂 ...
- Windows下mysql服务的安装与卸载
安装 mysqld -install 也可以指定mysql安装服务的文件 my.ini文件配置好后就可以在cmd中安装mysqld服务了,在cmd中运行命令:mysqld --install MySQ ...
- WPFの无边框窗体以及控件的移动
对于WPF,一旦隐藏了标题栏,就无法移动,这时候需要重写移动方法,下面列举常见的三种方式方式. 方式一:重写OnMouseLeftButtonDown protected override void ...
- Java序列化由于没有指定serialVersionUID导致报错
z.JobPersistenceException: Couldn't retrieve job because the BLOB couldn't be deserialized: com.mode ...
- Linux:Day6(上) egrep、条件测试
egrep及扩展的正则表达式: egrep = grep -E 扩展正则表达式的元字符: 或者:a | b 练习: 1.显示当前系统root.centos或user1用户的默认shell和UID: 2 ...
- 初学Python——文件操作
一.文件的打开和关闭 1.常用的打开关闭语句 f=open("yesterday","r",encoding="utf-8") #打开文件 ...
- Java内存模型(和堆栈等不是同一层次的划分)
什么叫Java内存模型? 现代计算机通过指令的重排序来提升计算机的性能,而没有限制条件的指令重排序会使得程序的行为不可预测,JMM就是通过一系列的操作规则限制指令重排序的方式使得指令重排序不会破坏JM ...