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:/ ...
随机推荐
- char如何储存3个字节或者4个字节
1.char字符存储的是Unicode编码的代码点,也就是存储的是U+FF00这样的数值,然而我们在调试或者输出到输出流的时候,是JVM或者开发工具按照代码点对应的编码字符输出的. 2. 所以虽然UT ...
- Vmware在NAT模式下网络配置详解
Vmware在NAT模式下网络配置详解 Linux中的网络配置对于接触Linux不久的小白菜来说,还是小有难度的,可能是不熟悉这种与windows系列迥然不同的命令行操作,也可能是由于对Linux的结 ...
- SQLServer中的Cross Apply、Outer Apply
https://www.2cto.com/database/201304/206330.html
- js 保留几位小数位数
定义和用法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字.
- javascript 闭包笔记
先来解释一下闭包: 1.闭包就是函数嵌套函数 2.内部函数可以引用外部函数的参数和变量 3.参数和变量不会被垃圾回收机制所收回( 垃圾回收机制就是用完变量之后就在内存中释放 ) 使用闭包的好处: ...
- Android RecyclerViewSwipeDismiss:水平、垂直方向的拖曳删除item
Android RecyclerViewSwipeDismiss:水平.垂直方向的拖曳删除item RecyclerViewSwipeDismiss是一种支持RecyclerView的水平.垂直 ...
- web项目log日志查看分析->流程理解
1.DEBUG [2017-07-10 11:38:41,705][] org.springframework.web.servlet.DispatcherServlet:865 - Dispatch ...
- [RxJS] Get current value out of Subject (BehaviorSubject)
When you want to get the current value of a subject, you need to switch BehaviorSubject, it always e ...
- [Jest] Use property matchers in snapshot tests with Jest
With the right process in place, snapshot tests can be a great way to detect unintended changes in a ...
- (IT/互联网行业)你给自己当前的职位拼几分?(评分标准,个人看法,勿喷~)
常常有身边的关系好的朋友或网友.问如今我该不该跳槽的问题. 我一般给他们的答复你能给当前的工作拼几分. 下面是我自己总结的一个评分标准.如有不当之处,勿喷~ --------------------- ...