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; }
相关文章推荐
- Dispatcher initialization failed
- AIM Tech Round (Div. 1) C. Electric Charges 二分
- salt服务端向客户端同步grains错误一枚
- Submission Details
- http://blog.csdn.net/limingchuan123456789/article/details/16849897
- 'hibernate.dialect' must be set when no Connection available
- 24-题目1436:Repair the Wall
- Paint类
- Jenkins进阶系列之——02email-ext邮件通知模板
- 关于 “does not contain bitcode.”的错误解决办法
- WindowsAPI - Net - WSAStartup - Baidu.com
- process launch failed: Security
- tools:context=".MainActivity的作用
- poj2010 Moo University - Financial Aid
- VS2008启动时提示“Package load failure”
- waiting for X server to shut down Server terminated successfully(0) ,closing log file
- AIDL service纪事 注意事项
- 人工智能 有限状态机 的时代过去了
- could not obtain the localhost address
- Rails 中的事物处理