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

Sample output:If Bessie accepts the calves with CSAT scores of 5, 35, and 50, the median is 35. The total financial aid required is 18 + 30 + 21 = 69 <= 70. 

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(优先队列(最小堆)+ 贪心 + 枚举)的更多相关文章

  1. POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)

    POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...

  2. poj -2010 Moo University - Financial Aid (优先队列)

    http://poj.org/problem?id=2010 "Moo U"大学有一种非常严格的入学考试(CSAT) ,每头小牛都会有一个得分.然而,"Moo U&quo ...

  3. POJ 2010 Moo University - Financial Aid 优先队列

    题意:给你c头牛,并给出每头牛的分数和花费,要求你找出其中n(n为奇数)头牛,并使这n头牛的分数的中位数尽可能大,同时这n头牛的总花费不能超过f,否则输出-1. 思路:首先对n头牛按分数进行排序,然后 ...

  4. poj 2010 Moo University - Financial Aid

                                                                                                Moo Univ ...

  5. poj 2010 Moo University - Financial Aid 最大化中位数 二分搜索 以后需要慢慢体会

    Moo University - Financial Aid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6599   A ...

  6. POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆

    考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...

  7. POJ 2010 Moo University - Financial Aid (优先队列)

    题意:从C头奶牛中招收N(奇数)头.它们分别得分score_i,需要资助学费aid_i.希望新生所需资助不超过F,同时得分中位数最高.求此中位数. 思路: 先将奶牛排序,考虑每个奶牛作为中位数时,比它 ...

  8. poj 2010 Moo University - Financial Aid (贪心+线段树)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...

  9. POJ 2010 Moo University - Financial Aid(堆维护滑窗kth,二分)

    按照score排序,贪心,从左到右用堆维护并且记录前面的最小N/2个花费之和. 然后从右向左枚举中位数,维护N/2个数之和加上并判断是否满足条件.(stl的队列没有clear(),只能一个一个pop. ...

随机推荐

  1. google login page

    </pre><pre name="code" class="html"><div class="container&qu ...

  2. LIB文件和DLL文件的作用

    (1)lib是编译时需要的,dll是运行时需要的.如果要完成源代码的编译,有lib就够了.如果也使动态连接的程序运行起来,有dll就够了.在开发和调试阶段,当然最好都有.(2)一般的动态库程序有lib ...

  3. IntelliJ IDEA 的Project structure说明

    IntelliJ IDEA 的Project structure可以在File->Project structure中打开,同时,在新建项目是IDE一般用向导的方式让你填写Project str ...

  4. JS跨域请求之JSONP

    在项目开发中遇到跨域的问题,一般都是通过JSONP来解决的.但是JSONP到底是个什么东西呢,实现的原理又是什么呢.在项目的空闲时间可以好好的来研究一下了. JSONP的产生 1.众所周知,Ajax请 ...

  5. Android --------- 命名规范

    工程 软件名称,最好是英文首字母大写:如MobileSafe. 包 企业单位网址的倒序+软件名称:如com.baidu.mobilesafe. 类 类中分为:(头字母小写,其他每个单子首字母大写) 1 ...

  6. partial修饰符,可以让同类命名空间下出现重名

    public partial class Person { } public partial class Person { } partial修饰符,可以让同类命名空间下出现重名,两个类其实是一个类, ...

  7. test在博客中嵌入实例代码

        // 其实很简单,只要把css.div.js代码直接写在里面就可以了.通过查看源代码就能看得很清楚了. 要注意的一点就是:如果div里没有任何内容,则它会被(博客网)删除掉,所以即使没有内容, ...

  8. dll signing issue

    1. Verify if a dll has been signed sn.exe -v module.dll Scenario: sometimes for security reasons, a ...

  9. 错误日志类C#

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  10. iOS知识点全梳理-备用

    感谢大神分享 文/Jack_lin(简书作者)原文链接:http://www.jianshu.com/p/5d2163640e26著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 序言 ...