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

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std; struct node
{
node *ch[];
int sz;
int v;
int r;
int sum;
node(int v = ) : v(v)
{
r = rand();
sz = ;
ch[] = ch[] = NULL;
sum = v;
}
int cmp(int k)
{
return k > v;
}
void maintain()
{
sz = ;
sum = v;
if(ch[] != NULL)
{
sz += ch[]->sz;
sum += ch[]->sum;
}
if(ch[] != NULL)
{
sz += ch[]->sz;
sum += ch[]->sum;
}
}
}; void rotate(node *&o, int d)
{
node *k = o->ch[d^];
o->ch[d^] = k->ch[d];
k->ch[d] = o;
o->maintain();
k->maintain();
o = k;
} void insert(node *&o, int v)
{
if(o == NULL) o = new node(v);
else
{
int d = o->cmp(v);
insert(o->ch[d], v);
if(o->ch[d]->r > o->r) rotate(o, d^);
}
o->maintain();
} void remove(node *&o, int v)
{
if(v == o->v)
{
if(o->ch[] == NULL) o = o->ch[];
else if(o->ch[] == NULL) o = o->ch[];
else
{
int d = o->ch[]->r < o->ch[]->r ? : ;
rotate(o, d);
remove(o->ch[d], v);
}
} else
{
int d = o->cmp(v);
remove(o->ch[d], v);
}
if(o != NULL) o->maintain();
} int query(node *o, int k)
{
int s = o->ch[] == NULL ? : o->ch[]->sz;
int sum = o->ch[] == NULL ? : o->ch[]->sum;
if(s + > k) return query(o->ch[], k);
if(s + == k) return sum + o->v;
if(s + < k) return sum + o->v + query(o->ch[], k - s - );
} void del(node *o)
{
if(o->ch[] != NULL) del(o->ch[]);
if(o->ch[] != NULL) del(o->ch[]);
delete o;
} int main()
{
int n, c, f;
while(scanf("%d%d%d", &n, &c, &f) != EOF)
{
vector<pair<int, int> > a;
for(int i = ; i < c; i++)
{
int x, y;
scanf("%d%d", &x, &y);
a.push_back(make_pair(x, y));
} sort(a.begin(), a.end()); node *r = NULL, *l = NULL;
for(int i = ; i < n / ; i++)
insert(r, a[c - i - ].second);
for(int i = c - n / - ; i >= ; i--)
insert(l, a[i].second);
int ans = -;
for(int i = c - n / - ; i >= n / ; i--)
{
remove(l, a[i].second);
if(query(l, n / ) + query(r, n / ) + a[i].second <= f)
{
ans = a[i].first;
break;
}
insert(r, a[i].second);
} printf("%d\n", ans);
del(l); del(r);
}
return ;
}

POJ 2010 Moo University - Financial Aid treap的更多相关文章

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

    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 (贪心+线段树)

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

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

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

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

    题意:从C头奶牛中招收N(奇数)头.它们分别得分score_i,需要资助学费aid_i.希望新生所需资助不超过F,同时得分中位数最高.求此中位数. 思路: 先将奶牛排序,考虑每个奶牛作为中位数时,比它 ...

随机推荐

  1. ubuntu修改设备名称

    按住ctrl+alt+t打开终端 输入 sudo gedit /etc/hostname 出现一个打开的文件,显示的就是你的设备名字,也就是终端@的那个名字.将其修改成你想显示的名字,保存重启系统就可 ...

  2. IAR调节字体大小

    在主面板上点击tools->Options,然后点开Editor,选择下面的Colors and Fonts选项,最后选右上方的Font,选择要设置的字体就OK了.

  3. 让ztree树默认是关闭的

    只需要在ztree的回调函数中加 var treeObj = $.fn.zTree.getZTreeObj("zTreeContent");treeObj.expandAll(tr ...

  4. PCA降纬一步一步

    import numpy as np 第一步:原始值 X1 0.9 2.4 1.2 0.5 0.3 1.8 0.5 0.3 2.5 1.3 X2 1 2.6 1.7 0.7 0.7 1.4 0.6 0 ...

  5. 激活PHPStorm 2016.3

    通过互联网激活PHPStorm 2016.3: http://jetbrains.tencent.click http://172.245.22.235:1017 http://idea.imsxm. ...

  6. VLAN 间路由的几种方法

    方法一:给每个VLAN去分配一个路由器的物理连接第一步,PC设置IP地址和网关192.168.2.2/24 网关是192.168.2.1192.168.3.2/24 网关是192.168.3.1第二步 ...

  7. Ext.get Ext.getDom Ext.getCmp 的区别

    Html DOM     Ext Element   Component Component 最高层 Html DOM 最基础 Ext.getCmp  是  Ext.ComponentMgr.get ...

  8. Oracle游标--cursor_01-简单不带参数实现功能

    对于游标这个概念,可以理解为类似我们以前学习的jdbc的结果集对象, 我们可以使用游标对结果集从上到下进行遍历,获取每一行内容 首先我们要分析一下游标的分类 带参数的 书写函数的整体架构 定义游标 开 ...

  9. Net力软快速信息化系统开发框架 + 开发手册+数据库说明

    源码目录结构说明LeaRun.Cache –缓存层LeaRun.Resource –本地语言LeaRun.Utilities –公共类库LeaRun.DataAccess –数据库访问核心组件LeaR ...

  10. Device eth0 does not seem to be present, delaying initialization. 问题

    今天在复制vmware的时候 出现网卡无法启动 报错显示 Device eth0 does not seem to be present, delaying initialization. 这个错误原 ...