您的位置:首页 > 大数据 > 人工智能

UVa 1450 Airport (二分+思路)

2017-10-03 16:25 274 查看
题目链接:https://vjudge.net/problem/UVA-1450

题目大意:某飞机场有两个通道W和E。每一时刻都有一些飞机到达W通道或E通道(数目分别为ai和bi),每个通道的飞机按照来的顺序编号为0 1 2 ...,然后,每一时刻只能有一架飞机起飞。求任意时刻停留在机场的飞机的最大编号的最小值。

思路:首先二分答案ans,判断是否存在一种方案,使得最大编号为ans。在判断时,如果只有一边有飞机,那么直接起飞一架那边的飞机,否则将飞机保留下来后面判断时用,即两边都能起飞飞机时就把飞机暂时保留下来。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include<cctype>
#include <cstdlib>
#include<map>
#include<iostream>
#include<queue>
using namespace std;
typedef long long LL;
const int dr[] = {0,0,-1,1};
const int dc[] = {-1,1,0,0};
const int maxn = 5000 + 10;
int a[maxn], b[maxn];
int n;

bool judge(int Max)
{
//++Max;
int suma = 0, sumb = 0, res = 0;
for(int i = 1; i <= n; i++)
{
suma += a[i]; sumb += b[i];
int aa = max(suma-Max, 0);
int bb = max(sumb-Max, 0);
if(aa + bb > res) return false;
if(suma > 0 && sumb > 0 && suma + sumb > res) ++res;
else if(suma > 0 && sumb == 0) --suma;
else if(suma == 0 && sumb > 0) --sumb;
}
return true;
}

int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d%d", &a[i], &b[i]);
int L = 1, R = maxn*20, ans = -1;
while(L <= R)
{
int mid = (L + R) >> 1;
if(judge(mid)) {
ans = mid;
R = mid - 1;
}
else L = mid + 1;
}
printf("%d\n", ans-1);
}
return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: