博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)
阅读量:7046 次
发布时间:2019-06-28

本文共 3560 字,大约阅读时间需要 11 分钟。

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 7030 2550 2120 205 1835 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

 

题意:奶牛学校招生,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

1 #include
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 #define N 101006 8 #define ll long long 9 ll n,c,f;10 struct Node{11 ll sco;12 ll cost;13 }cow[N];14 bool cmp(Node a,Node b){15 if(a.sco!=b.sco)16 return a.sco>b.sco;17 return a.cost
q;34 ll niu=(n-1)/2;35 ll sum=0;36 for(int i=0;i
cow[i].cost){43 sum=sum-q.top()+cow[i].cost;44 q.pop();45 q.push(cow[i].cost);46 dpL[i]=sum;47 }48 else{49 dpL[i]=dpL[i-1];50 }51 }52 53 while(!q.empty()){54 q.pop();55 }56 57 sum=0;58 for(ll i=c-1;i>=c-niu;i--){59 q.push(cow[i].cost);60 sum+=cow[i].cost;61 }62 dpR[c-niu]=sum;63 for(ll i=c-niu-1;i>=0;i--){64 if(q.top()>cow[i].cost){65 sum=sum-q.top()+cow[i].cost;66 q.pop();67 q.push(cow[i].cost);68 dpR[i]=sum;69 }70 else{71 dpR[i]=dpR[i+1];72 }73 }74 ll flag=0;75 for(int i=niu;i
View Code

 

转载地址:http://tnzol.baihongyu.com/

你可能感兴趣的文章
独孤九剑(0x04) - 测试篇
查看>>
JS学习总结
查看>>
一个前端写的php博客系统--支持markdown哦
查看>>
Laravel Taggable 为你的模型添加打标签功能
查看>>
Erlang/Elixir: 使用 Edeliver 进行自动化的持续部署
查看>>
Nodejs检测端口是否被占用
查看>>
webpack入坑之旅(二)loader入门
查看>>
如何优雅高效地插入百度广告
查看>>
DbUtils应用在Android6.0中为什么会崩溃
查看>>
OpenJDK9 Hotspot Mac OSX 编译和调试
查看>>
守护进程, 孤儿进程, 僵尸进程与waitpid
查看>>
Django 安全策略的 7 条总结!
查看>>
Android:我为何要封装DialogFragment?
查看>>
表单知识总结
查看>>
Xcode插件实效解决办法
查看>>
[Leetcode] Permutation Sequence 全排列序列
查看>>
多用户协同写作博客系统 Simple-Log v1.7 build20190306
查看>>
SVProgressHUD--比MBProgressHUD更好用的 iOS进度提示组件
查看>>
Android DexIndexOverflow错误解析和解决方案
查看>>
关于BFC的一些应用
查看>>