Description

N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands. 
Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow. 
Given this data, tell FJ the exact ordering of the cows. 

Input

* Line 1: A single integer, N 
* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on. 

Output

* Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.

题目大意:有n只牛,每只牛有一个1~n的编号,每只牛知道前面有多少只牛编号比它小,问每只牛的编号是啥。

思路:这题思路很多,这里采取平衡树的做法(O(n²)也是能AC的o(╯□╰)o)。先把1~n插入平衡树,从后往前扫,如果牛 i 前面有 pre[i] 只牛编号比它小,那么他就是还未被删掉的编号的第 pre[i] + 1个,然后删掉这个编号。

代码(POJ 141MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = ; int key[MAXN], weight[MAXN], child[MAXN][], size[MAXN];
int stk[MAXN], top, cnt; inline int new_node(int k) {
int x = (top ? stk[top--] : ++cnt);
key[x] = k;
size[x] = ;
weight[x] = rand();
child[x][] = child[x][] = ;
return x;
} inline void update(int &x) {
size[x] = size[child[x][]] + size[child[x][]] + ;
} inline void rotate(int &x, int t) {
int y = child[x][t];
child[x][t] = child[y][t ^ ];
child[y][t ^ ] = x;
update(x); update(y);
x = y;
} void insert(int &x, int k) {
if(x == ) x = new_node(k);
else {
int t = (key[x] < k);
insert(child[x][t], k);
if(weight[child[x][t]] < weight[x]) rotate(x, t);
}
update(x);
} void remove(int &x, int k) {
if(key[x] == k) {
if(child[x][] && child[x][]) {
int t = weight[child[x][]] < weight[child[x][]];
rotate(x, t); remove(child[x][t ^ ], k);
}
else {
stk[++top] = x;
x = child[x][] + child[x][];
}
}
else remove(child[x][key[x] < k], k);
if(x > ) update(x);
} int kth(int &x, int k) {
if(x == || k <= || k > size[x]) return ;
int s = ;
if(child[x][]) s = size[child[x][]];
if(k == s + ) return key[x];
if(k <= s) return kth(child[x][], k);
return kth(child[x][], k - s - );
} int ans[MAXN], pre[MAXN]; int main() {
int n;
scanf("%d", &n);
pre[] = ;
for(int i = ; i <= n; ++i) scanf("%d", &pre[i]);
int root = new_node();
for(int i = ; i <= n; ++i) insert(root, i);
for(int i = n; i > ; --i) {
int t = kth(root, pre[i] + );
ans[i] = t;
remove(root, t);
}
for(int i = ; i <= n; ++i)
printf("%d\n", ans[i]);
}

使用pb_ds库。使用方法可以参考WC2015的论文《C++的pb_ds库在OI中的应用》。

注意:这题在HDU上是多组数据,但是题目没有写清楚。

代码(31MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> const int MAXN = ; __gnu_pbds::tree<int, __gnu_pbds::null_type, std::less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> tree;
int ans[MAXN], pre[MAXN];
int n; int main() {
while(scanf("%d", &n) != EOF) {
for(int i = ; i <= n; ++i) scanf("%d", &pre[i]);
tree.clear();
for(int i = ; i <= n; ++i)
tree.insert(i);
for(int i = n; i > ; --i) {
int t = *tree.find_by_order(pre[i]);
ans[i] = t;
tree.erase(t);
}
for(int i = ; i <= n; ++i)
printf("%d\n", ans[i]);
}
}

POJ 2182 / HDU 2711 Lost Cows(平衡树)的更多相关文章

  1. Lost Cows POJ 2182 思维+巧法

    Lost Cows POJ 2182 思维 题意 是说有n头牛,它们身高不一但是排成了一队,从左到右编号为1到n,现在告诉你从第二号开始前面的那些牛中身高小于它的个数,一共有n-1个数.然后求出它们按 ...

  2. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  3. hdu 3045 Picnic Cows(斜率优化DP)

    题目链接:hdu 3045 Picnic Cows 题意: 有n个奶牛分别有对应的兴趣值,现在对奶牛分组,每组成员不少于t, 在每组中所有的成员兴趣值要减少到一致,问总共最少需要减少的兴趣值是多少. ...

  4. POJ 2182/暴力/BIT/线段树

    POJ 2182 暴力 /* 题意: 一个带有权值[1,n]的序列,给出每个数的前面比该数小的数的个数,当然比一个数前面比第一个数小的个数是0,省略不写,求真正的序列.(拗口) 首先想到的是从前到后暴 ...

  5. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  6. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  7. POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

    求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...

  8. poj 2182 Lost Cows(段树精英赛的冠军)

    主题链接:http://poj.org/problem? id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  9. POJ 2182 Lost Cows 【树状数组+二分】

    题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. 课时89.CSS三大特性练习(理解)

    给大家补充一点:通配符是不参与权重计算的,因为通配符的权重是0,而权重只计算id,类,标签,将来还有一种,到后面说. 练习1 直接选中和间接选中的,必然要听直接选中的. 练习2 直接选中一共有两个,直 ...

  2. CentOS 7设置网卡开机自动启用

    一.查看网卡配置 root权限 [root@dbsyn ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue ...

  3. boost::asio::ip::tcp中几个重要类型

    typedef basic_stream_socket socket; 流式套接字,提供同/异步发送接收数据,连接,绑定,设置套接字选项等功能 对于socket中的connect()方法,它只针对某一 ...

  4. SD 信贷出口 备忘

    信贷出口LVKMPFZ1,LVKMPFZ2,LVKMPFZ3

  5. VMware虚拟机下载与安装(内附密钥)

    VMware下载与安装 一.虚拟机的下载 1.进入VMware官网,点击左侧导航栏中的下载,再点击图中标记的Workstation Pro,如下图所示. 2.根据操作系统选择合适的产品,在这里以Win ...

  6. mysql数据库和数据表的简单操作

    一.数据库的增删改查 1.新建数据库 CREATE DATABASE 数据库名 charset utf8; 数据库名规则:可以由字母.数字.下划线.@.#.$ 区分大小写, 不能使用关键字如 crea ...

  7. jQuery做一个小小的移动图片的位置

    样式图点击按钮移动: jQuery代码如下: $(function () { //上            $("#btnUp").click(function () { var ...

  8. web开发学习路线

    第一阶段: HTML+CSS: HTML进阶.CSS进阶.div+css布局.HTML+css整站开发. JavaScript基础: Js基础教程.js内置对象常用方法.常见DOM树操作大全.ECMA ...

  9. thinkphp5数据库导入Excel表格

    $data=$order_info; //$data 你要下载谁 就去查谁 // $data= Db::name('order_info') // ->field('consignee,tel, ...

  10. python-三级菜单的优化实现

    三级菜单需求: 1.可依次选择进入各子菜单 2.可从任意一层往回退到上一层 3.可从任意一层退出程序 所需新知识点:列表.字典 先通过字典建立数据结构 #创建字典 city_dic = { " ...