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. mysql外键设置(待测试)

    外键的定义语法:[CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)    REFERENCES tbl_name (index_col ...

  2. 使用Linux调用资源库中的Job报错-ERROR: No repository provided, can't load job.

    使用kettle调用资源库中的作业或者是转换,需要注意一下两个问题: 问题一:(-rep后不需要IP)标准shell代码如下 #!/bin/bash export JAVA_HOME=/usr/lib ...

  3. memcached 高级机制(二)

    memcached删除机制 a) (1)有内存机制里说明了,这里会运用到LRU删除机制.我们知道,当我们在add或set一个值时,我们会设置这个值得期限.当某个值过期后,这个值并没有从内存中删除,我们 ...

  4. 关于android WebViewClient的方法解释

    1.public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true ...

  5. boa介绍文档

    http://wenku.baidu.com/view/873aa903cc175527072208ce.html?re=view

  6. How to write a windows service

    how to write a windows services susport microsoft This aritcle describe the detail step to setup a w ...

  7. 技术揭秘12306改造(一):尖峰日PV值297亿下可每秒出票1032张

    [编者按]12306网站曾被认为是"全球最忙碌的网站",在应对高并发访问处理方面,曾备受网民诟病. 2015年铁路客票春运购票高峰期已过,并且12306网站今年没"瘫痪& ...

  8. C++中数组求偏移量计算公式

    已知数组:type A[10][5]A[0][0] --A[8][4]面试常考:数组定义A[0....x][0...y]已知A[m][n] --求A[k][l]的地址:    &A[m][n] ...

  9. 20145120 《Java程序设计》实验三实验报告

    20145120 <Java程序设计>实验三实验报告 实验名称:敏捷开发与XP实践 实验目的与要求: XP基础 XP核心实践 相关工具 (一)敏捷开发与XP 极限编程(eXtreme Pr ...

  10. 我存在,你深深的循环里--从反射看JSON死循环

      JSON有一个非常经典的问题:JSONException: There is a cycle in the hierarchy!俗称死循环.解决这个问题至少有三种以上的办法,总之一句话就是过滤.今 ...