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. ACdream 1417 Numbers

    pid=1417">题目链接~~> 做题感悟:比赛的时候用的广搜,然后高高兴兴的写完果断TLE .做题的时候不管什么题都要用笔画一下,模拟几组数据,这样或许就AC了(做题经验,有 ...

  2. c++ 连接两个字符串实现代码 实现类似strcat功能(转)

    想实现strcat功能,直接网上找一个. 第一种: #include "stdafx.h" #include<iostream> using namespace std ...

  3. Linux内核ROP学习

    0x00 前言 1.SMEP(Supervisor Mode Execution Protection):一种减缓内核利用的cpu策略,禁止内核态到用户态内存页的代码执行(32位的addresses ...

  4. Python顺序与range和random

    range([start,] stop[, step]) start是开始,stop是停下,step是步长. >>> range(10) range(0, 10) >>& ...

  5. box-shadow全面解析

    一.box-shadow语法: box-shadow: none | inset(可选值,不设置,为外投影,设置,为内投影) x-offset(阴影水平偏移量,正方向为right) y-offset( ...

  6. oracle数据库实验讲义-读书笔记(一)

    1.激活锁定的用户alter user scott account unlock identified by tiger;2.使用内含脚本建立scott用户@%oracle_home%\rdbms\a ...

  7. 动态代理写connection连接池Demo

    public class JdbcUtil2 { //声明连接池<放到LinkedList中,操作其中对象的速度快 只需要改变连接> private static LinkedList&l ...

  8. js基本类型

    1.undefined 1)var a;//没有赋值的时候就是undefined 2)undefined派生自null,alert(undefined==null)//true 虽然这上条语句是一样, ...

  9. HTML5画布(图像)

    案例1: <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8& ...

  10. 求Sn=1!+2!+3!+4!+5!+…+n!之值,其中n是一个数字

    #include <iostream> using namespace std; int main() {long long s,n,i,j,p;s=0; cin>>n;    ...