POJ_2010 Moo University - Financial Aid 【堆预处理】
一、题面
二、分析
堆预处理
首先可以考虑吧随便取一个点,判断两侧的最小的总费用是多少,然后相加判断是否满足条件。如果直接判断会超时,所以需要用大根堆预处理一下。先看从分数最小的往最大的预处理,先取N/2个相加,并把他们都加入到堆中,先假设这个和值是最大的,然后不断往后扫描的过程中,不断更新大根堆的根值,以及它的和。反向预处理类似。比较容易出错的是选的范围,如果不用那些不可能到的点,可以随意点,但如果需要使用这些点,需要用足够大的值填充。
三、AC代码
#include <cstdio>
#include <algorithm>
#include <queue>
#include <fstream>
#include <iostream> using namespace std; const int MAXN = 1e5+;
int N, C, F, ans;
struct Node
{
int score, aid;
bool operator<(const Node t)const
{
return score < t.score;
}
}Calves[MAXN];
int Left[MAXN], Right[MAXN]; void solve()
{
priority_queue<int, vector<int>, less<int> > pql, pqr;
int i, temp, sum = ;
//正向扫描求最小和,保持大根堆的容量始终为N/2
for(i = ; i < N/; i++)
{
sum += Calves[i].aid;
Left[i] = ;
pql.push(Calves[i].aid);
}
for(i; i < C-N/; i++)
{
Left[i] = sum;
temp = pql.top();
if(Calves[i].aid < temp)
{
pql.pop();
sum -= temp;
sum += Calves[i].aid;
pql.push(Calves[i].aid);
}
} //反向扫描
sum = ;
for(i = C-; i > C-N/-; i--)
{
sum += Calves[i].aid;
Right[i] = ;
pqr.push(Calves[i].aid);
}
for(i; i >= N/; i--)
{
Right[i] = sum;
temp = pqr.top();
if(Calves[i].aid < temp)
{
pqr.pop();
sum -= temp;
sum += Calves[i].aid;
pqr.push(Calves[i].aid);
}
} } int main()
{
// freopen("input.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(scanf("%d %d %d", &N, &C, &F)!=EOF)
{
bool flag = false;
for(int i = ; i < C; i++)
{
scanf("%d %d", &Calves[i].score, &Calves[i].aid);
}
sort(Calves, Calves+C);
solve();
for(int i = C-N/-; i >= N/; i--)
{
if(Right[i] + Left[i] + Calves[i].aid <= F)
{
printf("%d\n", Calves[i].score);
flag = true;
break;
}
}
if(!flag)
{
printf("-1\n");
}
}
return ;
}
POJ_2010 Moo University - Financial Aid 【堆预处理】的更多相关文章
- Divide and conquer:Moo University - Financial Aid(POJ 2010)
Moo University - Financial Aid 其实是老题了http://www.cnblogs.com/Philip-Tell-Truth/p/4926008.html 这一次我们换二 ...
- 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 Descriptions 奶牛大学:奶大招生,从C头奶牛中招收N(N为奇数)头.它们分别得分score_i,需要资助学费aid_i.希望新 ...
- 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 ...
- poj2010 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( ...
随机推荐
- 599. Minimum Index Sum of Two Lists两个餐厅列表的索引和最小
[抄题]: Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of fa ...
- SHELL读取 ini 格式文件做配置文件
ini文件格式一般都是由节.键.值三部分组成 格式: [第一节 ] 第一个键 = 值 第二个键 = 第二个值 [第二节 ] 第一个键 = val1,val2,val3 例子: [COM] KINGGO ...
- GridSearchCV scoring 参考
http://scikit-learn.org/stable/modules/model_evaluation.html Scoring parameter: Model-evaluation too ...
- nodelet的理解
1.介绍 nodelet包可以为在相同进程中的多个算法之间实现零拷贝的传输方式. 这个包也提供了实现一个nodelet所需的nodelet基类以及用于实例化nodelet的NodeletLoader类 ...
- Part6-点亮指路灯_lesson1
1. 2.GPIO 查阅芯片手册:GPIO 代码: 3.外设基地址初始化 打开arm核手册, 基地址为0x70000000,去搜芯片手册6410, 把这个基地址告诉处理器,通过协处理器的cp15, 转 ...
- WCF项目问题1-找不到类型“WCFService.Service1”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHostingEnvironment/serviceActivations 中提供。
找不到类型“WCFService.Service1”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHosti ...
- cmake安装方法
由于Ubuntu14.04的cmake版本为2.8.x,而如果需要cmake3.x版本时,无法生成makefile,有两种方法可以安装cmake3.10.0: 方法1: sudo apt-get in ...
- JS Img对象获取图片高度宽度(兼容Chrome)
一般获取图片高度宽度的写法: var img = new Image();img.src = imgsrc;var imgWH = CalcImgTiple(img.width, img.height ...
- linux下利用httpd搭建tomcat集群,实现负载均衡
公司使用运营管理平台是单点tomcat,使用量大,或者导出较大的运营数据时,会造成平台不可用,现在需要搭建tomcat集群,调研后,决定使用apache的httpd来搭建tomcat集群.以下是搭建步 ...
- 数组中 reduce累计运算
let arr = [1,2,3,4]; let sum = (a, b) => a + b; arr.reduce(sum, 0); 最后输出10