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. Android SDK Manager 无法更新SDK

    Android SDK Manager 被墙后无法更新SDK 下载sdk时抛出错误:Failed to fetch URL http://dl-ssl.google.com/ 參考例如以下博客: ht ...

  2. Java可见性机制的原理

    基本概念 可见性 当一个线程修改了共享变量时,另一个线程可以读取到这个修改后的值. 内存屏障(Memory Barriers) 处理器的一组指令,用于实现对内存操作的顺序限制. 缓冲行 CPU告诉缓存 ...

  3. Python-xml解析常用方法简介

    [XML几种解析方法] 常见的XML编程接口有DOM和SAX,这两种接口处理XML文件的方式不同,使用场合自然也就不同. Python有三种方法解析XML: SAX,DOM,以及ElementTree ...

  4. $(document).ready(); $().ready(); $()

    $(document).ready(function(){}); $().ready(function(){}); $(function(){}), 三者效果是一样的,在文档加载完成之后执行()中的代 ...

  5. jquery获取checkbox被选中的值

    只用一个循环,就可以找出被选中的checkbox的值 var s; $("[name = b]:checkbox").each(function () {              ...

  6. java优势

    跨平台,只要有JVM(Java虚拟机)的操作系统就可以运行JAVA程序: 安全: 弃用危险的指针: 自动内在管理机制,垃圾回收机制,由垃圾回收器回收,减轻程序负担,也避免了手动回收的危险性: 完全面向 ...

  7. poj3254状压DP入门

    G - 状压dp Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit ...

  8. c++中static的使用

    static可以用来修饰变量,包括函数的局部变量,类的成员变量.可以用来修饰函数,包括类的成员函数,普通函数. 今天就只说说static修饰类之外的函数的情况.假设你写了一个head.h,一个a.cp ...

  9. C++ Primer 5th 第4章 表达式

    表达式是运算对象及运算符组成的式子,表达式求值将得到一个结果,单独的变量或者字面值也算表达式,结果是其本身. 运算符分为:一元运算符.二元运算符.三元运算符.一元即一个作用对象,二元两个作用对象,以此 ...

  10. DEDE数据库修改后台变量

    进行数据库之后找到 dede_sysconfig 这个数据表,然后查找到你要删除的dede教程变量名称. 这样就可以了