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-roadW 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 roadsW and E. Each aircraft arriving onW 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 ofW 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 aircraftsA1, A2 andA3 receive the ranks 0, 1 and 2, respectively, and the aircraftsB1 and B2 receive the ranks 0 and 1, respectively. Then the control tower allows the aircraftB1 on the road E to take off, and B1 leaves the ground. At time 2, the aircraftsB3, B4, andB5 receive the ranks 1, 2 and 3, respectively. ThenA1 on the road W is allowed to take off, and it leaves the ground. At time 3, the aircraftsA4 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.
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 integern (1n5000) , the number of times. In the next n lines of each test case, thei-th line contains two integer numbers ai and bi, representing the number of arriving aircrafts on the roadW and E, respectively, at timei, where 0ai,bi20.
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.
3 1 1 1 3 3 2 0 3 2 0 6 0 1 1 1 1 2 1 1 1 1 6 0
0 3 5
有W跑道和E跑道,每时刻只能起飞一个飞机。每个跑道会不定时的进入一些飞机,把没有起飞的飞机从0开始标号,问这个过程中能达到的最小的最大标号是多少。
我发现一般求个什么最小的最大值这样类似的数据量又比较大的问题经常是二分,判断一个值行不行,最后找到临界值。这个题关键是怎么判断一个值是否满足条件。可以把t从0到N模拟,w,e分别表示此时W道和E道的飞机数量,用l表示此时W道最多可以飞出几架飞机,r表示E道最多可以飞出几架飞机,s表示此时一共可以最多飞出几架飞机。(虽然说一个时刻只能飞一架,但是可以假设一开始先存着不飞,到需要的时候一起飞)。那么如果w大于要检测的值m,就要从W飞出w-m架,如果不能飞出这么多就说明m不符合。同理如果e大于m,就要从E飞出e-m架。不一定l>w-m或s>e-m就满足,而是要min(w,s)>m或min(e,s)>m,因为l和r都是在另外一道一架飞机都不飞的情况下的值,要考虑到两个跑道可能都飞了飞机。
还要注意的是,每过一个时间,如果W道有飞机,l++,但是l不能大于目前w的数量,同理如果E道有飞机,r++,r不能大于e的数量。如果W或E任何一道有飞机,s++,s不能大于w+e的数量。
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<map> #define INF 0x3f3f3f3f #define MAXN 5010 #define MAXM 60 #define eps 1e-9 #define pii pair<int,int> using namespace std; int T,N,a[MAXN],b[MAXN]; int check(int m){ int l=0,r=0,w=0,e=0,s=0; for(int t=0;t<N;t++){ w+=a[t]; e+=b[t]; if(w>m){ int f=min(l,s); if(w-f>m) return 0; l-=w-m; s-=w-m; w=m; } if(e>m){ int f=min(r,s); if(e-f>m) return 0; r-=e-m; s-=e-m; e=m; } if(w) l++; l=min(l,w); if(e) r++; r=min(r,e); if(w||e) s++; s=min(s,w+e); } return 1; } int bsearch(int L,int R){ int m; while(L<R){ m=(L+R)/2; if(check(m)) R=m; else L=m+1; } return L; } int main(){ freopen("in.txt","r",stdin); scanf("%d",&T); while(T--){ scanf("%d",&N); for(int i=0;i<N;i++) scanf("%d%d",&a[i],&b[i]); printf("%d\n",bsearch(1,20*N)-1); } return 0; }