C - Tram
Problem description
Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n in the order of tram's movement. At the i-th stop ai passengers exit the tram, while bipassengers enter it. The tram is empty before it arrives at the first stop. Also, when the tram arrives at the last stop, all passengers exit so that it becomes empty.
Your task is to calculate the tram's minimum capacity such that the number of people inside the tram at any time never exceeds this capacity. Note that at each stop all exiting passengers exit before any entering passenger enters the tram.
Input
The first line contains a single number n (2 ≤ n ≤ 1000) — the number of the tram's stops.
Then n lines follow, each contains two integers ai and bi (0 ≤ ai, bi ≤ 1000) — the number of passengers that exits the tram at the i-th stop, and the number of passengers that enter the tram at the i-th stop. The stops are given from the first to the last stop in the order of tram's movement.
- The number of people who exit at a given stop does not exceed the total number of people in the tram immediately before it arrives at the stop. More formally,
. This particularly means that a1 = 0.
- At the last stop, all the passengers exit the tram and it becomes empty. More formally,
.
- No passenger will enter the train at the last stop. That is, bn = 0.
Output
Print a single integer denoting the minimum possible capacity of the tram (0 is allowed).
Examples
Input
4
0 3
2 5
4 2
4 0
Output
6
Note
For the first example, a capacity of 6 is sufficient:
- At the first stop, the number of passengers inside the tram before arriving is 0. Then, 3 passengers enter the tram, and the number of passengers inside the tram becomes 3.
- At the second stop, 2 passengers exit the tram (1 passenger remains inside). Then, 5 passengers enter the tram. There are 6 passengers inside the tram now.
- At the third stop, 4 passengers exit the tram (2 passengers remain inside). Then, 2 passengers enter the tram. There are 4 passengers inside the tram now.
- Finally, all the remaining passengers inside the tram exit the tram at the last stop. There are no passenger inside the tram now, which is in line with the constraints.
Since the number of passengers inside the tram never exceeds 6, a capacity of 6 is sufficient. Furthermore it is not possible for the tram to have a capacity less than 6. Hence, 6 is the correct answer.
解题思路:题目的意思就是找出上下车过程中车内的最多人数,即为车的至少容量,简单水过。题目已经保证了一开始下车的人数为0,最后上车的人数也为0,并且最后车内的人将全部下车。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,a,b,nowcap=,maxcap=;
cin>>n;
while(n--){
cin>>a>>b;
nowcap-=a;nowcap+=b;
maxcap=max(maxcap,nowcap);
}
cout<<maxcap<<endl;
return ;
}
C - Tram的更多相关文章
- [最短路径SPFA] POJ 1847 Tram
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...
- POJ 1847 Tram (最短路)
Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- (简单) POJ 1847 Tram,Dijkstra。
Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...
- Codeforces Round #386 (Div. 2) C. Tram
C. Tram time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- Tram
Tram 题目大意:给你一个图,这个图上有n点和边.点上有开关,开关开始指向一条道路,拨动开关可以使开关指向由开关出发的任意一条路径,读入,a,b,求,至少要拨动几次才能从a点走到b点. 注释:n&l ...
- POJ1847 Tram
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20274 Accepted: 7553 Description ...
- poj1847 Tram(最短路dijkstra)
描述: Tram network in Zagreb consists of a number of intersections and rails connecting some of them. ...
- POJ 1847 Tram (最短路径)
POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...
- POJ1847:Tram(最短路)
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20116 Accepted: 7491 题目链接:http:/ ...
随机推荐
- table案例一
实现效果: html <style> #mform{ margin: 10px; } #mtable{ border-collapse: collapse; } #mtable thead ...
- drf06 认证Authentication 权限Permissions 限流Throttling
为了方便接下来的学习,我们创建一个新的子应用 four python manage.py startapp four 因为接下来的功能中需要使用到登陆功能,所以我们使用django内置admin站点并 ...
- Python-----基本操作
Python是一种简单易学,且功能强大的编程语言.它是面向对象的编程语言. 对象的意义: 对象可以通过一个“.” 的方式来调用这个对象的方法. Python环境安装配置: Python是一种通用的计算 ...
- 【模板】Link-Cut Tree
#include<cstdio> #include<algorithm> #define N 500010 #define rg register #define ls (c[ ...
- Codeforces 912D - Fishes
传送门:http://codeforces.com/contest/912/problem/D 本题是一个概率问题——求数学期望. 在一个n×m的方格中,有k个“*”.每个格子里可能有0~1个“*”. ...
- SQLServer中的Cross Apply、Outer Apply
https://www.2cto.com/database/201304/206330.html
- Boolean Expressions
Boolean Expressions Time Limit: 1000MS Memory Limit: 30000K Description The objective of the ...
- [poj2417]Discrete Logging_BSGS
Discrete Logging poj-2417 题目大意:求$a^x\equiv b(mod\qquad c)$ 注释:O(分块可过) 想法:介绍一种算法BSGS(Baby-Step Giant- ...
- ASP.NET MVC 提供与訪问 Web Api
ASP.NET MVC 提供与訪问 Web Api 一.提供一个 Web Api 新建一个项目.类型就选 "Web Api". 我用的是MVC5,结果生成的项目一大堆东西.还编译只 ...
- Zepto Code Rush 2014-A. Feed with Candy(HACK)
A. Feed with Candy time limit per test 2 seconds memory limit per test 256 megabytes input standard ...