按照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,二分)的更多相关文章

  1. POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)

    POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...

  2. poj 2010 Moo University - Financial Aid

                                                                                                Moo Univ ...

  3. poj 2010 Moo University - Financial Aid 最大化中位数 二分搜索 以后需要慢慢体会

    Moo University - Financial Aid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6599   A ...

  4. poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)

    Description Bessie noted that although humans have many universities they can attend, cows have none ...

  5. POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆

    考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...

  6. poj -2010 Moo University - Financial Aid (优先队列)

    http://poj.org/problem?id=2010 "Moo U"大学有一种非常严格的入学考试(CSAT) ,每头小牛都会有一个得分.然而,"Moo U&quo ...

  7. poj 2010 Moo University - Financial Aid (贪心+线段树)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...

  8. POJ 2010 Moo University - Financial Aid treap

    按第一关键字排序后枚举中位数,就变成了判断“左边前K小的和 + 这个中位数 + 右边前K小的和 <= F",其中维护前K小和可以用treap做到. #include <cstdi ...

  9. POJ 2010 Moo University - Financial Aid 优先队列

    题意:给你c头牛,并给出每头牛的分数和花费,要求你找出其中n(n为奇数)头牛,并使这n头牛的分数的中位数尽可能大,同时这n头牛的总花费不能超过f,否则输出-1. 思路:首先对n头牛按分数进行排序,然后 ...

随机推荐

  1. 浅谈JavaScript--this指向

    js中this的值取决于调用的模式 方法调用模式 var student={ name:"adoctors", showThis:function(){ console.log(t ...

  2. 【转】C# 使用正则表达式去掉字符串中的数字,或者去掉字符串中的非数字

    源地址:http://www.cnblogs.com/94cool/p/4332957.html

  3. 2017-10-1 清北刷题冲刺班a.m

    位运算1 (bit) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK拥有一个十进制的数N.它赋予了N一个新的意义:将N每一位都拆开来后再加起来就是N所拥 ...

  4. BootStrap系统

    BootStrsp的引入: <!-- 新 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="// ...

  5. 洛谷P2812校园网络【Network of Schools加强版】

    题目背景 浙江省的几所\(OI\)强校的神犇发明了一种人工智能,可以\(AC\)任何题目,所以他们决定建立一个网络来共享这个软件.但是由于他们脑力劳动过多导致全身无力身体被\(♂\)掏\(♂\)空,他 ...

  6. H5页面开发的touchmove事件

    在做一屏滚动的H5页面的时候,必须移除touchmove事件,如果不移除,在安卓机上会触发微信原生的向下滚动拉出刷新.在IOS上出现上下都可以继续滑动,所以需要移除document的touchmove ...

  7. chapter06

    /** * Created by EX-CHENZECHAO001 on 2018-03-30. */class Chapter06 { } // 6 对象// 用对象作为单例或存放工具的方法// 类 ...

  8. Linux得到某个文件夹内文件的个数

    panonum=$(find . -type f | wc -l)

  9. UnityError 切换场景灯光变黑问题解决

    编辑器在当前场景时,它的灯光是已经渲染好了,但重新加载的时候灯光没有进行渲染 解决方法如下: Window>>lighting>>Scene面板>>右下角build ...

  10. DevOps的工程化

    孙敬云 --Worktile高级系统架构师,WTC成员 1.研发的困境 互联网的环境 互联网这个环境比较特别,包括现在不只是互联网,就算是被互联网赋能的这些“互联网+”的企业也在改变,用户在发生变化, ...