Airport
A big city has an international airport handling 40 million passengers a year. But this is notorious as one of the most congested airports in the world. In this airport, there is only one landing strip as in the above figure. Therefore the landing strip is always crowded with a lot of aircrafts waiting for a takeoff. There are two ways, say west-road W and east-road E, to approach the landing strip. The aircrafts are waiting for a takeoff on the two roads as in the above figure.

At each time t, an arbitrary number of aircrafts arrive on the roads W and E. Each aircraft arriving on W orE at time t receives a rank, which is equal to the number of the waiting aircrafts on the same road to precede it. Then the one of W and E is chosen by a control tower, and the most front aircraft on the road leaves the ground. Given an information of the arriving aircrafts at times, we are concerned in the takeoff schedule of the control tower to minimize the maximum rank of the aircrafts.

For example, the above table represents the aircrafts arriving on the roads W and E at each time. At time 1, the aircrafts A1, A2 and A3 receive the ranks 0, 1 and 2, respectively, and the aircrafts B1 and B2 receive the ranks 0 and 1, respectively. Then the control tower allows the aircraft B1 on the road E to take off, and B1 leaves the ground. At time 2, the aircrafts B3, B4, and B5 receive the ranks 1, 2 and 3, respectively. Then A1 on the road W is allowed to take off, and it leaves the ground. At time 3, the aircrafts A4 and A5 receive the ranks 2 and 3, respectively. So the maximum rank of the aircrafts is 3, and this is the minimum of the maximum rank over all the possible takeoff schedules.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test cases Tis given on the first line of the input. The first line of each test case contains an integer n (1 n 5000) , the number of times. In the next n lines of each test case, the i-th line contains two integer numbers ai and bi, representing the number of arriving aircrafts on the road W and E, respectively, at timei, where 0 ai, bi 20.
Output
Your program is to write to standard output. Print exactly one line for each test case. The line contains the minimum of the maximum rank over all the possible takeoff schedules.
The following shows sample input and ouput for three test cases.
Sample Input
3
1
1 1
3
3 2
0 3
2 0
6
0 1
1 1
1 2
1 1
1 1
6 0
Sample Output
0
3
5
题意: 一个机场有W,E两个飞机起飞的跑道, 每次只可以起飞一部飞机, 随机从W,E两个跑道选一部飞机起飞,
但是要保证最后全部飞机起飞完,求整个过程中最大的飞机滞留编号最小.(从0开始)
解题思路:
1. 又是一个最大中求最小问题, 二分法求解. 枚举一个结果, 然后判断结果是否可行, 然后往
左/右区间进行逼近.
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 5005
int n;
int sa[MAX], sb[MAX];
inline int max(int a, int b)
{
return a > b ? a : b;
}
bool judge(int m)
{
int a = 0, b = 0, sum = 0;
for(int i = 1; i <= n; ++i)
{
int ta = max(sa[i]-m, 0);
int tb = max(sb[i]-m, 0);
if(ta > a || tb > b || (ta+tb) > sum)
return false;
if(sa[i] > ta) a++;
if(sb[i] > tb) b++;
if( (sa[i]+sb[i]) > sum ) sum++;
}
return true;
}
int main()
{
// freopen("input.txt", "r", stdin);
int caseNum, i;
scanf("%d", &caseNum);
while(caseNum--)
{
scanf("%d", &n);
memset(sa, 0, sizeof(int)*(n+1));
memset(sb, 0, sizeof(int)*(n+1));
for(i = 1; i <= n; ++i)
{
scanf("%d %d", &sa[i], &sb[i]);
sa[i] += sa[i-1];
sb[i] += sb[i-1];
}
int left = max(sa[1], sb[1]);
int right = max(sa[n], sb[n]);
int mid;
while(left < right)
{
mid = (left+right)/2;
if( judge(mid) ) right = mid;
else left = mid+1;
}
printf("%d\n", left == 0 ? left : left-1);
}
return 0;
}