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,同时得分中位数最高.求此中位数. 思路: 先将奶牛排序,考虑每个奶牛作为中位数时,比它 ...
随机推荐
- HTML DOM 实例-Document 对象
使用 document.write() 向输出流写文本 <html><body><script type="text/javascript">d ...
- [solr] - Facet
Solr facet使用于分类统计,是最好的工具.下面例子使用facet将可模拟查询搜索出租房信息. 1.在schema.xml中的内容如下: <?xml version="1.0&q ...
- Python paramiko 模块
paramiko模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: import paramiko # 创建SSH对象 ssh ...
- bzoj4130: [PA2011]Kangaroos
Description 定义两个区间互相匹配表示这两个区间有交集. 给出长度为N的区间序列A,M次询问,每次询问序列A中最长的连续子序列,使得子序列中的每个区间都与[L,R]互相匹配 N<=50 ...
- C++中没有finally,那么应该在如何关闭资源
这是一篇有趣的帖子 原文链接: http://bbs.csdn.net/topics/90070457 楼主: C++中没有finally,那么应该在哪里关闭资源? C++的try{}catch(){ ...
- STM32F4 SPI2初始化及收发数据【使用库函数】
我的STM32F4 Discovery上边有一个加速度传感器LIS302DL.在演示工程中,ST的工程师使用这个传感器做了个很令人羡慕的东西:解算开发板的姿态.当开发板倾斜时候,处于最上边的LED点亮 ...
- python分布式任务调度Celery
Celery是Python开发的分布式任务调度模块,今天抽空看了一下,果然接口简单,开发容易,5分钟就写出了一个异步发送邮件的服务. Celery本身不含消息服务,它使用第三方消息服务来传递任务,目前 ...
- Cordova从服务器更新客户端的JS文件
思路: 1.主要使用 Cordova的File插件 2.获取需要替换的js文件安装后的路径 3.软件使用js发起ajax请求,后台返回版本号跟客户端版本号对比 4.如果发现需要更新js文件,则用js调 ...
- poj 1416 (hdu 1539)Shredding Company:剪枝搜索
点击打开链接 题目大意是有一个分割机,可以把一串数字分割成若干个数字之后求和,题目输入一个数字上界和待分割的数字,让我们求出分割后数字之和在不超过给定max的情况下的最大值,并且给出分割方案,如果没有 ...
- Aquarium Filtration
http://www.fishyou.com/aquarium-filtration.php Aquarium Filtration This section covers aquarium filt ...