题目
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17264
题目来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105318#overview
简要题意:两个机场,每小时一些飞机到达,每小时安排一架飞机起航。
求任意时刻中两机场飞机数目的最大值的最大值的最小值。
题解
这个题目看似简单但却有点无从下手。
数据如果说比较小的话,那可以用dp来搞定,但这个规模肯定搞不定。
后面反正也想到了二分贪心,但是整个东西还是很难思考清楚,即贪心策略。
二分,然后去模拟,然后等到超过条件之后再去决定之前飞机的起飞情况。
维护当前机场的飞机数目,机场可以供起飞的数目还有可以起飞的总数。
超过数目后,判断可供起飞的数目是否为 0 <script type="math/tex" id="MathJax-Element-2">0</script>就行了。
代码
#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][N];
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;
}