POJ 2010
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 4235 | Accepted: 1293 |
Description
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
* 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
Sample Input
3 5 70
30 25
50 21
20 20
5 18
35 30
Sample Output
35
Hint
Source
#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的更多相关文章
- POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆
考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...
- POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)
POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...
- poj -2010 Moo University - Financial Aid (优先队列)
http://poj.org/problem?id=2010 "Moo U"大学有一种非常严格的入学考试(CSAT) ,每头小牛都会有一个得分.然而,"Moo U&quo ...
- 【POJ 2010 Moo University-Financial Aid】优先级队列
题目链接:http://poj.org/problem?id=2010 题意:C只牛犊,各有自己的分数score和申请的补助aid,现要选出N只(N为奇数),使得其aid的总和不超过F,且按score ...
- poj 2010 Moo University - Financial Aid (贪心+线段树)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...
- Moo University - Financial Aid POJ 2010 优先队列(最大堆)
题目:http://poj.org/problem?id=2010 题目大意: 奶牛上大学.因为经济问题,每头奶牛都需要一定的补助需求,学校会提供一定的资金用于补助 每头牛都有自己的分数,学校招收的名 ...
- POJ 2010 Moo University - Financial Aid treap
按第一关键字排序后枚举中位数,就变成了判断“左边前K小的和 + 这个中位数 + 右边前K小的和 <= F",其中维护前K小和可以用treap做到. #include <cstdi ...
- 堆 poj 2010
选n个人从c个中 花费不超过f c个人的成绩和花费 求分数中位数最大 n是奇数 显然中位数是n/2+1 ~c-n/2之间的(假如存在的话) 用大顶堆维护前n/2个小的花费 求出以这个人为中位数的花费 ...
- Divide and conquer:Moo University - Financial Aid(POJ 2010)
Moo University - Financial Aid 其实是老题了http://www.cnblogs.com/Philip-Tell-Truth/p/4926008.html 这一次我们换二 ...
- Heap:Moo University - Financial Aid(POJ 2010)
牛的学校 题目大意:这只Bessie真是太顽皮了,她又搞了个学校,准备招生,准备通过一个考试筛选考生,但是不能招到每个学生,每个学生也不能一定能上学,要资助,问你在一定资金内,怎么收学生,使收到 ...
随机推荐
- uva 11186 Circum Triangle<叉积>
链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 在Windows下使用BAT调度存储在资源库中的KTR
描述: 在Windows下使用BAT调度存储在资源库中的KTR 准备环境: 1.ktr文件(该KTR必须是存储在资源管库中的) 2.bat文件 @echo off D: cd D:\software\ ...
- oracle 日志文件管理
OS: [root@b28-122 ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 DB: SQL> select * f ...
- azure 云服务证书下载方式
打开地址自动下载证书,vs中项目-右键-发布-导入证书. https://manage.windowsazure.cn/publishsettings/index 在 Visual Studio 中打 ...
- Redis 客户端配置及示例
一.redis自定义配置节点 <configSections> <section name ="RedisConfig" type="Amy.Toolk ...
- [quote ]ffmpeg, gstreamer, Raspberry Pi, Windows Desktop streaming
[quote ]ffmpeg, gstreamer, Raspberry Pi, Windows Desktop streaming http://blog.pi3g.com/2013/08/ffmp ...
- 修改一行代码提升 Postgres 性能 100 倍
http://www.datadoghq.com/2013/08/100x-faster-postgres-performance-by-changing-1-line/ SELECT c.key, ...
- SFI(段文件描述符存在的理由与意义)
SFI:short file identifier,短文件描述符 SFI的取值必须在0001-001E之间,即0-30 SFI存在的理由:有些COS命令可以不需要事先选择文件而直接通过SFI快速访问文 ...
- 使用JavaScript+Html创建win8应用(二)
向我们的应用中添加JavaScript 的 Windows 库控件,首先我们接着上一个demo把一个评分控件添加进来 与 HTML 控件不同的是,适用于 JavaScript 的 Windows 库控 ...
- js判断浏览器滚动条是否拉到底
$(window).scroll(function(){ // 当滚动到最底部以上n像素时, 加载新内容 if ($(document).height() - $(this).scrollTop() ...