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头牛按分数进行排序,然后 ...
随机推荐
- sqlachemy知识点
mysql语句 1.GROUP BY基本语法格式: GROUP BY关键字可以将查询结果按照某个字段或多个字段进行分组.字段中值相等的为一组.基本的语法格式如下: GROUP BY 属性名 [HAVI ...
- bootstrap初学者模板
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> ...
- go语言实战教程:实战项目资源导入和项目框架搭建
从本节内容开始,我们将利用我们所学习的Iris框架的相关知识,进行实战项目开发. 实战项目框架搭建 我们的实战项目是使用Iris框架开发一个关于本地服务平台的后台管理平台.平台中可以管理用户.商品.商 ...
- uoj #5. 【NOI2014】动物园
#5. [NOI2014]动物园 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学向游客要吃的园长决定开设算法班,让 ...
- [转]AFNetworking 3.0迁移指南
http://www.jianshu.com/p/047463a7ce9b?utm_campaign=hugo&utm_medium=reader_share&utm_content= ...
- POST和 GET
http GET 和 POST 请求的优缺点.区别以及误区 Get和Post在面试中一般都会问到,一般的区别: (1)post更安全(不会作为url的一部分,不会被缓存.保存在服务器日志.以及 ...
- k8s yaml文件详解
1.yaml格式的Pod配置文件内容及注解 深入Pod之前,首先我们来了解下Pod的yaml整体文件内容及功能注解. 如下: # yaml格式的pod定义文件完整内容: apiVersion: v1 ...
- Migration-添加表(加外键)
public partial class _222 : DbMigration { public override void Up() { //DropForeignKey("dbo.Ass ...
- Models-查询详细操作
# 单表简单查询13种方法 1.all(): 查询所有结果 all: models.表名.objects.all() book_all=models.Book.objects.all() # 结果是q ...
- dpkg dependency problems prevent configuration
dpkg: dependency problems prevent configuration of cr3: cr3 depends on libpng12-0 (>= 1.2.13-4); ...