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. ...
随机推荐
- TCP/IP具体解释--三次握手和四次握手 Dos攻击
TCP连接的状态图 TCP建立连接的三次握手过程,以及关闭连接的四次握手过程 贴一个telnet建立连接,断开连接的使用wireshark捕获的packet截图. 1.建立连接协议(三次握手) (1) ...
- gcc和g++编译c或者c++文件碰到的问题
gcc和g++都是GNU(组织)的一个编译器. 误区一:gcc只能编译c代码,g++只能编译c++代码 两者都可以,但是请注意: 1.后缀为.c的,gcc把它当作是C ...
- Dynamics CRM记录页面上隐藏子网格“+”标识
前段时间微软发布了Dynamics 365,这是Dynamics产品的又一次大的变动,期待新的版本能够更好的满足客户的需求,同时提供更多的可定制化的内容. 近期做Dynamics CRM项目遇到很多审 ...
- Android(java)学习笔记262:JNI之工具快速开发步骤
下面通过一个案例说明一下,利用工具jni快速开发步骤 1.新建一个Android工程,命名为"03_对int数组加1",如下: 2. 在MainActivity.java中对add ...
- http://msh.baidu.com/UTWpR6wY4722
超人的计算机专业应届研究生个人简历,但企业不需要 前几天和一位做人力资源的朋友在饭店里面喝酒,聊起来大学生找工作不好找的话题.我的这个朋友对这个还真比较感兴趣,说着说着从公文包里拿出来一份简历递给我看 ...
- Linux命令之文件与用户权限
1.文件管理 在Linux里,任何软件和I/O设备都被视为文件.Linux中的文件名最大支持256个字符,分别可以用A-Z.a-z.0-9等字符来命名. 和Windows不同,Linux中文件是区分大 ...
- (四)《Java编程思想》——可变参数列表
以object数组为参数的方法实现可变参数列表 package chapter5; /** * 以object数组为参数的方法实现可变参数列表 */ class A { } public class ...
- HQL查询
HQL ,Hibernate Query Language ,是Hibernate查询语言,不直接操作数据表,而是操作实体类,根据实体类和对应数据表中的映射关系,查找数据. 下面是hql的基本步骤: ...
- Oracle ACL (Access Control List)详解
在Oracle11g中,Oracle在安全方面有了很多的改进,而在网络权限控制方面,也有一个新的概念提出来,叫做ACL(Access Control List), 这是一种细粒度的权限控制.在ACL之 ...
- 验证码 Demo
//设置响应头 response.setCharacterEncoding("image/jpeg"); int width=160; int height=40; Buffere ...