Canada Cup 2016 D. Contest Balloons 好题。优先队列 + 简单贪心
http://codeforces.com/contest/725/problem/D
这题一看就是贪心的了,w - t最小的那个,肯定是优先打死。
但是一直都不会写,为什么呢,因为这个太像二分答案了,一看到这题就想到了二分答案,二分排名,二分扔掉气球......
但是是不行的啊。因为扔掉n个,可能会变优,但是不能保证扔掉更小或者扔掉更多,那个更优呢?(可能需要三分答案?晚上回来试试)
其实这题只需要用优先队列维护。
用两个队列,que保存本来排名就比我高的那些队,优先弹出w - t最小的。
第二个队列保存后面那些队,优先弹出t值最大的那些队(这个用来改变我的t后(就是送完气球后),把其他排名升上来进队que)
然后每一次,够更新答案即可。
其实就是贪心 + 模拟题目意思(把升上来的队加入来)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
struct node {
LL t;
LL w;
LL dis;
int id;
bool operator < (const struct node & rhs) const {
return dis > rhs.dis;
}
}a[maxn];
struct cmp {
bool operator() (struct node a, struct node b) {
return a.t < b.t;
}
};
priority_queue<struct node> que;
priority_queue<struct node, vector<struct node>, cmp> out;
int n;
int ans = ;
void work() {
cin >> n;
int pos;
for (int i = ; i <= n; ++i) {
cin >> a[i].t >> a[i].w;
a[i].dis = a[i].w - a[i].t + ;
a[i].id = i;
if (a[i].t > a[].t) {
que.push(a[i]);
++ans;
} else {
if (i == ) continue;
out.push(a[i]);
}
}
// cout << que.size() << " " << out.size() << endl;
LL left = a[].t;
// cout << ans << endl;
while (left > && !que.empty()) {
struct node t = que.top();
if (left >= t.dis) {
left -= t.dis;
que.pop();
while (!out.empty()) {
struct node temp = out.top();
if (temp.t > left) {
que.push(temp);
out.pop();
} else break;
}
} else break;
int cc = que.size() + ;
ans = min(ans, cc);
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
IOS;
work();
return ;
}
题解:
725D - Contest Balloons
I (a problem author) want to thank the ICPC Live staff. During the World Finals a commentator ([user:Egor] maybe?) said that contests always have balloons and maybe someone should invent a problem about them for the next finals (I'm not organizing those though). Thanks for the idea!
If you just want the solution, read only the last paragraph. Previous ones describe how to find a correct approach. It may lead to a slightly more complicated solution, but you will learn something for sure.
To come up with a solution, you must try iterating over some value or maybe binary searching an optimal value. Let's list a few reasonable ideas. Try to binary search the answer (for fixed final place of Limak you must say if he can get at least this place). Or iterate over the number of disqualified teams (if Limak should beat exactly i teams, what is the best place he can get?). Or iterate over the number of balloons Limak should give away.
The last idea turns out to be fruitful. The first observation is that you don't have to consider every possible number of balloons to give away. We can assume that the final number of balloons will be either 0 or ti for some i. Otherwise Limak could give away some more balloons and his place won't be worse (if there are no more teams, let's imagine he can destroy them). We use here a simple observation that the i-th team either will be disqualified or will still have exactly ti balloons, i.e. values ti won't change.
We have O(n) possibilities for the final number of balloons. Let's first solve a problem in by considering possibilities separately, each in . Let's say Limak will have k balloons at the end. He can give away t1 - k balloons. He should try to get rid of teams with ti > k because worse teams don't affect his place. You should sort teams with ti > k by the number of balloons they need to get disqualified i.e. wi - ti + 1. It's easiest for Limak to get rid of teams with small value of wi - ti + 1 so you should just check how many of those values can be chosen so that the sum of them won't exceed t1 - k. It turns out to be possible to improve the above by considering possibilities in the decreasing order. Think what happens when you decrease k. Some more teams become a possible target and you should consider their values wi - ti + 1. A sorted set of those values should be useful. Try to invent a solution yourself now. Then you can see one simple version in the next paragraph.
Now let's see an greedy solution. Create a variable k representing the final number of balloons Limak has, initially k = t1. Create and maintain a multiset with valueswi - ti + 1 of teams better than the current k, i.e. teams with ti > k. If it makes sense to give away some more balloons, Limak has to eliminate at least one of those better teams (otherwise he can stop now). It's easiest to eliminate a team with the smallest wi - ti + 1, so we can just remove the smallest element from the set and decrease k by its value. Maybe some more teams become better than the current k and you should add those to the multiset. Note that the multiset contains exactly those teams which would win with Limak in the current scenario, so after every change of k you can update the answer 'ans = min(ans, my_multiset.size() + 1)'. Remember to stop if k becomes smaller than 0.
Canada Cup 2016 D. Contest Balloons 好题。优先队列 + 简单贪心的更多相关文章
- Canada Cup 2016 D. Contest Balloons
最近好弱做什么题目都是做一晚上 这是合肥站炼铜后遗症? 这题就是贪心 我已开始还写了1小时---三分-----. #include<bits/stdc++.h> using namespa ...
- 【Codeforces Round 725】Canada Cup 2016
模拟Canada Cup 2016,ABC三题,Rank1376 第三题卡住了 Codeforces 725 C 求出两个相同字符的位置,记为x和y. 然后考虑把相同的那个字符放在第一行的什么地方, ...
- Canada Cup 2016 C. Hidden Word
C. Hidden Word time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Canada Cup 2016 C. Hidden Word 构造模拟题
http://codeforces.com/contest/725/problem/C Each English letter occurs at least once in s. 注意到题目有这样一 ...
- Codeforces Canada Cup 2016
A. Jumping Ball time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- CodeForces Canada Cup 2016【A,B,C,D】
CodeForces 725A: 思路就是如果"最左"不是'>'这个了,那么这个右边的一定不可能到达左边了: 同理最右: CodeForces 725B: 有两个空姐,一个从 ...
- 8VC Venture Cup 2016 - Final Round C. Package Delivery 优先队列
C. Package Delivery 题目连接: http://www.codeforces.com/contest/627/problem/C Description Johnny drives ...
- codeforces 725D . Contest Balloons(贪心+优先队列)
题目链接:codeforces 725D . Contest Balloons 先按气球数从大到小排序求出初始名次,并把名次排在第一队前面的队放入优先队列,按w-t-1值从小到大优先,然后依次给气球给 ...
- Contest Balloons
Contest Balloons 题目链接:http://codeforces.com/problemset/problem/725/D 贪心+枚举 每次在排名在自己前面的选出w-t值最小的送气球,更 ...
随机推荐
- OpenCV——PS滤镜算法之 球面化 (凹陷效果)
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- HihoCoder1650 : 扁平化管理([Offer收割]编程练习赛38)(二分)
描述 小Hi的公司包括CEO在内一共有N名员工.这N名员工的上下级关系形成树形结构,CEO处于树根,普通员工处于叶子节点. 现在公司希望管理扁平化,要求树形结构中的层级不超过L层.此外,假设A是B的直 ...
- 1080 Graduate Admission (30)(30 分)
It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...
- P1880 [NOI1995]石子合并[区间dp+四边形不等式优化]
P1880 [NOI1995]石子合并 丢个地址就跑(关于四边形不等式复杂度是n方的证明) 嗯所以这题利用决策的单调性来减少k断点的枚举次数.具体看lyd书.这部分很生疏,但是我还是选择先不管了. # ...
- ACM学习历程—BestCoder 2015百度之星资格赛1003 IP聚合(set容器)
Problem Description 当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊想知道在某个固定的子网掩码下,有多少个网络地址.网 ...
- Windows下Anaconda安装 python + tensorflow
下载安装Anaconda 首先下载Anaconda,可以从清华大学的镜像网站进行下载. 安装Anaconda,注意安装时不要将添加环境变量的选项取消掉. 安装完成之后,在安装目录下cmd,输入 con ...
- JAVA 编程思想二
1: java 单根继承的优点? 方便垃圾回收: 垃圾回收的设计会方便实现. 多重继承的函数重名的问题. 2: 向下转型和向上转型? 向下转型不安全,向上转型安全. 3: system.g ...
- jvm学习五: 方法执行过程
方法执行过程:Java各个大版本更新提供的新特性(需要简单了解)
- Redux API之applyMiddleware
applyMiddleware(...middlewares) 使用包含自定义功能的 middleware 来扩展 Redux 是一种推荐的方式.Middleware 可以让你包装 store 的di ...
- PCL中异常处理机制
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=287 本节我们主要讨论PCL在编写和应用过程中如何利用PCL的异常机制,提高 ...