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 ...
随机推荐
- 认识HTML5的WebSocket
在HTML5规范中,我最喜欢的Web技术就是正迅速变得流行的WebSocket API.WebSocket提供了一个受欢迎的技术,以替代我们过去几年一直在用的Ajax技术.这个新的API提供了一个方法 ...
- Reverse Words in a String
void reverseWords(string &s) { string res = "", tmp = ""; int l = s.length() ...
- JQuery fullCalendar 时间差 排序获取距当前最近的时间。
let time = (wo: WoDto) => wo.ScheduleTime || wo.ScheduleStartTime; let wo = technician.wos .filte ...
- 创建多个Oracle数据库及相应的实例
转 http://blog.csdn.net/luiseradl/article/details/6972217 对于使用过SQL Server数据库的用户可以会对Oracle中的数据库的实例的概念理 ...
- 如何批量删除虚拟机及其关联的存储(Windows Azure)
可以通过运行附件中PowerShell脚本文件RemoveVMandDisk.ps1批量删除VM和Disk,详细代码如下: param($serviceName) echo "Startin ...
- Codeforces Round #370(div 2)
A B C :=w= D:两个人得分互不影响很关键 一种是f[i][j]表示前i轮,分差为j的方案数 明显有f[i][j]=f[i-1][j-2k]+2*f[i-1][j-2k+1]+...+(2k+ ...
- 离散系统频响特性函数freqz()
MATLAB提供了专门用于求离散系统频响特性的函数freqz(),调用freqz()的格式有以下两种: l [H,w]=freqz(B,A,N) B和A分别为离散系统的系统函数分子.分母 ...
- 关于出现 org.apache.commons.lang.exception.NestableRuntimeException的解决方法
最近做服务端和客户端之间的访问,出现了 org.apache.commons.lang.exception.NestableRuntimeException等状况.实在令人头大,翻到了一个很好的帖子说 ...
- datePiker弹出框被其他div遮挡
最近在做项目的时候,datePiker弹出框被下面的div给遮挡住了,以前也碰到过这样类似的问题,之前直接在style中添加"z-index:1000".但是现在使用angular ...
- 理解 Paxos
Paxos是前段时间刚获得图灵奖的大神Leslie Lamport所提出的,是用来解决分布式系统中的一致性问题的算法.该算法对于分布式系统的重要性,在这里不再赘言.了解过Paxos的朋友应该都知道,要 ...