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

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. 

Source

 
两种方法
第一是二分答案,首先把生成两个分别按score排序和按aid排序的数组,定义left为中位数左边可加的数的个数,right为右边可加的数的个数
若left < n / 2则中位数只能调高,若right < n / 2 中位数只能调低,否则满足条件则应把中位数调高,按此规则二分即可
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define maxn 100005 struct node {
int id,aid,sco;
}; int n,c,f;
node calve[maxn],s[maxn];
int sma[maxn],big[maxn]; bool cmp1(node a,node b) {
return a.aid < b.aid;
} bool cmp2(node a,node b) {
return a.sco < b.sco;
} int check(int x) {
int sum = s[x].aid,left = ,right = ; for(int i = ; i <= c; ++i) {
if(calve[i].id < x && sum + calve[i].aid <= f && left < n / ) {
sum += calve[i].aid;
++left;
} else if(calve[i].id > x && sum + calve[i].aid <= f && right < n / ) {
sum += calve[i].aid;
++right;
}
} if(left < n / ) return ;
if(right < n / ) return ;
return ; } void solve() {
int l = ,r = c; //for(int i = 1; i <= c; ++i) printf("%d ",s[i].sco);
while(l < r) {
int mid = (l + r + ) >> ;
if(check(mid) == ) {
r = mid - ;
} else if(check(mid) == ) {
l = mid + ;
} else {
l = mid;
}
} printf("%d\n",s[l].sco); } int main()
{ // freopen("sw.in","r",stdin); scanf("%d%d%d",&n,&c,&f); for(int i = ; i <= c; ++i) {
scanf("%d%d",&s[i].sco,&s[i].aid);
} sort(s + ,s + c + ,cmp2); for(int i = ; i <= c; ++i) {
calve[i].id = s[i].id = i;
calve[i].sco = s[i].sco;
calve[i].aid = s[i].aid;
} sort(calve + ,calve + + c,cmp1); int sum = ;
for(int i = ; i <= n; ++i) {
sum += calve[i].aid;
} if(sum > f) {
printf("-1\n");
} else {
solve();
} return ;
}

第二种方法是用大根堆,首先按照score从小到大排序。用大根堆预处理数组dpl[i]代表从1  到 i 能得到的数量为 n / 2的最小的aid ,dpr[i]代表 从i 到 c能得到的数量为 n / 2的最小的aid,具体方法是,若堆的大小小于 n / 2,则不断入列,否则进列,并删除堆顶。

则从大到小找出能满足条件的最大的中位数。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; #define maxn 100005 struct node {
int sco,aid;
}; int n,c,f;
node s[maxn];
int dpl[maxn],dpr[maxn]; bool cmp(node a,node b) {
return a.sco < b.sco;
} bool cmp2(node a,node b) {
return a.aid < b.aid;
} void solve() {
priority_queue<int> q;
int sum = ;
for(int i = ; i <= c; ++i) {
if(q.size() < (n / )) {
sum += s[i].aid;
q.push(s[i].aid);
} else {
q.push(s[i].aid);
sum += s[i].aid;
sum -= q.top();
q.pop(); }
dpl[i] = sum;
} while(!q.empty()) q.pop();
sum = ;
for(int i = c; i >= ; --i) {
if(q.size() < (n / )) {
sum += s[i].aid;
q.push(s[i].aid);
} else {
q.push(s[i].aid);
sum += s[i].aid;
sum -= q.top();
q.pop(); }
dpr[i] = sum;
} int ans;
for(int i = c; i >= ; --i) {
if(i - < n / || c - i < n / ) continue;
if(dpl[i - ] + dpr[i + ] + s[i].aid <= f) {
ans = s[i].sco;
break;
}
} printf("%d\n",ans); } int main() {
//freopen("sw.in","r",stdin); scanf("%d%d%d",&n,&c,&f); for(int i = ; i <= c; ++i) {
scanf("%d%d",&s[i].sco,&s[i].aid);
} sort(s + ,s + c + ,cmp2);
int sum = ;
for(int i = ; i <= n; ++i) sum += s[i].aid;
if(sum > f) {
printf("-1\n");
return ;
} sort(s + ,s + c + ,cmp); solve(); return ; }

