poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)
Description
Bessie noted that although humans have many universities they can attend, cows have none. To remedy this problem, she and her fellow cows formed a new university called The University of Wisconsin-Farmside,"Moo U" for short.
Not wishing to admit dumber-than-average cows, the founders created an incredibly precise admission exam called the Cow Scholastic Aptitude Test (CSAT) that yields scores in the range ..,,,.
Moo U is very expensive to attend; not all calves can afford it.In fact, most calves need some sort of financial aid ( <= aid <=,). The government does not provide scholarships to calves,so all the money must come from the university's limited fund (whose total money is F, 0 <= F <= 2,000,000,000).
Worse still, Moo U only has classrooms for an odd number N ( <= N <= ,) of the C (N <= C <= ,) calves who have applied.Bessie wants to admit exactly N calves in order to maximize educational opportunity. She still wants the median CSAT score of the admitted calves to be as high as possible.
Recall that the median of a set of integers whose size is odd is the middle value when they are sorted. For example, the median of the set {, , , , } is , as there are exactly two values above and exactly two values below it.
Given the score and required financial aid for each calf that applies, the total number of calves to accept, and the total amount of money Bessie has for financial aid, determine the maximum median score Bessie can obtain by carefully admitting an optimal set of calves.
Input
* Line : Three space-separated integers N, C, and F * Lines ..C+: Two space-separated integers per line. The first is the calf's CSAT score; the second integer is the required amount of financial aid the calf needs
Output
* Line : A single integer, the maximum median score that Bessie can achieve. If there is insufficient money to admit N calves,output -.
Sample Input
Sample Output
Hint
Source
题意:奶牛学校招生,c头奶牛报名,要选n头(n为奇数),学校是义务制,所以每头奶牛的学费都由学校负责。每头奶牛都由自己的考试分数和它需要花的学费,学校总共有f的资金,问合法招生方案中中间分数(即排名第(n+1)/2)最高的是多少。
题解:先将所有的奶牛按照分数由高到低排序,假设k是招的奶牛中排名中间的那头,按照排序可知,[1,k-1]中的奶牛必定被招了(n-1)/2头,[k+1,c]中也必定被招了(n-1)/2头,而且无论招的是谁,分数是怎么样,最后影响结果的都只是k的分数。于是,可以预处理dpl[i]代表[1,i]头牛中选出(n-1)/2头牛的最小花费,dpr[i]代表[i,c]头牛中选出(n-1)/2头牛的花费,预处理方法可以用一个大顶堆,复杂度nlogn,最后枚举中间牛复杂度n。
步骤:1、先按牛的分数从大到小排序
2、预处理dpL、dpR数组,dpL表示 1-?的最低花费,dpR表示 ?-c 的最低花费,这里用了优先队列来实现
3、从高到低枚举能作为中位数的牛,看看能否符合,符合直接break
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 101006
#define ll long long
ll n,c,f;
struct Node{
ll sco;
ll cost;
}cow[N];
bool cmp(Node a,Node b){
if(a.sco!=b.sco)
return a.sco>b.sco;
return a.cost<b.cost;
} ll dpL[N],dpR[N]; int main()
{
while(scanf("%I64d%I64d%I64d",&n,&c,&f)==){
for(int i=;i<c;i++){
scanf("%I64d%I64d",&cow[i].sco,&cow[i].cost);
}
sort(cow,cow+c,cmp);
memset(dpL,,sizeof(dpL));
memset(dpR,,sizeof(dpR)); priority_queue<int>q;
ll niu=(n-)/;
ll sum=;
for(int i=;i<niu;i++){
q.push(cow[i].cost);
sum+=cow[i].cost;
}
dpL[niu-]=sum;
for(ll i=niu;i<c;i++){
if(q.top()>cow[i].cost){
sum=sum-q.top()+cow[i].cost;
q.pop();
q.push(cow[i].cost);
dpL[i]=sum;
}
else{
dpL[i]=dpL[i-];
}
} while(!q.empty()){
q.pop();
} sum=;
for(ll i=c-;i>=c-niu;i--){
q.push(cow[i].cost);
sum+=cow[i].cost;
}
dpR[c-niu]=sum;
for(ll i=c-niu-;i>=;i--){
if(q.top()>cow[i].cost){
sum=sum-q.top()+cow[i].cost;
q.pop();
q.push(cow[i].cost);
dpR[i]=sum;
}
else{
dpR[i]=dpR[i+];
}
}
ll flag=;
for(int i=niu;i<c-niu;i++){
if(cow[i].cost+dpL[i-]+dpR[i+]<=f){
printf("%I64d\n",cow[i].sco);
flag=;
break;
}
}
if(flag==){
printf("-1\n");
}
}
return ;
}
poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)的更多相关文章
- POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)
POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...
- poj -2010 Moo University - Financial Aid (优先队列)
http://poj.org/problem?id=2010 "Moo U"大学有一种非常严格的入学考试(CSAT) ,每头小牛都会有一个得分.然而,"Moo U&quo ...
- POJ 2010 Moo University - Financial Aid 优先队列
题意:给你c头牛,并给出每头牛的分数和花费,要求你找出其中n(n为奇数)头牛,并使这n头牛的分数的中位数尽可能大,同时这n头牛的总花费不能超过f,否则输出-1. 思路:首先对n头牛按分数进行排序,然后 ...
- poj 2010 Moo University - Financial Aid
Moo Univ ...
- poj 2010 Moo University - Financial Aid 最大化中位数 二分搜索 以后需要慢慢体会
Moo University - Financial Aid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6599 A ...
- POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆
考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...
- POJ 2010 Moo University - Financial Aid (优先队列)
题意:从C头奶牛中招收N(奇数)头.它们分别得分score_i,需要资助学费aid_i.希望新生所需资助不超过F,同时得分中位数最高.求此中位数. 思路: 先将奶牛排序,考虑每个奶牛作为中位数时,比它 ...
- poj 2010 Moo University - Financial Aid (贪心+线段树)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...
- POJ 2010 Moo University - Financial Aid(堆维护滑窗kth,二分)
按照score排序,贪心,从左到右用堆维护并且记录前面的最小N/2个花费之和. 然后从右向左枚举中位数,维护N/2个数之和加上并判断是否满足条件.(stl的队列没有clear(),只能一个一个pop. ...
随机推荐
- 【winform程序】自定义webrowser控件调用IE的版本
修改注册表: bit: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROW ...
- javadoc简介
Javadoc是Sun公司提供的一个技术,它从程序源代码中抽取类.方法.成员等注释形成一个和源代码配套的API帮助文档.也就是说,只要在编写程序时以一套特定的标签作注释,在程序编写完成后,通过Java ...
- hdu 4605-Magic Ball Game(树状数组)
题目大意: 给你一棵二叉树,每个节点有一个w值,现在有一颗小球,值为x,从根节点往下掉,如果w==x,那么它就会停止:如果w>x,那么它往左.右儿子的概率都是1.2:如果w<x,那么它往左 ...
- [Angular 2] Using Array ...spread to enforce Pipe immutability
Pipes need a new reference or else they will not update their output. In this lesson you will use th ...
- cocos2d-x 3.0 画图节点——Node
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- 论js闭包的重要性
很久没写博客了,今天发现了一个很有意思的问题,写下来分享一下 话不多说,贴前端代码: <script type="text/javascript" src="js/ ...
- 关于winform主题IrisSkin2的编写
第一步:首先引用IrisSkin2.dll. 第二步自定义类: /// <summary> /// 窗体主题边界类 /// </summary> public class Fo ...
- Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml
Android开发:碎片Fragment完全解析 为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像 Activi ...
- RecycleView 瀑布流滑动移位
RecycleView StaggeredLayoutManager(瀑布流)滑动的时候,默认会出现item移动的问题,需以下来个步骤来解决: 附上StaggeredLayoutManager中的一段 ...
- 让 asp.net mvc 支持 带有+ _ 等特殊字符的路由
最近配置微信 业务域名 时,需要在服务器的根目录中上传一个文本文件,而这个文本文件需要放这样的目录中: 于在就在 服务器目录中创建了对应的文件夹,并将kuPv.txt上传,但是访问时,却怎么也访问不到 ...