POJ 2010 Moo University - Financial Aid treap
按第一关键字排序后枚举中位数,就变成了判断“左边前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的更多相关文章
- 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 (优先队列)
http://poj.org/problem?id=2010 "Moo U"大学有一种非常严格的入学考试(CSAT) ,每头小牛都会有一个得分.然而,"Moo U&quo ...
- POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆
考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...
- poj 2010 Moo University - Financial Aid (贪心+线段树)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...
- POJ 2010 Moo University - Financial Aid 优先队列
题意:给你c头牛,并给出每头牛的分数和花费,要求你找出其中n(n为奇数)头牛,并使这n头牛的分数的中位数尽可能大,同时这n头牛的总花费不能超过f,否则输出-1. 思路:首先对n头牛按分数进行排序,然后 ...
- POJ 2010 Moo University - Financial Aid (优先队列)
题意:从C头奶牛中招收N(奇数)头.它们分别得分score_i,需要资助学费aid_i.希望新生所需资助不超过F,同时得分中位数最高.求此中位数. 思路: 先将奶牛排序,考虑每个奶牛作为中位数时,比它 ...
随机推荐
- Oracle重新装机后如何快速还原以前表和用户
本人使用的oracle10g 首先拷贝以前的oradata 文件夹 一:重新创建oracle数据库后手动关闭oracle所有服务 二:将oradata中新创建的数据库目录改名,d:\app\user\ ...
- RandomAccessFile使用小结
本文是基于Linux环境运行,读者阅读前需要具备一定Linux知识 RandomAccessFile是Java输入/输出流体系中功能最丰富的文件内容访问类,既可以读取文件内容,也可以向文件输出数据.与 ...
- 【转】Mac系统中安装homebrew(类似redhat|Centos中的yum;类似Ubuntu中的apt-get)
Homebrew,Homebrew简称brew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件,可以说Homebrew就是mac下的apt-get.yum神器 Homebr ...
- AIX 环境下ODM库同步
IBM AIX v5.3操作系统环境下有时会出现ODM库与rootvg硬盘上数据不同步的情况.使用命令lsvg -l datavg检查文件系统类型,发现显示为"???"这就表示OD ...
- sass揭秘之@mixin,%,@function
因为文章内含有很多sass代码,如需自己动手查看编译结果,推荐使用sassmeister这款在线编译工具,方便你阅读学习. 在阅读本文章之前,请先确认你已经阅读了上篇文章sass揭秘之变量,不然会给你 ...
- python 字符编码 转换
#!/bin/env python#-*- encoding=utf8 -*-# 文件头指定utf8编码还是乱码时,使用下面方式指定# fix encoding problem import sys ...
- 安装Eclipse及相关Plugin
Eclipse: 此处选用基于Eclipse的开发工具:STS 官方网站:https://spring.io/tools/sts Plugins: Open Explorer文件浏览插件it open ...
- Unity 几种优化建议
转: http://user.qzone.qq.com/289422269/blog/1453815561?ptlang=2052 Unity 几种优化建议 最简单的优化建议: 1.PC平台的话保持场 ...
- VFP MSSOAPTOOKIT 使用SOAP Headers
.NET 有如下使用了自定义扩展HEADER来做验证 server.asmx代码 using System;using System.Collections.Generic;using System ...
- Android菜鸟成长记16 -- JSON的解析
JSON的定义 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...