Moo University - Financial Aid

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 10894 Accepted: 3206

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.


解题心得:

  1. 题意就是n个学生,每个学生有一个分数和去学校读书学校要给的钱,学校希望能够收取c个学生,要求这c个学生的花费总和不超过f并且要求这c个学生的中位数尽量大。
  2. 其实贪心的方法还是很简单,主要就是一个预处理,首先需要按照学生的分数来排序,然后预处理每个位置前面从c/2个学生花费最少是多少,后面的c/2个学生花费最少是多少。就是维护一个优先队列,优先队列之中需要放最小的c/2个最小的数,具体实现方法可以直接看代码。

#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100; struct Student {
ll sco,sp;
bool operator < (const Student &a) const {
return a.sco > sco;
}
}st[maxn]; ll sum_pre[maxn],sum_end[maxn];
ll n,c,f; void get_sum() {
priority_queue <int> qu;
ll sum = 0;
c = c/2; for(int i=n;i>n-c;i--) {
qu.push(st[i].sp);
sum += st[i].sp;
sum_end[i] = sum;
}
for(int i=n-c;i>=1;i--) {
if(st[i].sp <= qu.top()) {
sum -= qu.top() - st[i].sp;
qu.pop();
qu.push(st[i].sp);
}
sum_end[i] = sum;
} sum = 0;
while(!qu.empty())
qu.pop();
for(int i=1;i<=c;i++) {
sum += st[i].sp;
qu.push(st[i].sp);
sum_pre[i] = sum;
}
for(int i=c+1;i<=n;i++) {
if(st[i].sp < qu.top()) {
sum -= qu.top()-st[i].sp;
qu.pop();
qu.push(st[i].sp);
}
sum_pre[i] = sum;
}
} ll get_ans() {
for(int i=n-c;i>c;i--) {
ll sum = sum_pre[i-1] + sum_end[i+1];
sum += st[i].sp;
if(sum <= f)
return st[i].sco;
}
return -1;
} int main() {
scanf("%lld%lld%lld",&c,&n,&f);
for(int i=1;i<=n;i++)
scanf("%lld%lld",&st[i].sco,&st[i].sp);
sort(st+1,st+1+n);
get_sum();
ll ans = get_ans();
printf("%lld",ans);
return 0;
}

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 Univ ...

  3. poj 2010 Moo University - Financial Aid 最大化中位数 二分搜索 以后需要慢慢体会

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

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

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

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

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

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

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

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

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

  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,同时得分中位数最高.求此中位数. 思路: 先将奶牛排序,考虑每个奶牛作为中位数时,比它 ...

  10. POJ 2010 Moo University - Financial Aid treap

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

随机推荐

  1. 设置Tomcat的字符编码

    在 server.xml 中的 Connector 标签在加入 URIEncoding="UTF-8" 属性. <Connector port="8080" ...

  2. thinkphp中怎么判断是手机端访问还是pc端访问?

    function isMobile() { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) r ...

  3. Linux 学习 三, linux 文件结构

    linux 的文件结构 linux 下的bin 目录,包含了常用的命令应用程序 /bin: bin为binary的简写主要放置一些系统的必备执行档例如:cat.cp.dmesg.gzip.kill.l ...

  4. .net 使用Memcached

    1.创建个人MemcachedHelper类 /// <summary> /// 页 面 名:缓存管理类<br/> /// 说 明:设置.获取.移除Cache<br/&g ...

  5. Azure 3 月新公布

    Azure 3 月新发布:Power BI Embedded,R Server 和 IoT 套件预测性维护预配置解决方案正式发布,ExpressRoute 部署变更,以及计量名称变更 Power BI ...

  6. TeamViewer 软件完全卸载

    TeamViewer 软件似乎用于商业环境中 - 彻底卸载 Windows 1. 检测为商业用途该软件似乎用于商业环境中.请注意:免费版仅供个人使用.您的会话将在 5 分钟后终止. 2.1 Close ...

  7. winxp如何开启SNMP服务

    1.先安装SNMP组件 开始——>    控制面板——>添加或删除程序——>添加/删除windows组件——>管理和监视工具(前面方框选择后)——>详细信息——>简 ...

  8. jquery中对于ul>li列表分页。学习记录

    这个是很简单的一种分页,只能对列表进行分页.为了开发有可能需要用到记录下来 Html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...

  9. 如何将windows日志转成syslog格式并发到远程sysylog服务器

      安装Snare, 随便找了个版本下载下来,安装一路next,除了中间让你输入一次http的管理登录口令.   2,配置 之后打开URL:http://192.168.37.23:6161/,输入默 ...

  10. AttributeError: module 'requests' has no attribute 'get' 遇到了这个错误,是因为我把python关键字做了包名。。。

    初学者总会犯各种低级错误,这是其一,特此记录.