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, NLines 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.

Sample Input

5
1
2
1
0

Sample Output

2
4
5
3
1

题目大意:
有编号1-n的n个数字,2<=n<=8000,乱序排列,排序是未知的。对于每个位置的数字,知道排在它前面比它小的数字有多少个。求这个乱序数列的顺序。

解题关键:
在剩下的编号中,第pre[n]+1个数字就是ans[n]

代码如下:

#include <iostream>
using namespace std;
const int N = 10010;
int ans[N];
int pre[N];struct node {int l, r;int len;
} tree[4 * N];void build(int l, int r, int u) {tree[u].l = l;tree[u].r = r;if (l == r) {tree[u].len = l - r + 1;
//      cout << u << " - " << tree[u].len << endl;return;}int mid = (l + r) >> 1;build(l, mid, 2 * u);build(mid + 1, r, 2 * u + 1);tree[u].len = tree[2 * u].len + tree[2 * u + 1].len;
//  cout << u << " - " << tree[u].len << endl;
}int query(int u, int num) {tree[u].len--;if (tree[u].l == tree[u].r)return tree[u].l;if (tree[2 * u].len < num) {return query(2 * u + 1, num - tree[2 * u].len);}if (tree[2 * u].len >= num) {return query(2 * u, num);}
}int main() {int n, i;cin >> n;pre[1] = 0;for (int i = 2; i <= n; i++)cin >> pre[i];build(1, n, 1);
//  for (int i = 1; i <= n; i++)
//      cout << tree[i].len << " ";
//  cout << endl;for (int i = n; i >= 1; i--) {ans[i] = query(1, pre[i] + 1);}for (int i = 1; i <= n; i++)cout << ans[i] << endl;
//  cout << endl;return 0;
}

poj2182 Lost Cows-线段树相关推荐

  1. POJ2182 Lost Cows(树状数组、二分、倍增)

    Lost Cows 题目链接 Description N (2 <= N <= 8,000) cows have unique brands in the range 1-N. In a ...

  2. POJ2182 HDU2711 Lost Cows【树状数组+线段树】

    Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17113 Accepted: 10664 Descripti ...

  3. 线段树-Difficult Lost Cows

    总时间限制: 2000ms 单个测试点时间限制: 1000ms 内存限制: 65536kB 描述 N (2 <= N <= 100,000) cows have unique brands ...

  4. POJ - 2182 Lost Cows【线段树】

    题目链接 首先,题意比较清楚,给你n-1个数,表示从2到n,其中ai表示编号为i头牛所处位置的前面有几个的序号比他小.通过这些数据,让你输出这些牛的排列顺序. 思路: 对于最后的一头牛,他肯定是(an ...

  5. 线段树(Segment Tree)

    1.概述 线段树,也叫区间树,是一个完全二叉树,它在各个节点保存一条线段(即"子数组"),因而常用于解决数列维护问题,基本能保证每个操作的复杂度为O(lgN). 线段树是一种二叉搜 ...

  6. 【BZOJ】1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚(dp/线段树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1672 dp很好想,但是是n^2的..但是可以水过..(5s啊..) 按左端点排序后 f[i]表示取第 ...

  7. 洛谷P2826 [USACO08NOV]光开关Light Switching [2017年6月计划 线段树02]

    P2826 [USACO08NOV]光开关Light Switching 题目描述 Farmer John tries to keep the cows sharp by letting them p ...

  8. poj 3264 Balanced Lineup RMQ问题 线段树

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  9. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  10. POJ 3264 Balanced Lineup 【线段树】

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 50004 Accepted: 23434 Cas ...

最新文章

  1. 深入理解Objective-C:Category
  2. session过期情况下ajax请求不会触发重新登录的问题
  3. mybatis如何根据mapper接口生成其实现类
  4. AC自动机解决字符集很大的情况(可持久化数组优化getfail的过程)
  5. 数字电路实验怎么接线视频讲解_家庭影院中音箱、功放、投影机、4K播放机不知道怎么连接?手把手教你...
  6. Excel之抽奖器实现
  7. linux rsync 安装教程,linux下的rsync配置和使用教程
  8. python文本编码转换_python实现文件批量编码转换及注意事项
  9. ansible+packer+terraform在aws上布署web服务器
  10. Windows网络编程笔记5 -- 其他套接字
  11. JavaScript开发手册
  12. (一)绪论 | 如何判断某数据结构是逻辑结构还是存储结构,是线性结构还是非线性结构
  13. 土地一分用计算机怎么算,土地计量单位换算(土地计量亩分厘单位)
  14. CentOS8报错:Error: Failed to download metadata for repo ‘appstream‘: Cannot prepare internal mirrorlis
  15. git: Cannot communicate securely with peer: no common encryption algorithm(s)
  16. 《变形金刚ONLINE》策划案
  17. android 之手机客户端登陆
  18. 【Java】Spring 教程
  19. idea报错:org.codehaus.plexus.component.repository.exception.ComponentLookupException
  20. 我的Java学习之路(第十九天)------ 排序算法(冒泡排序、快速排序)

热门文章

  1. http和https和ssl和tcp/ip之间的关系和区别
  2. linux之cut命令使用和总结
  3. Android之使用IDA Pro静态分析so文件
  4. Android之自定义属性,format详解
  5. 重庆大学计算机课程设计,计算机组成原理课程设计报告重庆大学.doc
  6. text 热敏打印机_只要有想象力,打印机都能做游戏
  7. linux把2块盘挂到一个分区,linux系统如何挂载第二块硬盘
  8. 高空真人特技表演的这些冷知识,你都知道吗?
  9. 网友半夜差点被沐浴露吓死,众人:原来不止我胆小....
  10. 你们都被电视剧版的 《西游记》给骗了!| 今日趣图