UVa 1450 Airport 解题报告(二分+策略)

56 篇文章 0 订阅
5 篇文章 0 订阅

1450 - Airport

Time limit: 3.000 seconds

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.

\epsfbox{p4725.eps}

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  
   WE
times    
 1 A1,A2, A3B1,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.

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 integern (1$ \le$n$ \le$5000) , 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 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 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

    解题报告: 最小的最大值问题。一般都是二分。本题的二分判断部分很难想。《训练指南》里也将其划分为Advanced。

    判读策略为:如果只有一边有飞机,那么直接起飞;否则,将这次起飞的机会保留下来,如果以后发现起飞的机会不够两边所有的飞机起飞,那么增加编号。

    保留机会部分比较难理解。它没有直接决定每次谁起飞,而是判断机会是否足够。代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <string>
using namespace std;

#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define mem(a) memset((a), 0, sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
void work();

int main()
{
#ifdef ACM
    freopen("in.txt", "r", stdin);
//    freopen("in.txt", "w", stdout);
#endif // ACM

    work();
}

/*****************************************/

int n;
int a[5555], b[5555];

bool check(int space)
{
    int an = 0, bn = 0;
    int have = 0;

    ff(i, n)
    {
        an += a[i];
        bn += b[i];

        int needa = max(an - space, 0);
        int needb = max(bn - space, 0);

        if(needa + needb > have) return false;

        if(an == 0 && bn)
            bn--;
        else if(bn == 0 && an)
            an--;
        else if(an + bn > have)
            have++;
    }

    return true;
}

void work()
{
    int T;
    scanf("%d", &T);
    ff(cas, T)
    {
        scanf("%d", &n);
        ff(i, n) scanf("%d%d", a+i, b+i);

        int l = 0, r = 100000;
        while(l <= r)
        {
            int m = (l+r)/2;

            if(check(m))
                r = m - 1;
            else
                l = m + 1;
        }

        r = max(r, 0);
        printf("%d\n", r);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值