考虑到数据结构短板严重,从计算几何换换口味= =

二叉堆

简介

堆总保持每个节点小于(大于)父亲节点。这样的堆被称作大根堆(小根堆)。

顾名思义,大根堆的数根是堆内的最大元素。

堆的意义在于能快速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 初探数据结构 二叉堆的更多相关文章

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

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

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

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

  3. poj 2010 Moo University - Financial Aid

                                                                                                Moo Univ ...

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

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

  5. poj -2010 Moo University - Financial Aid (优先队列)

    http://poj.org/problem?id=2010 "Moo U"大学有一种非常严格的入学考试(CSAT) ,每头小牛都会有一个得分.然而,"Moo U&quo ...

  6. poj 2010 Moo University - Financial Aid (贪心+线段树)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...

  7. POJ 2010 Moo University - Financial Aid treap

    按第一关键字排序后枚举中位数,就变成了判断“左边前K小的和 + 这个中位数 + 右边前K小的和 <= F",其中维护前K小和可以用treap做到. #include <cstdi ...

  8. POJ 2010 Moo University - Financial Aid 优先队列

    题意:给你c头牛,并给出每头牛的分数和花费,要求你找出其中n(n为奇数)头牛,并使这n头牛的分数的中位数尽可能大,同时这n头牛的总花费不能超过f,否则输出-1. 思路:首先对n头牛按分数进行排序,然后 ...

  9. POJ 2010 Moo University - Financial Aid (优先队列)

    题意:从C头奶牛中招收N(奇数)头.它们分别得分score_i,需要资助学费aid_i.希望新生所需资助不超过F,同时得分中位数最高.求此中位数. 思路: 先将奶牛排序,考虑每个奶牛作为中位数时,比它 ...

随机推荐

  1. mysql登陆出现unknown database错误可能原因

    输入了错误命令如 # mysql -u root -p test 然后客户端会出现需要输入命令的提示,即使输入正确出现错误提示 正确命令是 # mysql -u root -p

  2. C#创建datatable

    Asp.net DataTable添加列和行的方法 方法一: DataTable tblDatas = new DataTable("Datas"); DataColumn dc ...

  3. [Linux] VirtualBox - 主机与虚拟机互通 - CentOS

    使用VirtualBox的主机与虚拟机相互通信方法: (使用VirtualBox不要使用绿色版的,因为绿色版的没有安装虚拟网卡驱动,所以主机与虚拟机是不能相互通信的,切记) 1.设置已经安装好的操作系 ...

  4. 电源开关IC

    RT9701:IO控制的电源开关.宽输入电压(2.2~6v),1.1A的连续输出电流.用在USB开关电压,热插拔和电池电池充电器的场合应用

  5. JVM参数(一)JVM类型以及编译器模式

    现在的JVM运行Java程序(和其它的兼容性语言)时在高效性和稳定性方面做的非常出色.自适应内存管理.垃圾收集.及时编译.动态类加载.锁优化——这里仅仅列举了某些场景下会发生的神奇的事情,但他们几乎不 ...

  6. 便捷的php操作mysql库MysqliDb

    github 地址:https://github.com/joshcam/PHP-MySQLi-Database-Class MysqliDb -- Simple MySQLi wrapper and ...

  7. mac下删除svn账号

    以mac os x为例(Unix/Linux类似), 1.打开命令行窗口,即用户的根目录(用户的home目录) $ ls -al drwxr-xr-x   6 linxyz  staff    204 ...

  8. FreeBSD 10 中文环境

    中文字体位置 FontPath        "/usr/local/lib/X11/fonts/misc/:unscaled" FontPath        "/us ...

  9. R型思维模式对软件开发的影响(草稿)

    The pragmatic programmers 一直在工作之余读些书,之前主要是纯英文版的计算机相关的算法,编译器,数学等,想通过读这些书来提高自己每日工作效能,结果收效甚微.一是,因为纯英文的书 ...

  10. 用 Navicat 写mysql的游标

    千言万语也比不上一个简单直接明了的小例子: CREATE PROCEDURE pro_users() begin DECLARE myid int; DECLARE no int; ); ); ); ...