codeforces 116A Tram
来源:互联网 发布:空心软件测试平台 编辑:程序博客网 时间:2024/06/05 17:24
Linear Kingdom has exactly one tram line. It has n stops, numbered from1 to n in the order of tram's movement. At thei-th stop ai passengers exit the tram, whilebi passengers 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 exitbefore any entering passenger enters the tram.
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 integersai andbi (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 thei-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 thata1 = 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.
Print a single integer denoting the minimum possible capacity of the tram (0 is allowed).
40 32 54 24 0
6
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.
AC:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,up[1010],down[1010],sum[1010],gun[1010];
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d %d",&down[i],&up[i]);
sum[i]=up[i]-down[i];
}
gun[0]=sum[0];
for(int i=1;i<n;i++)
gun[i]=gun[i-1]+sum[i];
sort(gun,gun+n);
printf("%d\n",gun[n-1]);
}
- CodeForces-116A-Tram
- CodeForces 116A Tram
- Codeforces 116A Tram
- CodeForces 116A Tram
- codeforces 116A Tram
- Problem--116A--Codeforces--Tram
- codeforces 116A Tram(模拟水题)
- codeforces 116A(Tram) Java
- CodeForces 116 A.Tram(水~)
- Codeforces刷题之路——116A Tram
- 116A - Tram
- 116A - Tram
- CF 116A Tram
- A. Tram
- CodeForces 746C Tram
- 746 C. Tram codeforces
- codeforces 746 C. Tram
- 【找最值】#87 A. Tram
- springboot(十六):使用Jenkins部署Spring Boot
- 面向对象:谁家女朋友走丢了,快来认领!
- 趣图:工作一整天,你就写了 6 行代码?
- 漫画:什么是HashMap?
- Android开发笔记——串口通讯
- codeforces 116A Tram
- shiro(三)
- [操作系统] I/O硬件原理
- setPreferredSize和的setSize和setbounds的用法
- How To Install Java with Apt-Get on Ubuntu 16.04
- bzoj3926 [Zjoi2015]诸神眷顾的幻想乡(广义后缀自动机)
- shiro(四)
- Java中用正则表达式截取字符串
- ffmpeg 移植安卓