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.
![\epsfbox{p4725.eps}](https://i-blog.csdnimg.cn/blog_migrate/4f04d7c99c5b9840701840989204808b.png)
At each time t, an arbitrary number of aircrafts arrive on the roads W and E. Each aircraft arriving on W or E 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.
roads | ||||
W | E | |||
times | ||||
1 | A1, A2, A3 | B1, B2 | ||
2 | B3, B4, B5 | |||
3 | A4, A5 |
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 T is given on the first line of the input. The first line of each test case contains an integer n (1n
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 time i, 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 两条跑道,两个跑道里面的飞机编号为0,1,2。。每个时刻只能起飞任一跑道里面的一架飞机,然后重新编号,每个时刻会有ai bi架飞机到达跑到后面,给你时间表,每个时刻到达的飞机数,求最大编号的最小值
很明显的二分,但是不好判断,对于最大编号k,只是记录一共的起飞飞机次数,只是一个通道里面没有飞机的时候注意一下,对于任意时刻,如果当前两个通道变成符合小于等于k需要起飞的次数小于之前公共的起飞次数,那么当前k就符合,反之如果有一个时刻不行当前k则不行
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <cmath>
#include <string>
#include <queue>
#include <set>
using namespace std;
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#endif
const int INF = 0x3f3f3f3f;
const int maxn = 5000 + 50;
int n;
int W[maxn];
int E[maxn];
int judge(int k) {
int w = 0;
int e = 0;
int sum = 0; //总共起飞次数(两通道都有飞机时的起飞次数)
for(int i=0; i<n; i++) {
w += W[i];
e += E[i];
int ww = max(w-k, 0);//使得W小于等于K需要的最少起飞次数
int ee = max(e-k, 0);//使得E小于等于K需呀的最少起飞次数
if(ww + ee > sum) return 0;
if(w==0 && e) e--;
if(e==0 && w) w--;
if(w && e && w+e>sum) sum++;
}
return 1;
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i=0; i<n; i++) scanf("%d%d", &W[i], &E[i]);
int l = 0, r = maxn * 20;
while(l < r) {
int mid = l + (r - l) / 2;
if(judge(mid)) r = mid;
else l = mid+1;
}
int ans = max(l-1, 0);
printf("%d\n", ans); //编号0开始
}
return 0;
}