【POJ2182】Lost Cows
【POJ2182】Lost Cows
题面
题解
从后往前做
每扫到一个点\(i\)以及比前面小的有\(a[i]\)个数
就是查询当前的第\(a[i]+1\)小
然后查询完将这个数删掉
两个操作可以用平衡树实现
但是我比较懒用了\(01trie\)
据说暴力也可以过
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (ch != '-' && (ch > '9' || ch < '0')) ch = getchar();
if (ch == '-') w = -1 , ch = getchar();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return w * data;
}
const int MAX_N = 80005;
struct Trie { int ch[2], size; } t[MAX_N * 18]; int tot;
void insert(int v) {
int o = 1; t[o].size++;
for (int i = 17; i >= 0; i--) {
int c = v >> i & 1;
if (!t[o].ch[c]) t[o].ch[c] = ++tot;
t[o = t[o].ch[c]].size++;
}
}
void erase(int v) {
int o = 1; t[o].size--;
for (int i = 17; i >= 0; i--) {
int c = v >> i & 1;
t[o = t[o].ch[c]].size--;
}
}
int Kth(int k) {
int o = 1, res = 0;
for (int i = 17; i >= 0; i--) {
int sz = t[t[o].ch[0]].size;
if (k <= sz) o = t[o].ch[0];
else k -= sz, res |= 1 << i, o = t[o].ch[1];
}
return res;
}
int N, a[MAX_N], ans[MAX_N];
int main () {
N = gi(); for (int i = 1; i < N; i++) a[i + 1] = gi(); ++tot;
for (int i = 1; i <= N; i++) insert(i);
for (int i = N; i >= 1; i--) {
ans[i] = Kth(a[i] + 1);
erase(ans[i]);
}
for (int i = 1; i <= N; i++) printf("%d\n", ans[i]);
return 0;
}
【POJ2182】Lost Cows的更多相关文章
- 【POJ2182】Lost Cows 树状数组+二分
题中给出了第 i 头牛前面有多少比它矮,如果正着分析比较难找到规律.因此,采用倒着分析的方法(最后一头牛的rank可以直接得出),对于第 i 头牛来说,它的rank值为没有被占用的rank集合中的第A ...
- 【POJ3621】Sightseeing Cows 分数规划
[POJ3621]Sightseeing Cows 题意:在给定的一个图上寻找一个环路,使得总欢乐值(经过的点权值之和)/ 总时间(经过的边权值之和)最大. 题解:显然是分数规划,二分答案ans,将每 ...
- 【2186】Popular Cows(强连通分支及其缩点)
id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS Memory Limit: 65536 ...
- 【POJ3621】Sightseeing Cows
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8331 Accepted: 2791 ...
- 【USACO】Milking Cows
Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer b ...
- 【poj2182】【poj2828】树状数组/线段树经典模型:逆序查找-空位插入法
poj2182题意:有一个1~n的排列,现在给定每个人前面有多少个人的编号比他大,求这个排列是什么.n<=8000 poj2182题解: 逆序做,可以确定二分最后一个是什么,然后删除这个数.树状 ...
- 【图论】Popular Cows
[POJ2186]Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34752 Accepted: ...
- 【USACO】Strolling Cows
Strolling Cows 给定有 \(n\) 个点 \(n\) 条边的有向图,每个点的出度都为 \(1\),求图中的最大环. 显然入度为 \(0\) 的点不可能为最大环上的点,所以考虑删点. 然后 ...
- 【poj2186】 Popular Cows
http://poj.org/problem?id=2186 (题目链接) 题意 给出一个n个点m条边的有向图,求其中没有出度强连通分量所包含的点有几个 Solution 其实这道题的题解已经在“题意 ...
随机推荐
- Python模块(进阶3)
转载请标明出处: http://www.cnblogs.com/why168888/p/6411917.html 本文出自:[Edwin博客园] Python模块(进阶3) 1. python中模块和 ...
- hdu-2879 hehe---积性函数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2879 题目大意: he[n]为小于n且满足x^2 = x (mod n)的个数 hehe[n] = ...
- 关于notify() 和notifyAll() 一个需要注意的地方
notify() 和 notifyAll()都是唤醒其他正在等待同一个对象锁的线程. 下面是我遇到的一个问题,记下来,免得忘了. 直接上代码,有错误的代码: 代码描述:有一个Caculate类,类中又 ...
- linux解压eclipse启动时无法找到jre环境的解决办法
使用软链接的方法: 1.打开终端进入到eclipse安装主目录下:mkdir jre 2.cd jre 3.ln -s /home/zhoushuo/app/jdk1.8.0_102/bin bin
- codechef Transform the Expression 转换成逆波兰式
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- 2spring注解:@Lazy,@Conditional,@import,FactoryBean接口
大致的工程内容也来源于上一篇博文! https://www.cnblogs.com/Mrchengs/p/10108603.html 1.@Lazy懒加载 懒加载:针对单实例的 单实例bean,默认在 ...
- BootstrapTable返回结果集递增序号
{ 'field': '', 'title': '序号', 'align': 'center', 'width': '5%', 'formatter': function (value, row, i ...
- building for production...Killed
npm run build报错 building for production...Killed 原理 按照他人的说法是,服务器内存不够用了,这样就给他配置一个单独的内存出来就解决了 解决方法 sud ...
- Java构造器(构造方法)与方法区别
构造器,又称为构造方法.构造器用于构造该类的实例,也就是对象. 格式如下:[修饰符] 类名 (形参列表){//n条语句} 构造方法是一种特殊的方法,与一般的方法区别: 1.构造方法的名字必须与定义 ...
- Android大图片裁剪终极解决方案(下:拍照截图)
http://blog.csdn.net/floodingfire/article/details/8144617 http://mzh3344258.blog.51cto.com/1823534/8 ...