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( ...
随机推荐
- Linux3基本命令 ls,pwd,cat,echo,mv,cp,mkdir,rm,ln
ls 列出文件名称. -l 列出长文件名称. -rwxr-xr-- 1 root root 10739 Dec 23 13:31 bbscon (7) (4) (5) (6) ...
- lucene和solr
我们为什么要用solr呢? 1.solr已经将整个索引操作功能封装好了的搜索引擎系统(企业级搜索引擎产品) 2.solr可以部署到单独的服务器上(WEB服务),它可以提供服务,我们的业务系统就只要发送 ...
- vmware workstation + kvm 部署
1.物理机BIOS设置里开启虚拟化功能 2.vm里面开启一下两个功能 3.找到存放该虚拟机的dir,找到以.vmx结尾的文件,增加一行 apic.xapic.enabled = "FALSE ...
- NUMA微架构
NUMA微架构 written by qingran September 8th, 2011 no comment 现在开始补日志,逐步的扫清以前写了一半的和"欠账未还的".半年之 ...
- 跨库连接报错Server 'myLinkedServer' is not configured for RPC
Solution: Problem is most likely that RPC is not configured for your linked server. That is not a de ...
- Requests接口测试(一)
接口测试概念 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻辑依赖关 ...
- 如何处理与开发有争议的Bug?
工作中,测试人员有时会遇到类似的问题:提交了一份软件缺陷报告,可由于某种原因,无论是开发人员还是开发经理就是不愿修改程序.应如何处理这类问题呢?我认为,当对报告出现分歧意见后,测试工程师应首先做如下 ...
- js任意位数求和
<script> //任意位数求和 function sum(){ if(arguments.length==1) { console.log(arguments[0]) return; ...
- Win7 WPF程序无法接受外部拖拽
最近在WPF项目中遇到一个问题.虽然选择了AllowDrop = True,但是还是无法支持从外部拖拽文件到程序,倒是内部拖拽(如从一个列表拖拽到树)和从程序拖拽到外部可以. 解决过程 1.考虑是程序 ...
- android android遇到的错误
android遇到的错误 一.eclipse运行.生成.打包APK报错: 原因1: jdk版本太低,升级jdk 原因2: tools选择: android sdk build-tools 大于等于2 ...