POJ2010 Moo University - Financial Aid(二分法)
分析:如果用二分法,关键是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(二分法)的更多相关文章
- poj2010 Moo University - Financial Aid 优先队列
Description Bessie noted that although humans have many universities they can attend, cows have none ...
- Poj2010 Moo University - Financial Aid
题意的话,就看其他人的吧 概括:二分中位数 大体上便是二分一个中位数,带入检验,若分数比他小的有\(\lfloor n/2 \rfloor\)个,分数比他的大的也有这么多,而且贪心的买,花费小于预算. ...
- Divide and conquer:Moo University - Financial Aid(POJ 2010)
Moo University - Financial Aid 其实是老题了http://www.cnblogs.com/Philip-Tell-Truth/p/4926008.html 这一次我们换二 ...
- 【POJ - 2010】Moo University - Financial Aid(优先队列)
Moo University - Financial Aid Descriptions 奶牛大学:奶大招生,从C头奶牛中招收N(N为奇数)头.它们分别得分score_i,需要资助学费aid_i.希望新 ...
- Moo University - Financial Aid
Moo University - Financial Aid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6020 Accep ...
- poj 2010 Moo University - Financial Aid
Moo Univ ...
- POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)
POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...
- poj 2010 Moo University - Financial Aid 最大化中位数 二分搜索 以后需要慢慢体会
Moo University - Financial Aid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6599 A ...
- poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)
Description Bessie noted that although humans have many universities they can attend, cows have none ...
随机推荐
- vmware安装cent os 6.5 + oracle 11g xe + jboss eap 6.2 + weblogic 12c+ webshpere mq 7.5
前言: mac系统发展速度确实很快,短短数年,mac os上已经能网银支付(中行.招行.工商.支付宝等均已全面支持mac os了),windows上的经典常用软件:qq.飞信.旺旺.有道词典.有道云笔 ...
- 什么是API
我们从API的功能.分类.设计.实现.用户来看什么是API. API是应用程序组件之间通信的接口 --wiki:Application Programming Interface In compute ...
- 从setTimeout谈JavaScript运行机制
从setTimeout说起 众所周知,JavaScript是单线程的编程,什么是单线程,就是说同一时间JavaScript只能执行一段代码,如果这段代码要执行很长时间,那么之后的代码只能尽情地等待它执 ...
- 记、基于react-router的单页应用
现在用react写单页应用基本上都是用react-router做前端路由了吧!最近在使用react-router的过程中遇到了不少问题,在这里总结一下. 浏览器url react-router默认提供 ...
- Angular指令1
Angular的指令 也就是directive,其实就是一WebComponent,以前端的眼光来看,好象很复杂,但是以后端的眼光来看,还是非常简单的.其实就是一个中等水平的类. var myModu ...
- Log4net使用(一)
LogHelper.cs using NLog; using NLog.Targets; namespace MyProject.Tool.Log { public class LogHelper { ...
- [HDOJ5439]Aggregated Counting(乱搞)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5439 题意:按规则构造一个数列a a(1)=1 a(2)=2 a(2)=2 -------> 写两个 ...
- 关于Hellas和Greece
一直以来我就好奇,为什么希腊的中文名字“希腊”和英文名字”Greece”听起来都不像(就像“德国”不像“Germany”一样),而且,为什么在很多体育比赛中看到希腊运动员的衣服上都是“Hellas”, ...
- Project Serve 2013部署方法
在线版Project2013部署手册 服务器环境要求 系统:windows server 2008r2.windows server2012x64 Sharepoint 2013 内存至少16GB,最 ...
- Beta版本冲刺Day2
会议讨论 628: 和建平同学一起合作解决了如何获取勾选事件,将勾选的课程信息存入select表格中.但是当运行更新后的项目遇到了无法连接数据库的问题了,目前还在解决中. 因为碰到了这样的 ...