【POJ 2010 Moo University-Financial Aid】优先级队列
题目链接:http://poj.org/problem?id=2010
题意:C只牛犊,各有自己的分数score和申请的补助aid,现要选出N只(N为奇数),使得其aid的总和不超过F,且按score排序后中位数最大。
数据范围:N [1, 19999], C [N, 10^5], aid [0, 10^5], F, score不超过int的最大值
思路:
1. 先将C只牛按score从小到大排序,得到序列 s,这样顺序抽取出N只构成的序列 t 必然是 s 的子序列。
中位数特殊的地方在于它在序列中所处的位置,t 的长度为N,则中位数 m 是 t 的第N/2个位置(从0开始数),因此从原序列中抽取时,要保证 m 前后都已有N/2个元素。
所以 m 的位置在原序列中是有一定范围约束的,具体分析可得范围区间[N/2, C-N/2-1](从0开始)。
2. 既然已经按score从小到大排序,那么我们希望 m 在原序列 s 中的位置越靠后越好。因此可以在范围区间内从后往前枚举中位数的位置 i,判断以 i 为中位数能否选出aid总和不超过F的子序列 t。
3. 枚举的区间长度为O(C-N*2)=O(C), 每个枚举位置的判断若为O(C)一定超时。好在只是求中位数的值,不必找出所有解,甚至不必维护解,那么可以用贪心策略构造一个aid总和尽可能小的解,若其值满足条件,则说明必然有解。
4. 如何得到贪心策略的值呢,接下来的做法是参考了网上别人的思路:
预处理出两个数组before[i], after[i],分别记录位置 i 前、后按贪心策略选出的N/2个元素所能得到的最小aid总和。
具体先对 s 序列从左到右扫描,用容量上限为N/2的大顶堆维护当前扫描位置 i 之前的aid值最小的N/2个元素,并把堆中元素值总和记录在数组元素before[i]中。
然后再进行一遍从右到左的扫描,填充数组after[i]。
这里为了减少预处理的的时间,我只对before数组进行了预处理,after数组可以随着枚举位置的移动而计算,这样一旦枚举到一个可行位置,则不必计算剩余的after[i]。
这个堆开始写搓了。。。上滤下滤忘加else break了一直T,对基本原理的掌握还是不够熟练啊
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAX_C = ; int heap[MAX_C]; void swap(int& a, int& b){
int tmp = a;
a = b;
b = tmp;
} struct Heap
{
int heap[MAX_C];//大顶堆
int size;
Heap(){size=;}
void insert(int x){
size++;
heap[size-] = x;
int i = size - ;
while(i > ){
int p = (i-)/;
if(heap[p] < heap[i]){
swap(heap[p], heap[i]);
i = p;
}else break;
}
}
int getTop(){
return heap[];
}
void deleteTop(){
heap[] = heap[size-];
size--;
int i = ;
while(i*+ < size){
int lc = i*+, rc=i*+;
int c = lc;
if(rc<size && heap[rc]>heap[lc])
c = rc;
if(heap[c] > heap[i]){
swap(heap[c], heap[i]);
i = c;
}else break;
}
}
}; struct Calf
{
int score, aid;
}calves[MAX_C];
bool cmp(Calf c1, Calf c2){
return c1.score < c2.score;
} int N, C, F;
int before[MAX_C], after[MAX_C]; int main()
{
freopen("2010.txt", "r", stdin);
scanf("%d%d%d", &N, &C, &F);
for(int i=; i<C; i++){
scanf("%d%d", &calves[i].score, &calves[i].aid);
}
int begin = N/;
int end = C-N/-;
sort(calves, calves+C, cmp); if(N==){
printf("%d\n", calves[C-].score);
return ;
} Heap hb; //前begin的before[i]=0
memset(before, , sizeof(before));
memset(after, , sizeof(after)); //printf("begin=%d\nend=%d\n", begin, end);
for(int i=; i<begin; i++){ //N为奇数,begin为中间数
hb.insert(calves[i].aid);
before[begin] += calves[i].aid;//前begin个必然算入before[begin]
}
//第begin个开始有对换
for(int i=begin+; i<=end; i++){
if(calves[i-].aid < hb.getTop()){//有比堆顶更小的aid,对换
before[i] = before[i-] - hb.getTop() + calves[i-].aid;
hb.deleteTop();
hb.insert(calves[i-].aid);
}else before[i] = before[i-];
} Heap ha;
for(int i=C-; i>end; i--){
ha.insert(calves[i].aid);
after[end] += calves[i].aid;//后begin个必然算入after[C-begin]
}
//printf("after[%d] = %d\n", end, after[end]);
int flag = -;
int sum = calves[end].aid + before[end] + after[end];
if(sum <= F){
printf("%d\n", calves[end].score);
return ;
}
for(int i=end-; i>=begin; i--){ //开始从后往前枚举中位数i
if(calves[i+].aid < ha.getTop()){
after[i] = after[i+] - ha.getTop() + calves[i+].aid;
ha.deleteTop();
ha.insert(calves[i+].aid);
}else after[i] = after[i+];
//printf("after %d: %d\n", i, after[i]);
int sum = before[i] + after[i] + calves[i].aid;
//printf("sum %d: %d\n", i, sum);
if(sum <= F){
flag = calves[i].score;
break;
}
}
printf("%d\n", flag);
return ;
}
【POJ 2010 Moo University-Financial Aid】优先级队列的更多相关文章
- POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)
POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...
- poj 2010 Moo University - Financial Aid
Moo Univ ...
- 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 (优先队列)
http://poj.org/problem?id=2010 "Moo U"大学有一种非常严格的入学考试(CSAT) ,每头小牛都会有一个得分.然而,"Moo U&quo ...
- poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)
Description Bessie noted that although humans have many universities they can attend, cows have none ...
- POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆
考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...
- poj 2010 Moo University - Financial Aid (贪心+线段树)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...
- POJ 2010 Moo University - Financial Aid(堆维护滑窗kth,二分)
按照score排序,贪心,从左到右用堆维护并且记录前面的最小N/2个花费之和. 然后从右向左枚举中位数,维护N/2个数之和加上并判断是否满足条件.(stl的队列没有clear(),只能一个一个pop. ...
- POJ 2010 Moo University - Financial Aid treap
按第一关键字排序后枚举中位数,就变成了判断“左边前K小的和 + 这个中位数 + 右边前K小的和 <= F",其中维护前K小和可以用treap做到. #include <cstdi ...
- POJ 2010 Moo University - Financial Aid 优先队列
题意:给你c头牛,并给出每头牛的分数和花费,要求你找出其中n(n为奇数)头牛,并使这n头牛的分数的中位数尽可能大,同时这n头牛的总花费不能超过f,否则输出-1. 思路:首先对n头牛按分数进行排序,然后 ...
随机推荐
- xsank的快餐 » Python simhash算法解决字符串相似问题
xsank的快餐 » Python simhash算法解决字符串相似问题 Python simhash算法解决字符串相似问题
- iOS 面试常见问题最全梳理
序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了.今年,找过工作人可能会更深刻地体会到今年的就业形势不容乐观,加之,培训机构一火车地向用人单位输送iOS开发人员,打破了生态圈的动态 ...
- 如何在cmd窗口启动Tomcat
平时,一般使用tomcat/bin/startup.bat目录在windows环境启动Tomcat,或者使用IDE配置后启动. 下面来简单介绍下如果在cmd窗口直接输入命令启动Tomcat: 1.将t ...
- Android资源--颜色RGB值以及名称及样图
颜 色 RGB值 英文名 中文名 #FFB6C1 LightPink 浅粉红 #FFC0CB Pink 粉红 #DC143C Crimson 深红/猩红 #FFF0F5 L ...
- 链表list容器中通过splice合并链表与merge的不同,及需要注意的问题
#include "stdafx.h" #include <iostream> #include <list> #include <algorithm ...
- 【软件技巧】Sublime Text为不同语法定义不同高亮
Sublime Text默认的语法高亮已经非常美丽了,可是对于个别语言还是有些不爽. 默认高亮规则叫Monokai,能够从Preferences->Settings - Default中看到: ...
- 适用于CSS2的各种运动的javascript运动框架
<script> window.onload = function() { //var oDiv1 = document.getElementById('box1'); //var oDi ...
- NO.14 两个div并排,左边为绝对宽度,右边为相对宽度
两个div并排,左边为绝对宽度,右边为相对宽度,这个问题,我也经常遇到,我一般的处理方法是将最大的容器padding-left固定宽度,左边的固定宽度的一块position:absolute,然后ri ...
- 模板页 相对路径 JS 加载问题
问题:我在master页面中引入了如下js文件:<script type="text/javascript" src="http://www.cnblogs.com ...
- js渲染的3d玫瑰
参看下面链接: 程序员最美情人节礼物:JS渲染的3D玫瑰