转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove

骗一下访问量。。。。

题意大概是:从c个中选出n个,使得总花费小于等于f,保证价值的中位数最大

http://poj.org/problem?id=2010

做法:按价值排序之后,枚举中位数,然后对于小于中位数的部分贪心选出花费最小的n/2个,大于的部分也贪心选出花费最小的n/2个,然后比较总花费与f的关系。

可以用两个线段树维护一下,左部分便是每次insert一个,比较当前的第k小的大小,如果更小则remove掉原来的第k小,insert一个新的。

右部分便是每次remove一个,然后更新第k小。

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#define lson step<<1
#define rson step<<1|1
#define LL long long
using namespace std;
const int N = 100005;
struct Node{
int cost,val;
bool operator<(const Node n)const{
return val<n.val;
}
}a[N];
struct Seg_tree{
int left,right;
int small,large;
}L[N<<2];
int m,n,f,x[N],tot;
LL Left=0,Right=0;
void bulid(int step,int l,int r){
L[step].left=l;
L[step].right=r;
L[step].small=L[step].large=0;
if(l==r) return ;
int m=(l+r)>>1;
bulid(lson,l,m);
bulid(rson,m+1,r);
}
void insert(int step,int pos,int kind){
if(!kind) L[step].small++;
else L[step].large++;
if(L[step].left==pos&&pos==L[step].right) return ;
int m=(L[step].left+L[step].right)>>1;
if(pos<=m) insert(lson,pos,kind);
else insert(rson,pos,kind);
}
int query(int step,int k,int kind){
if(L[step].left==L[step].right) return L[step].left;
int m=(L[step].left+L[step].right)>>1;
if(!kind){
if(L[lson].small>=k) return query(lson,k,kind);
else return query(rson,k-L[lson].small,kind);
}
else{
if(L[lson].large>=k) return query(lson,k,kind);
else return query(rson,k-L[lson].large,kind);
}
}
void remove(int step,int pos,int kind){
if(!kind) L[step].small--;
else L[step].large--;
if(L[step].left==L[step].right) return ;
int m=(L[step].left+L[step].right)>>1;
if(pos<=m) remove(lson,pos,kind);
else remove(rson,pos,kind);
}
int main(){
scanf("%d%d%d",&m,&n,&f);
for(int i=0;i<n;i++){
scanf("%d%d",&a[i].val,&a[i].cost);
x[i]=a[i].cost;
}
sort(a,a+n);
sort(x,x+n);
tot=unique(x,x+n)-x;
bulid(1,1,tot);
for(int i=0;i<m/2;i++){
Left+=(LL)a[i].cost;
int pos=lower_bound(x,x+tot,a[i].cost)-x+1;
insert(1,pos,0);
}
for(int i=m/2+1;i<n;i++){
int pos=lower_bound(x,x+tot,a[i].cost)-x+1;
if(i<m){
Right+=(LL)a[i].cost;
insert(1,pos,1);
}
else{
int idx=query(1,m/2,1);
if(a[i].cost<x[idx-1]) Right=Right-x[idx-1]+a[i].cost;
insert(1,pos,1);
}
}
int ans=-1;
for(int i=m/2;i<n-m/2;i++){
if((LL)Left+Right+a[i].cost<=f){
ans=max(ans,a[i].val);
}
int pos=lower_bound(x,x+tot,a[i].cost)-x+1;
int idx=query(1,m/2,0);
if(a[i].cost<x[idx-1]){
Left-=(LL)x[idx-1];
Left+=(LL)a[i].cost;
remove(1,idx,0);
insert(1,pos,0);
}
pos=lower_bound(x,x+tot,a[i+1].cost)-x+1;
remove(1,pos,1);
idx=query(1,m/2,1);
if(a[i+1].cost<x[idx-1]){
Right-=(LL)a[i+1].cost;
Right+=(LL)x[idx-1];
}
}
printf("%d\n",ans);
return 0;
}

poj 2010 Moo University - Financial Aid (贪心+线段树)的更多相关文章

  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 University - Financial Aid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6599   A ...

  3. poj 2010 Moo University - Financial Aid

                                                                                                Moo Univ ...

  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 (优先队列)

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

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

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

  7. POJ 2010 Moo University - Financial Aid(堆维护滑窗kth,二分)

    按照score排序,贪心,从左到右用堆维护并且记录前面的最小N/2个花费之和. 然后从右向左枚举中位数,维护N/2个数之和加上并判断是否满足条件.(stl的队列没有clear(),只能一个一个pop. ...

  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. 全面解读WM_NOTIFY

    VC中的消息的分类有3种:窗口消息.命令消息和控件通知消息,我们这里要谈的是最后一种:控件通知消息. 控件通知消息,是指这样一种消息,一个窗口内的子控件发生了一些事情,需要通知父窗口.通知消息只适用于 ...

  2. 键盘皇者 RealForce 104Pro独家评测

    http://tech.sina.cn/?sa=t84d20738943v44&page=2&pwt=rest2&vt=4&from=mbaidu&clickt ...

  3. TCP/IP笔记 二.网络层(1)

    1. IP 1.1 配套协议 IP 是 TCP/IP 体系中两个最主要的协议之一 . 与 IP 协议配套使用的还有四个协议:   (1)ARP (Address Resolution Protocol ...

  4. C++ overloading contructor

     // overloading class constructors #include <iostream> using namespace std; class Rectangle ...

  5. DFA最小化 -- Hopcroft算法 Python实现

    wiki 伪代码看上去一直以为怪.发现葡萄牙语和俄罗斯语那里的 if 推断都还缺少一个条件. 国内的资料比較少.这几份学习资料不错.比我稀里糊涂的思路要好,分享下: http://www.liafa. ...

  6. Qml 写的弹出层控件(13篇博客)

    QML弹出窗口组件,灯箱效果.动画效果,可拖拽 核心思路:一个mask层,一个最顶层,都用rectangle,禁止事件穿透 使用 Popup { id: popup width: 200; heigh ...

  7. mysql 主从同步出问题,重新修复从库 - web架构研究

    mysql 主从同步出问题,重新修复从库 - web架构研究     mysql 主从同步出问题,重新修复从库    0     昨天由于操作失误,在从库上执行一堆sql之后,导致主从同步错误,并且已 ...

  8. 马航MH17事件将把普京逼入绝境?

    据7月22日报道,马克兰东部民间武装22日凌晨将失事客机的"黑匣子"交给马来西亚方面.乌政府与民间武装允许在坠机地点附的小范围停火. 与此同一时候,联合国安理会21日通过决议,敦促 ...

  9. Delphi引用C对象文件

    C语言应用非常广泛,并在世界各地拥有大量的代码库.这些代码库与Delphi的可比性较小,因此如果我们无需转换为Delphi代码而可以直接使用这些库的部分代码就完美了.幸运的是,Delphi允许连接到C ...

  10. 汇编与高级语言(插图结合Delphi代码,来自linzhengqun)

    汇编与高级语言 1.      汇编基础知识 1.1.      寄存器 寄存器 用途 EAX,EBX,EDX,ECX 通用寄存器,由程序员自己指定用途,也有一些不成文的用法: EAX:常用于运算. ...