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。在判断时,如果只有一边有飞机,那么直接起飞一架那边的飞机,否则将飞机保留下来后面判断时用,即两边都能起飞飞机时就把飞机暂时保留下来。
题目大意:某飞机场有两个通道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; }
相关文章推荐
- UVA 1450 Airport (二分)PS:两种思路的改错,想AC就看我!!!
- uva 1450 - Airport(贪心+二分)
- uva 1450 - Airport(二分)
- UVa 1450 Airport 解题报告(二分+策略)
- UVA 1450 Airport (二分)
- (advanced) UVA 二分+贪心 1450 - Airport
- UVA 1450 Airport(二分+想法题)
- UVa 1450 - Airport(二分)
- UVa1450 - Airport
- UVALive - 4725 Airport 分析+二分
- uva1450-Airport
- UVA 4725 Airport (贪心+二分)
- UVA 1450 LA 4725 Airport
- uvalive 4725 - Airport (二分)
- Uva-1450-Airport
- UVA1450-Airport
- UVA - 1450 Airport
- uva1450 - Airport
- UVALive 4725 Airport 贪心 二分 模拟
- UVA1450-Airport