POJ 2010 Moo University - Financial Aid(堆维护滑窗kth,二分)
按照score排序,贪心,从左到右用堆维护并且记录前面的最小N/2个花费之和。
然后从右向左枚举中位数,维护N/2个数之和加上并判断是否满足条件。(stl的队列没有clear(),只能一个一个pop...
复杂度O(nlogn)
也可以二分。先按照score排序,记录备份牛在排序后的下标。然后将备份按照资金排序,
,二分中位数的值,对于mid值,按照备份顺序贪心选择牛,根据之前的下标判断在左边或者右边。
因为是最贪心的选,如果两边都不够,说明无解。如果某一边少了,mid就应该往相反的方向移动。
如果满足条件就记录答案,并向上缩小区间。
复杂度也是O(nlogn)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
//#include<bits/stdc++.h>
using namespace std; #define PB push_back
#define MP make_pair
#define fi first
#define se second
#define PS push const int maxn = 1e5+;
const int maxsz = 1e4+;
struct BinaryHEAP
{
int Heap[maxsz];
int sz;
#define Cmp(a,b) ((a)>(b))
void push(int x)
{
int i = ++sz;
while(i > ){
int p = i>>;
if(!Cmp(x,Heap[p])) break;
Heap[i] = Heap[p];
i = p;
}
Heap[i] = x;
} void pop()
{
int x = Heap[sz--];
int i = ;
while((i<<)<=sz){
int a = i<<, b = i<<|;
if(b<=sz && Cmp(Heap[b],Heap[a])) a = b;
if(!Cmp(Heap[a],x)) break;
Heap[i] = Heap[a];
i = a;
}
Heap[i] = x;
}
int top(){ return Heap[]; }
//int operator[](int x){ return Heap[x]; }
}q; pair<int,int> cow[maxn];
int N, C, F;
int Half;
int preSum[maxn]; int sol()
{
sort(cow,cow+C);
int Half = N>>, sum = ;
for(int i = ; i < Half; i++) {
sum += cow[i].se;
q.push(cow[i].se);
}
for(int i = Half; i < C-Half; i++){
preSum[i] = sum;
if(q.top() > cow[i].se) {
sum -= q.top();
q.pop();
sum += cow[i].se;
q.push(cow[i].se);
} }
q.sz = ;
sum = ;
for(int i = C-Half; i < C; i++) {
sum += cow[i].se;
q.push(cow[i].se);
}
for(int i = C-Half; --i >= Half; ){
if(sum + preSum[i] + cow[i].se <= F) return cow[i].fi;
if(q.top() > cow[i].se) {
sum -= q.top();
q.pop();
sum += cow[i].se;
q.push(cow[i].se);
}
}
return -;
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif scanf("%d%d%d",&N,&C,&F);
for(int i = ; i < C; i++){
scanf("%d%d",&cow[i].fi,&cow[i].se);
}
printf("%d\n",sol());
return ;
}
POJ 2010 Moo University - Financial Aid(堆维护滑窗kth,二分)的更多相关文章
- 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(优先队列(最小堆)+ 贪心 + 枚举)
Description Bessie noted that although humans have many universities they can attend, cows have none ...
- POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆
考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...
- 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头牛按分数进行排序,然后 ...
随机推荐
- Codeforces - 102222H - Fight Against Monsters - 贪心
https://codeforc.es/gym/102222/problem/H 题意:有一堆怪兽,怪兽有HP和ATK.你有一个英雄,英雄每次先被所有怪兽打,然后打其中一个怪兽.打的伤害递增,第一次1 ...
- AIM Tech Round 5 (rated, Div. 1 + Div. 2) E(思维,构造)
#include<bits/stdc++.h>using namespace std;long long a[150007];long long ans[150007];int main( ...
- servlet之doPost()、doGet()
1.doGet和doPost方法的具体应用?即在什么时候程序调用doGet方法,什么时候程序执行doPost方法? HttpServlet是从GenericServlet继承而来,因此HttpServ ...
- 51nod1064(Bash博弈)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1067 题意:中文题诶~ 思路:直接规律就好了... 代码: ...
- BadBoy录制模式:Request 和 Navigation比较
[前言] 今天来为大家介绍下BadBoy录制模式: Request 和 Navigation的比较! 如果您的电脑还未安装BadBoy这款工具的话,可以参考下BadBoy安装步骤和简单介绍:http: ...
- Exadata中Infiniband交换机升级
Infiniband交换机的软件补丁包,随着软件版本的不同,其发布方式也有所变化,从如下图表可以看出,1.3.3-2这个版本是一个分水岭,这个版本及以前的版本,补丁包是单独下载的,而之后的版本,inf ...
- POJ1046 Color Me Less
题目来源:http://poj.org/problem?id=1046 题目大意: 在RGB颜色空间中,用下面的公式来度量两个颜色值的距离. 现给出16个RGB表示的颜色,和一些用于测试的颜色,求被测 ...
- 2017年江西理工大学C语言程序设计竞赛(高级组)
问题 A: 求近似值 #include <stdio.h> #include <time.h> #include <stdlib.h> using namespac ...
- nginx配置openssl证书
引用出处: https://blog.csdn.net/liuchunming033/article/details/48470575 证书生成基本步骤: 生成私钥(.key)-->生成证书请求 ...
- postgresql删除还有活动连接的数据库
select pg_terminate_backend(pid) from pg_stat_activity where datname='testdb' and pid<>pg_back ...