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. WinForm 加载自定义控件闪烁问题

    WinForm加载多个自定义控件时,会出现很严重的闪烁问题,很卡,一块一块的加载(像打开网页时,网络很卡的那种感觉)简直没法忍受. 在网上搜索了好久,网上大部分的方法是一下4种,但是都不能有效的解决问 ...

  2. Java之NIO传输数据

    NIO可谓陈词旧调,不值一提. 但之前都是泛泛而谈, 现在深入应用才知道秘诀所在. 对于SocketChannel有read()与write(),但由于"非阻塞IO"本质, 这二个 ...

  3. RCF

    1. RCF: 纯c++的RPC, 不引入IDL, 大量用到boost,比较强大.2. casocklib:  protobuf + asio 较完善实现3. eventrpc: protobuf + ...

  4. iOS学习之UI可视化编程-StoryBoard

    一.StoryBoard与xib 对比: 相同点:都属于IB编程的方式,可以快速构建GUI. 不同点:xib侧重于单文件(单独的控制器或者视图)编辑,storyboard侧重于多页面关联.storyb ...

  5. iOS学习之UITableView编辑

    一.UITableView编辑 UITableView编辑(删除.添加)步骤: 让TableView处于编辑状态. 协议设定:1)确定Cell是否处于编辑状态:2)设定cell的编辑样式(删除.添加) ...

  6. jsapi支付,提示redirect_uri 参数错误

    检查授权目录(微信支付——配置中心) appid MCHID KEYS 配置参数是否正确 appsecrect 配置是否正确(开发者中心) 如果是使用测试链接,需要同时指定测试授权目录,测试账号,并且 ...

  7. Spark 3000门徒第一课随笔

    昨晚听了王家林老师的Spark 3000门徒系列课程的第一课,把scala基础过了一遍,对ArrayBuffer有了新的认识: Array本身创建后不可修改ArrayBuffer可修改import s ...

  8. Centos 安装 p7zip,即Linux下的7z

    Centos 无法直接通过yum安装7z,我们一般通过repoforge,rpmforge的软件包进行安装,你只需要下载一个对应的包,直接安装就可以 p7zip-9.20.1-1.el4.rf.i38 ...

  9. ABAP文本编辑框操作

    * 1.创建文本框 DATA: g_container TYPE REF TO cl_gui_custom_container, g_editor TYPE REF TO cl_gui_textedi ...

  10. UITableView设置cell为不可选?

    本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术.本文将为读者讲解UITableView如何设置单 ...