POJ 2010的更多相关文章

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

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

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

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

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

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

  4. 【POJ 2010 Moo University-Financial Aid】优先级队列

    题目链接:http://poj.org/problem?id=2010 题意:C只牛犊,各有自己的分数score和申请的补助aid,现要选出N只(N为奇数),使得其aid的总和不超过F,且按score ...

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

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

  6. Moo University - Financial Aid POJ 2010 优先队列(最大堆)

    题目:http://poj.org/problem?id=2010 题目大意: 奶牛上大学.因为经济问题,每头奶牛都需要一定的补助需求,学校会提供一定的资金用于补助 每头牛都有自己的分数,学校招收的名 ...

  7. POJ 2010 Moo University - Financial Aid treap

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

  8. 堆 poj 2010

    选n个人从c个中 花费不超过f c个人的成绩和花费 求分数中位数最大 n是奇数 显然中位数是n/2+1 ~c-n/2之间的(假如存在的话) 用大顶堆维护前n/2个小的花费 求出以这个人为中位数的花费 ...

  9. Divide and conquer:Moo University - Financial Aid(POJ 2010)

    Moo University - Financial Aid 其实是老题了http://www.cnblogs.com/Philip-Tell-Truth/p/4926008.html 这一次我们换二 ...

  10. Heap:Moo University - Financial Aid(POJ 2010)

       牛的学校 题目大意:这只Bessie真是太顽皮了,她又搞了个学校,准备招生,准备通过一个考试筛选考生,但是不能招到每个学生,每个学生也不能一定能上学,要资助,问你在一定资金内,怎么收学生,使收到 ...

随机推荐

  1. hdu 1237 简单计算器

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1237 简单计算器 Description 读入一个只包含 +, -, *, / 的非负整数计算表达式, ...

  2. Oracle 11gR2 Database和Active Data Guard迁移案例

    客户一套核心系统由一台Oracle Database 11.2.0.3.4单机和一台Active Data Guard组成,分别运行在两台PC服务器上,Oracle Linux 5.8 x86_64b ...

  3. Html5最简单的游戏Demo——Canvas绘图的弹弹球

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  4. sql总结

    sql总结 sql总结 where字句中使用的运算符 定义外键 定义主键 多表联合查询 统计函数 数据类型 sql语句格式 转换函数 null函数 运算符 日期 求某天是星期几 日期天数差 next_ ...

  5. html标记列表应用

    一.[ul]无序列表 1.无序列表====== 二.[ol]有序列表 1.有序列表用于段落有序的排列, <ol> <li>内容</li> </ol> 三 ...

  6. bower解决js的依赖管理

    bower解决js的依赖管理 前言: 一个新的web项目开始,我们总是很自然地去下载需要用到的js类库文件,比如jQuery,去官网下载名为jquery-1.10.2.min.js文件,放到我们的项目 ...

  7. ListView单击单元格 产生其他控件

    以combobox为例. 假如一行里面只有一个combobox. //在类中声明一个控件数组 private ComboBox[] cmds = null; //initview中调用dao组件获得显 ...

  8. 一些 Shell 脚本(持续更新)

    1. 启动日志分析 启动日志格式如下: 开机时间:2015/05/13 周三 16:45:17.79 关机时间:2015/05/13 周三 18:46:03.91 开机时间:2015/05/14 周四 ...

  9. 3、颜色的字符串、十进制、十六进制相互转换(color convert between dec、hex and string )

    int color_int=***; 1.(十进制整数)转换成(十六进制的字符串) String color_hex = String.format("#%06X", (0xFFF ...

  10. How to Build FFmpeg for Android

    http://www.roman10.net/how-to-build-ffmpeg-for-android/ ffmpeg is an open-source platform for record ...