Moo University - Financial Aid
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7961   Accepted: 2321

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 1..2,000,000,000.

Moo U is very expensive to attend; not all calves can afford it.In fact, most calves need some sort of financial aid (0 <= aid <=100,000). 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 (1 <= N <= 19,999) of the C (N <= C <= 100,000) 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 {3, 8, 9, 7, 5} is 7, as there are exactly two values above 7 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 1: Three space-separated integers N, C, and F

* Lines 2..C+1: 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 1: A single integer, the maximum median score that Bessie can achieve. If there is insufficient money to admit N calves,output -1. 

Sample Input

3 5 70
30 25
50 21
20 20
5 18
35 30

Sample Output

35

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. 
 
题意:有C头牛想要进大学,但只能从中挑选出N头牛进大学,并且给这N头牛的补助资金总和不能超过F,在满足此条件的情况下,求挑选出来的N头牛中成绩最中间的牛的成绩最高是多少。
思路:可以先将C头牛按成绩从低到高排序,之后遍历每头牛,对于每头牛,挑选出比当前牛成绩低的(N/2)头牛,使得这些牛需要资金的总和达到最小值并记录此最小值,再次遍历求成绩比当前牛成绩高的牛中挑选出来的(N/2)牛需要资金总和的最小值,最后取和与F作比较即可,使得满足条件的那头牛的成绩越高越好。
AC代码:

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<queue>
#include<functional>
using namespace std;
const int N_MAX = +;
struct cow {
int score;
int need_money;
bool operator<(const cow&b)const{
return score<b.score ;
}
};
cow cows[N_MAX];
int lower_money[N_MAX];
int upper_money[N_MAX];
int main() {
//cout << 0x3f3f3f3f <<" "<<INT_MAX<<endl;
int N, C, F;
scanf("%d%d%d",&N,&C,&F);
for (int i = ;i < C;i++)
scanf("%d%d",&cows[i].score,&cows[i].need_money);
sort(cows,cows+C);//按成绩从低到高排
priority_queue<int>que;
unsigned int half = N / ,total=;
for (int i = ;i < C;i++) {//对于每一头牛,计算成绩比他低的牛的资金总和的最小值
lower_money[i] = ( que.size() == half ? total : 0x3f3f3f3f);
que.push(cows[i].need_money);
total += cows[i].need_money;
if (que.size() > half) {
total -= que.top();
que.pop();
}
} priority_queue<int>q;
total = ;
for (int i = C - ;i >= ;i--) {
upper_money[i] = (q.size() == half ? total : 0x3f3f3f3f);
q.push(cows[i].need_money);
total += cows[i].need_money;
if (q.size() > half) {
total -= q.top();
q.pop();
}
}
int grade=-;
for (int i = C-;i>=;i--) {
if (upper_money[i] + lower_money[i] + cows[i].need_money <= F) { grade = cows[i].score; break; }
}
if (grade>)cout << grade << endl;
else cout << grade << endl;
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 最大化中位数 二分搜索 以后需要慢慢体会

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

  3. poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)

    Description Bessie noted that although humans have many universities they can attend, cows have none ...

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

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

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

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

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

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

  7. POJ 2010 Moo University - Financial Aid treap

    按第一关键字排序后枚举中位数,就变成了判断“左边前K小的和 + 这个中位数 + 右边前K小的和 <= F",其中维护前K小和可以用treap做到. #include <cstdi ...

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

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

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

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

随机推荐

  1. 0c-38-ARC快速入门

    2.ARC快速使用 int main(){ Student *s = [[Student alloc] init]; return 0; } 只需要写一行代码,编译器会在合适的位置释放学生对象,程序员 ...

  2. oc-30-堆栈

    /** 操作引用计数器的方式:每个对象内部(对象的堆内存里面)都有一个引用计数器变量,表示对象被引用的次数. 1:retainCount:获得对象的引用计数器的值 2:retain:能够让对象的计数器 ...

  3. FlatUI博皮制作

    Bootstrap3出来了,抛开内部框架结构和css命名的变化,bs3最大的改变莫过于扁平化. 扁平化UI中的典范,除了Metro,就是Flat了.目前本人FlatUI中毒中,于是开始慢慢的开始将博皮 ...

  4. 利用yum工具安装应用程序

    在安装gtk+编译环境的过程中,你会发现,RPM软件包之间的依赖关系非常复杂.在实际管理过程中,这种依赖关系可能会更加复杂.因此非常有必要寻找一种自动化安装工具,让安装工具自己处理这些关系复杂的依赖关 ...

  5. (转)ASP.NET Identity登录原理 - Claims-based认证和OWIN

    在Membership系列的最后一篇引入了ASP.NET  Identity,看到大家对它还是挺感兴趣的,于是来一篇详解登录原理的文章.本文会涉及到Claims-based(基于声明)的认证,我们会详 ...

  6. c#给用户控件添加事件处理程序

    1.首先在usercontrol后台添加如下代码: public partial class MyControl: UserControl { //添加事件代理       public event ...

  7. mysql基本定义--数据类型

    浮点数类型与定点数类型: MySQL中使用浮点数类型和定点数类型来表示小数. 浮点数类型包括单精度浮点数(float型)和双精度浮点数(double型).定点数类型就是decimal型. OK,现在我 ...

  8. 小白日记49:kali渗透测试之Web渗透-XSS(三)-存储型XSS、DOM型XSS、神器BEFF

    存储型XSS与DOM型XSS [XSS原理] 存储型XSS 1.可长期存储于服务器端 2.每次用户访问都会被执行js脚本,攻击者只需侦听指定端口 #攻击利用方法大体等于反射型xss利用 ##多出现在留 ...

  9. WPF 之 文本框及密码框添加水印效果

    1.文本框添加水印效果 文本框水印相对简单,不需要重写模板,仅仅需要一个 VisualBrush 和触发器验证一下Text是否为空即可. <TextBox Name="txtSerac ...

  10. Apache 日志分析(一)

    日志格式: 101.38.166.177 – – [10/Jun/2016:14:19:19 +0800] “POST /wp-admin/admin-ajax.php HTTP/1.1” 200 1 ...