關於一些初級ACM競賽題目的分析和題解(四)
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.
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.
Print a single integer denoting the minimum possible capacity of the tram (0 is allowed).
4
0 3
2 5
4 2
4 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.
對於第一個例子,容量爲6就足夠了:
在第一站,到達前電車內的乘客人數爲0.然後,3名乘客進入電車,電車內的乘客人數爲3。
在第二站,2名乘客離開電車(1名乘客留在裏面)。 然後,5名乘客進入電車。 現在電車裏有6名乘客。
第三站有4名乘客離開電車(內有2名乘客)。 然後,2名乘客進入電車。 現在電車裏有4名乘客。
最後,電車內的所有剩餘乘客在最後一站下車。 現在電車裏沒有乘客,這是符合約束的。
由於電車內的乘客人數從未超過6人,因此6人的容量就足夠了。 此外,電車的容量不可能小於6.因此,6是正確的答案。
#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; // 輸出最終值
}
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:
- Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≤ i < 5).
- 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.
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.
Print a single integer — the minimum number of moves needed to make the matrix beautiful.
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
3
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
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) 行列位置到關鍵點的行列距離,並試着表示出來。