24h購物| | PChome| 登入
2012-09-22 11:47:38| 人氣1,269| 回應0 | 上一篇 | 下一篇

[ACM-ICPC][Asia - Seoul][二分+Greedy] 4725 - Airport

推薦 0 收藏 0 轉貼0 訂閱站台

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}

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 (1$ le$n$ le$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$ le$ai, bi$ le$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 13 3 20 32 06 0 11 11 21 11 16 0

Sample Output 

0 3 5

我們去二分答案, 然後我們去隨著時間累加兩邊的飛機數量, 同時記錄 lea,
lea 是用來當狀況數量大於答案的時候, 一次能消去的飛機數量

特別注意 sa+sb > lea, lea++, 因為此時這個狀態就可以全部離開, 如果沒有, 則會產生負飛機的情況

或者是換另一個方式講, 前一次的 0 0 中間可以出幾台

#include <stdio.h>
#include <stdlib.h>
int a[5005], b[5005], n;
int chk(int rank) {
    int i, sa = 0, sb = 0;
    int dif1, dif2, lea = 0;
    for(i = 0; i < n; i++) {
        sa += a[i];
        sb += b[i];
        if(sa > rank)   dif1 = sa - rank;
        else dif1 = 0;
        if(sb > rank)   dif2 = sb - rank;
        else dif2 = 0;

        if(dif1 + dif2 > lea)
            return 0;

        if(sa == 0 && sb > 0)
            sb--;
        else if(sb == 0 && sa > 0)
            sa--;
        else if(sa > 0 && sb > 0 && sa+sb > lea)
            lea++;
    }
    return 1;
}
int main() {
    int t, i;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        for(i = 0; i < n; i++) {
            scanf("%d %d", &a[i], &b[i]);
        }
        int l = 1, r = 100000, m;
        while(l < r) {
            m = (l+r)>>1;
            if(chk(m))
                r = m;
            else
                l = m+1;
        }
        printf("%d\n", r-1);
    }
    return 0;
}

台長: Morris
人氣(1,269) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][數學] 12335 - Lexicographic Order
此分類上一篇:[ACM-ICPC][Asia - Seoul][Greedy+dp] 4731 - Cellular Network

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文