题目地址

  分析:如果用二分法,关键是score和aid分开排序,score排序是为了充分利用中位数的性质,这样就可以确定m左右必须各选N/2个,到这之后有人是用dp求最优解,可以再次按照aid排序一次,可以直接确定最优解(肯定是从最小的开始选择!):

 #include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int N, C, F;
const int maxn = ;
struct Cow{
int id, score, aid;
}Score[maxn], Aid[maxn];
bool cmp_score( const Cow &a, const Cow &b) {
return a.score < b.score;
}
bool cmp_aid(const Cow &a, const Cow &b) {
return a.aid < b.aid;
}
int main(void) { scanf("%d%d%d", &N, &C, &F);
for (int i = ; i < C; i++) scanf("%d%d", &Score[i].score, &Score[i].aid);
sort(Score, Score+C, cmp_score);
for (int i = ; i < C; i++) Score[i].id = i;
memcpy(Aid, Score, sizeof(Cow)*C);
sort(Aid, Aid+C, cmp_aid);
int l = , u = C, ans = -;
while (u-l > ) {
int m = (u+l) >> ;
int left = , right = , total = Score[m].aid;
for (int i = ; i < C; i++) {
if (Aid[i].id < m && (total+Aid[i].aid) <= F && left < N/) {
total += Aid[i].aid;
left++;
} else if (Aid[i].id > m && (total+Aid[i].aid) <= F && right < N/) {
total += Aid[i].aid;
right++;
}
}
if (left < N/ && right < N/) {//insufficient
ans = -;
break;
} else if (left < N / ) {
l = m;
} else if (right < N / ){
u = m;
} else {
ans = Score[m].score;
l = m;
}
}
printf("%d\n", ans);
return ;
}

  百度还看到用堆、优先队列做的!mark。  

  参考:

    《挑战程序设计竞赛》3.1

POJ2010 Moo University - Financial Aid(二分法)的更多相关文章

  1. poj2010 Moo University - Financial Aid 优先队列

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

  2. Poj2010 Moo University - Financial Aid

    题意的话,就看其他人的吧 概括:二分中位数 大体上便是二分一个中位数,带入检验,若分数比他小的有\(\lfloor n/2 \rfloor\)个,分数比他的大的也有这么多,而且贪心的买,花费小于预算. ...

  3. Divide and conquer:Moo University - Financial Aid(POJ 2010)

    Moo University - Financial Aid 其实是老题了http://www.cnblogs.com/Philip-Tell-Truth/p/4926008.html 这一次我们换二 ...

  4. 【POJ - 2010】Moo University - Financial Aid(优先队列)

    Moo University - Financial Aid Descriptions 奶牛大学:奶大招生,从C头奶牛中招收N(N为奇数)头.它们分别得分score_i,需要资助学费aid_i.希望新 ...

  5. Moo University - Financial Aid

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

  6. poj 2010 Moo University - Financial Aid

                                                                                                Moo Univ ...

  7. POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)

    POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...

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

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

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

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

随机推荐

  1. ReactNative运行提示缺少文件xxxRootView.h解决方法

    我们经常在github获取源码后运行会出现缺少 "RCTRootView.h" notfound" 文件的错误,对于这种错误我们怎么解决了. 1.cd到项目根目录 删除项 ...

  2. EMV内核使用中的常见问题

    EMV内核在使用上会由于调用不当引起的许多问题,本文旨在基于内核LOG(也就是与IC卡交互的指令LOG)的基础上,对一些常见问题作初步的分析与解答,方便不熟悉EMV规范的同学参考. 本文的前提是你已经 ...

  3. FineUI参考手册(离线版)现已免费提供下载!

    http://fineui.com/bbs/forum.php?mod=viewthread&tid=3473

  4. lecture8-RNN的训练方法之二三

    HInton第8课,之所以说之二三,是因为训练RNN的四种方法之一:长短时记忆在lecture7中介绍过了,这里介绍的是第二和第三种方法:HF优化和Echo (这个字觉得翻译成回声是不是欠妥,所以保留 ...

  5. Bootstrap系列 -- 13. 内联表单

    有时候我们需要将表单的控件都在一行内显示.在Bootstrap框架中实现这样的表单效果是轻而易举的,你只需要在<form>元素中添加类名“form-inline”即可 如果你要在input ...

  6. Android开发环境部署

    引言   在windows系统中安装Android的开发环境,将分为五个步骤来完成: 第一步:安装JDK 第二步:配置Windows上JDK的变量环境 第三步: 下载安装Eclipse 第四步:下载安 ...

  7. MEF

    详情见链接:http://www.cnblogs.com/comsokey/p/MEF1.html#top

  8. centos 7 安装mysql

    # wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm # rpm -ivh mysql-community- ...

  9. 数据库高可用架构(MySQL、Oracle、MongoDB、Redis)

    一.MySQL MySQL小型高可用架构 方案:MySQL双主.主从 + Keepalived主从自动切换   服务器资源:两台PC Server 优点:架构简单,节省资源 缺点:无法线性扩展,主从失 ...

  10. 开发错误12:gradle编译错误:Conflict with dependency com.android.support:support-annotations

    在build.gradle中的configurations.all {}下添加:resolutionStrategy.force 'com.android.support:support-annota ...