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

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

题意:给你c头牛,每买一头牛都有一定的花费,要求从中选出n头牛,使得在总花费<=f的情况下使得中位数最大;
错因分析:一开始就陷入了二分的固定思维,下面第一份的自己的WA代码
#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
struct Node{
int s,m,id;
}node[20005];
int n,c,f,k;
bool cmp(Node a,Node b)
{
return a.m<b.m;
}
int ok(int mid)
{
int cnt1=0,cnt2=0,total=0;
for(int i=1;i<=c;i++)
if(node[i].s<mid&&cnt1<k)
{
cnt1++;
total+=node[i].m;
} //在加进一头牛时没有判断假设加进后总费用是否<=f;
else if(node[i].s>mid&&cnt2<(n-k))
{
cnt2++;
total+=node[i].m;
}
if(cnt1<k||cnt2<(n-k)||total>f)
return 0;
else return 1;
}
int main()
{
while(~scanf("%d %d %d",&n,&c,&f))
{
int maxn=0;
for(int i=1;i<=c;i++)
{
scanf("%d %d",&node[i].s,&node[i].m);
if(maxn<node[i].s)
maxn=node[i].s;
}
sort(node+1,node+1+c,cmp);
int l=0,r=maxn+1;
k=(n+1)/2;
while(r-l>1)
{
int mid=(l+r)/2;
if(ok(mid))
r=mid;
else
l=mid;
}
printf("%d\n",r-1);
}
return 0;
}

  第二份是AC代码:总的思想是先进行score排序,记录好数据后,二分枚举可能的取到的中位(因为score已经排好序,所以尽量往id大的取),然后money排序,进行贪心的选取。

#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
struct Node{
int s,m,id;
}node[100005];
int n,c,f,k,score[100005],weight[100005];
bool cmpm(Node a,Node b)
{
return a.m<b.m;
}
bool cmps(Node a,Node b)
{
return a.s<b.s;
}
int main()
{
while(~scanf("%d %d %d",&n,&c,&f))
{
for(int i=1;i<=c;i++)
scanf("%d %d",&node[i].s,&node[i].m);
sort(node+1,node+1+c,cmps);
for(int i=1;i<=c;i++)
{
node[i].id=i;
score[i]=node[i].s;
weight[i]=node[i].m;
}
int ans=-1,l=0,r=c+1;
sort(node+1,node+1+c,cmpm);
k=n/2;
while(r-l>1)
{
int mid=(l+r)>>1;
int total=weight[mid],cnt1=0,cnt2=0;
for(int i=1;i<=c;i++)
if(cnt1<k&&node[i].id<mid&&node[i].m+total<=f)
{
cnt1++;
total+=node[i].m;
}
else if(cnt2<k&&node[i].id>mid&&node[i].m+total<=f)
{
cnt2++;
total+=node[i].m;
}
if(cnt1<k&&cnt2<k)
break;
else if(cnt1>=k&&cnt2>=k)
{
l=mid;//尽量往右选取
ans=score[l];
}
else if(cnt1<k)
l=mid;
else if(cnt2<k)
r=mid;
}
printf("%d\n",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(优先队列(最小堆)+ 贪心 + 枚举)

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

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

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

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

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

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

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

  7. POJ 2010 Moo University - Financial Aid treap

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

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

随机推荐

  1. 制作U盘的win7系统安装

    方法一 用iso.需要下载个UltraISO软件安装. 制作64位WIN7系统U盘安装盘方法 首页就有iso下载,有雨林木风等,我下载了系统之家最新的1907 U盘安装win7系统BIOS设置 thi ...

  2. Manacher模版

    现在讲的也是一种处理字符串的方法,叫做Manacher,有点像“马拉车” 1179: [视频][Manacher]最长回文子串 时间限制: 1 Sec  内存限制: 128 MB提交: 209  解决 ...

  3. Dedesql数据库类详解(二次开发必备教程)

    其实数据库类织梦之前就有一个介绍,http://help.dedecms.com/v53/archives/functions/db/,这篇文章讲解了数据库类的一些常见的使用方法,不过没有结合例子去介 ...

  4. 从尾到头打印列表——牛客剑指offer

    题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 解题思路 思路1: 顺序遍历链表,取出每个结点的数据,插入list中. 由于要求list倒序存储链表中的数据,而我们是顺序取 ...

  5. 关于redis的几件小事(五)redis保证高并发以及高可用

    如果你用redis缓存技术的话,肯定要考虑如何用redis来加多台机器,保证redis是高并发的,还有就是如何让Redis保证自己不是挂掉以后就直接死掉了,redis高可用 redis高并发:主从架构 ...

  6. svn add 忽略node_modules

    一劳永逸 这个窗口怎么打开 桌面右键,TortoiseSvn,然后点settings,加如下代码,要加空格 node_modules 参考: https://www.leixuesong.cn/336 ...

  7. JQuery——关于CDN(内容分发网络)

    替代方案 如果您不希望下载并存放 jQuery,那么也可以通过 CDN(内容分发网络) 引用它. Staticfile CDN.百度.又拍云.新浪.谷歌和微软的服务器都存有 jQuery . 如果你的 ...

  8. 微信小程序刮刮乐

    <view class="scratch_body"> <image class="scratch_body_bg" mode="w ...

  9. Storm项目开发纪要

    1.POM引用storm-core和javax.servlet-api这两个组件,如果本地模式跑拓扑,要把<scope>provided</scope>去掉:如果远程发布运行, ...

  10. tomcat性能优化,内存优化和并发线程连接优化

    今天被一同事问到tomcat和内存优化的问题,而网上的资料基本都是来回copy,所以抽时间随便写点.文章中设置的参数都是一个随便写的,具体的还要根据自己的情况来定. 1.内存优化: 说到tomcat不 ...