POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆
考虑到数据结构短板严重,从计算几何换换口味= =
二叉堆
简介
堆总保持每个节点小于(大于)父亲节点。这样的堆被称作大根堆(小根堆)。
顾名思义,大根堆的数根是堆内的最大元素。
堆的意义在于能快速O(1)找到最大/最小值,并能持续维护。
复杂度
push() = O(logn);
pop() = O(logn);
BinaryHeap() = O(nlogn);
实现
数组下标从1开始的情况下,有
Parent(i) = i >> 1
LChild(i) = i << 1
RChild(i) = i << 1 + 1
实现 up() 和 down() 方法达到上浮,下沉
题目分析
题目要求
在c个元素中选出n(奇数)个元素,在a权值和小于F的情况下,求b权值中位数的最大值。
Handle
中位数的性质:两边元素个数相等。
解题思路
先按b权值升序排序,建两个大根堆,从n/2到c-n/2枚举中位数,维护前半段和后半段的a权值的n/2个最小值。
维护方法:若 b[i] < b[heap.top()] 则 pop() 并 push(i)。
代码比较丑 凑合看吧
//POJ 2010
//题目概述:数学题,状态转移
//二叉堆的应用,卡排序,BBS()过不了
//这个AC异常艰辛 总共提交11次 充分体现了锲而不舍的精神
//AC 2016-10-15 #include <cstdio>
#include <algorithm>
using namespace std;
#define MAXC 100000 + 10
#define MAXN 20000 + 10 struct node{
int val, cost;
friend bool operator < (const node &n1, const node &n2){
return n1.val < n2.val;
}
friend bool operator > (const node &n1, const node &n2){
return n1.val > n2.val;
}
}p[MAXC]; struct BHeap{
node heap[MAXC];
int n;
BHeap(): n(0){}
void clear(){n = 0;}
void down(int i){
for (int j = i * 2; j <= n; j *= 2){
j += (j < n) && (heap[j].cost < heap[j + 1].cost);
if (heap[j].cost > heap[i].cost){
swap(heap[i], heap[j]);
i = j;
}
else break;
}
}
void up(int i){
for (int j = i / 2; j >= 1; j /= 2){
if (heap[j].cost < heap[i].cost){
swap(heap[i], heap[j]);
i = j;
}
else break;
}
}
node &pop(){
swap(heap[1], heap[n--]);
down(1);
return heap[n + 1];
}
node &top(){
return heap[1];
}
void push(node &a){
heap[++n] = a;
up(n);
}
}heap;
int before[MAXC], after[MAXC]; int main(){
int n, c, f, minval = 0x7f7f7f7f;
freopen("fin.c", "r", stdin);
while (scanf("%d%d%d", &n, &c, &f) + 1){
for (int i = 1; i <= c; i++){
scanf("%d%d", &p[i].val, &p[i].cost);
}
sort(p + 1, p + 1 + c);
int size = n / 2; heap.clear();
before[size + 1] = 0;
for (int i = 1; i <= size; i++){
heap.push(p[i]);
before[size + 1] += p[i].cost;
}
for (int i = size + 1; i < c - size; i++){
if (heap.top().cost > p[i].cost){
before[i + 1] = before[i] - heap.pop().cost + p[i].cost;
heap.push(p[i]);
}
else before[i + 1] = before[i];
} heap.clear();
after[c - size] = 0;
for (int i = c; i > c - size; i--){
heap.push(p[i]);
after[c - size] += p[i].cost;
}
for (int i = c - size; i > size + 1; i--){
if (heap.top().cost > p[i].cost){
after[i - 1] = after[i] - heap.pop().cost + p[i].cost;
heap.push(p[i]);
}
else after[i - 1] = after[i];
}
for (int i = c - size; i >= size + 1; i--){
if (after[i] + before[i] + p[i].cost <= f){
printf("%d\n", p[i].val);
goto END;
}
}
puts("-1"); END:;
}
}
POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆的更多相关文章
- 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( 优先队列+二分查找)
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 (贪心+线段树)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...
- 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头牛按分数进行排序,然后 ...
- POJ 2010 Moo University - Financial Aid (优先队列)
题意:从C头奶牛中招收N(奇数)头.它们分别得分score_i,需要资助学费aid_i.希望新生所需资助不超过F,同时得分中位数最高.求此中位数. 思路: 先将奶牛排序,考虑每个奶牛作为中位数时,比它 ...
随机推荐
- kuangbin_SegTree E (HDU 1698)
POJ服务器炸了 还没好呢 然后就只能跳掉一些题目了 这题也是成段更新模板题 本来lazy标记不是很明白 后来学长上课讲了一下就知道原理了 回去看看代码很容易就理解了 #include <cst ...
- sql server Datetime格式转换
select CONVERT(varchar, getdate(), 120 ) 2004-09-12 11:06:08 select replace(replace(replace(CONVERT( ...
- 【转】WebKit 与 V8 的关系
页面的绘制(绘制,就是把一个HTML文件变成一个活灵活现的页面展示的过程...),只有一半轮子是Chrome自己做的,还有一部分来自于WebKit,这个Apple打造的Web渲染器...之所以说是一半 ...
- C#后台程序重启IIS,发邮件通知
应用场景:IIS网站挂掉,系统自动重启IIS,通知相关联系人: 主要代码: 监控类 public class monitoringiis { EmailSend send = new EmailSen ...
- css3立体旋转
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- (C++) CreateThread
先理解一下函数原型: HANDLE WINAPI CreateThread( _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, // 指向SECUR ...
- 外联css及js的使用
结构图如下: html如下: <!DOCTYPE html> <html> <head> <title>button test</title> ...
- javascript 使用btoa和atob来进行Base64转码和解码
javascript原生的api本来就支持,Base64,但是由于之前的javascript局限性,导致Base64基本中看不中用.当前html5标准正式化之际,Base64将有较大的转型空间,对于H ...
- ubuntu下设置clion是使用clang和clang++
链接 http://stackoverflow.com/questions/31725681/how-to-setup-clion-with-portable-clang-on-ubuntu I go ...
- 用自己的ID在appstore中更新app-黑苹果之路
由于之前套用了别人的镜像,在appstore中更新XCode时总要输别人id的密码,id还不能改.网上有的说要把XCode删掉,然后再用自己的ID更新,找到另外一个方法,更简单: 1.打开引用程序目录 ...