关于一些初级ACM竞赛题目的分析和题解(四)。

                                 

关于一些初级ACM竞赛题目的分析和题解(四)

今天做的题略有难度,以前的题目简单,只是对一行字符串,对一组数字,进行操作或判断,下面是一些复杂性的,具有图像性的问题的分析。惊讶
A. Tram
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 aipassengers exit the tram, while bi 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 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.


对于第一个例子,容量为6就足够了:


   在第一站,到达前电车内的乘客人数为0.然后,3名乘客进入电车,电车内的乘客人数为3。
   在第二站,2名乘客离开电车(1名乘客留在里面)。 然后,5名乘客进入电车。 现在电车里有6名乘客。
   第三站有4名乘客离开电车(内有2名乘客)。 然后,2名乘客进入电车。 现在电车里有4名乘客。
最后,电车内的所有剩余乘客在最后一站下车。 现在电车里没有乘客,这是符合约束的。
由于电车内的乘客人数从未超过6人,因此6人的容量就足够了。 此外,电车的容量不可能小于6.因此,6是正确的答案。

   
对于此题的变量范围(2 ≤ n ≤ 1000  (0 ≤ ai, bi ≤ 1000)   先输入n 表示电车停留n个站台  下面第一行表示原来人数 和  上车人数,  在下面n-1行表示 下车人数 和上车人数  问 电车最大容量  下面是代码:
#include <iostream>
using namespace std;
typedef long long ll;
int n,b,c,d,e;   //  定义全局变量
main()
{
    cin>>n;   //  输入n值
    for(int i=0;i<n;i++)
        {

       cin>>b>>c;d=d-b+c;  // 执行
       if(d>e)  //  判断条件
        e=d;
         }
        cout<<e;  //  输出最终值
}


A. Beautiful Matrix
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You've got a 5 × 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix:

  1. Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≤ i < 5).
  2. Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 ≤ j < 5).

You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful.

Input

The input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one.

Output

Print a single integer — the minimum number of moves needed to make the matrix beautiful.

Examples
input
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
output
3
input
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 0
output
1

  这个题目看起来有些复杂,但分析后发现就是求把1的位置移到正中心所需要的步数,下面是代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
int f;
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++){
cin>>f;     //  确定1所在的行列位置
if(f==1)
cout<<fabs(3-i)+fabs(3-j);  //  行数-3的绝对值 加上 列数-3的绝对值 即为步数
} 


}
遇到类似图象的输入的情况时  要找到关键点例如(3,3) 行列位置到关键点的行列距离,并试着表示出来。







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章