【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 其实这道题的题解已经在“题意 ...
随机推荐
- windows下libevent的编译及使用
之前简单分析了libevent的源码,过了一段时间要用的时候发现完全忘记了..从头记录一下流程 1.编译 可以从github下载libevent的压缩包,解压后 修改以下三个文件,添加宏定义: 在以下 ...
- WAS8.5安装和部署
刚研究WAS8.5的安装部署,写一些注意事项: 1.下载安装软件 安装器:agent.installer.linux.gtk.x86_64_1.7.2000.20140227_0303 WAS: WA ...
- Git bash 终止git log 命令
Git bash中 可以通过键入: q ,结束该命令.
- PHP Socket 简单使用
<?php /*socket收发数据 @host(string) socket服务器IP @post(int) 端口 @str(string) 要发送的数据 @back 1|0 socket端是 ...
- javascript 构造函数方式定义对象 (转载)
javascript 构造函数方式定义对象 javascript是动态语言,可以在运行时给对象添加属性,也可以给对象删除(delete)属性 <html> <head> & ...
- jpa多条件查询
首先继承JpaSpecificationExecutor<T>接口 需要用到JpaSpecificationExecutor<T>中的Page<T> findAll ...
- NodeJS学习日记--VSCode下调试
在vscode中打开项目文件夹 点击左侧的调试菜单,在打开的页面中点击下拉框并点击添加配置 在弹出框中选择 node.js vscode 会自动在项目文件夹下添加.vscode文件夹,并创建launc ...
- 爬虫之Beautifulsoup及xpath
1.BeautifulSoup (以 Python 风格的方式来对 HTML 或 XML 进行迭代,搜索和修改) 1.1 介绍 Beautiful Soup提供一些简单的.python式的函数用来处理 ...
- IOS开发,摄像头对焦状态监控
camera autofocus observer? I find the solution for my case to find when autofocus starts / ends. It' ...
- 轻量ORM-SqlRepoEx (九)与Dapper共舞
Dapper就另一个轻量ORM,Dapper及其扩展解决了数据访问端的大部门问题,提供了如数据事务管理.缓存等支持.SqlRepoEx的重点解决了Lambda转换成SQL语句,使SQL使用强类型编写, ...