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

UVALive 4725 Airport 贪心 二分 模拟

2016-02-23 13:44 337 查看

题目

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17264

题目来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105318#overview

简要题意:两个机场,每小时一些飞机到达,每小时安排一架飞机起航。

\qquad \quad \ \ \ \ 求任意时刻中两机场飞机数目的最大值的最大值的最小值。

题解

这个题目看似简单但却有点无从下手。

数据如果说比较小的话,那可以用dp来搞定,但这个规模肯定搞不定。

后面反正也想到了二分贪心,但是整个东西还是很难思考清楚,即贪心策略。

二分,然后去模拟,然后等到超过条件之后再去决定之前飞机的起飞情况。

维护当前机场的飞机数目,机场可以供起飞的数目还有可以起飞的总数。

超过数目后,判断可供起飞的数目是否为00就行了。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
// head
const int N = 5005;
const int E = 0, W = 1;

int a[2]
;
int cur[2];
int cnt[2];

bool ck(int k, int n) {
int tot = 0;
cur[E] = cur[W] = cnt[E] = cnt[W] = 0;

for (int i = 0; i < n; i++) {
cur[E] += a[E][i];
cur[W] += a[W][i];

while (cur[E] > k) {
if (!cnt[E] || !tot) return false;
cnt[E]--, cur[E]--, tot--;
}
while (cur[W] > k) {
if (!cnt[W] || !tot) return false;
cnt[W]--, cur[W]--, tot--;
}

if (cnt[E] < cur[E]) cnt[E]++;
if (cnt[W] < cur[W]) cnt[W]++;
if (tot < cur[E]+cur[W]) tot++;
}
return true;
}

int main() {
int t, n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", a[E]+i, a[W]+i);
}

int l = 1, r = n*20, ans;
while (l <= r) {
int mid = (l+r) >> 1;
if (ck(mid, n)) {
ans = mid;
r = mid-1;
} else {
l = mid+1;
}
}
printf("%d\n", ans - 1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: