总时间限制: 2000ms

单个测试点时间限制: 1000ms

内存限制: 65536kB

描述

N (2 <= N <= 100,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.

输入

* 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.

输出

* 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.

样例输入

5
1
2
1
0

样例输出

2
4
5
3
1

【分析】

先对着数据手算,试着总结出算法,发现应该倒着考虑

对于每一个位置,我们都需要找到数,使得中没有使用过的数恰好是(输入的数组)

一种思路是用一个数组,cnt[i]表示[0, i-1]没有被使用过的数的数量,然后二分查找即可。查询时间复杂度仅为O(log N),但是更新的时间复杂度为O(N)。

另一种思路是线段树,区间[l, r]中存放没有被使用过的数的数量。这里的查询操作并不是我们学过的区间查询,但依然用到了区间分解的思想,非常有意思。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;const int MAXN = 100005;struct node {int cnt;
};node tree[MAXN*4];
int a[MAXN];
int ans[MAXN];void build_tree(int root, int l, int r)
{tree[root].cnt = r - l + 1;if (l == r)return ;int mid = (r + l) / 2;build_tree(root*2+1, l, mid);build_tree(root*2+2, mid+1, r);
}/* * 1. return the brand i, such that in brand [l, i] there is num unused * 2. set brand i to be used*/
int find(int root, int l, int r, int num)
{// printf("calling find(root=%d, l=%d, r=%d, num=%d)\n", root, l, r, num);// printf("cnt = %d\n", tree[root].cnt);if (l == r) {--tree[root].cnt;return l;}int mid = (l+r)/2;int rt;if (tree[root*2+1].cnt >= num)rt = find(root*2+1, l, mid, num);elsert = find(root*2+2, mid+1, r, num-tree[root*2+1].cnt);--tree[root].cnt;return rt;
}int main()
{int N;scanf("%d", &N);build_tree(0, 0, N-1);for (int i = 1; i < N; ++i)scanf("%d", &a[i]);for (int i = N-1; i >= 0; --i) {ans[i] = find(0, 0, N-1, a[i]+1) + 1;}for (int i = 0; i < N; ++i)printf("%d\n", ans[i]);system("pause");return 0;
}

需要说明的是,由于一个root下标仅与唯一的区间[l, r]相对应(与N,与题目均无关),所以数据结构node中也可以不存放l, r,在递归函数的参数中体现即可。

线段树-Difficult Lost Cows相关推荐

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

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

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

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

  3. 【POJ】2828 Buy Tickets(线段树+特殊的技巧/splay)

    http://poj.org/problem?id=2828 一开始敲了个splay,直接模拟. tle了.. 常数太大.. 好吧,说是用线段树.. 而且思想很拽.. (貌似很久以前写过貌似的,,) ...

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

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

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

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

  6. 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 ...

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

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

  8. POJ 3264 Balanced Lineup 【线段树】

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

  9. 【BZOJ3050】Seating,线段树

    传送门(权限题) 3050: [Usaco2013 Jan]Seating Time Limit: 10 Sec Memory Limit: 128 MB Submit: 132 Solved: 76 ...

最新文章

  1. 批归一化和Dropout不能共存?这篇研究说可以
  2. MySQL设计一个图书馆数据库_请设计一个图书馆数据库
  3. python 进程理论基础
  4. 那些2019年会爆发的泛娱乐黑科技风口——网易MCtalk泛娱乐创新峰会揭秘
  5. P1251-餐巾计划问题【费用流】
  6. python 轨迹预测_CVPR 2019轨迹预测竞赛冠军方法总结
  7. 爱因斯坦最熟悉的中国人,曾被学校开除的自学天才周培源
  8. [转载] Java对返回值的封装
  9. vue.js:634 [Vue warn]: Error in render: “TypeError: Cannot read property ‘matched‘ of undefined“
  10. python电脑下载-Python2.7.6
  11. 数字图像来源:光学成像系统
  12. linux常用net命令
  13. 从小程序快速扫码进微信群聊
  14. Drill系列(1):Dremel的原理
  15. C语言之struct
  16. 360wifi一直在启用中
  17. java vpa_使用VPA快速洞悉Java应用性能瓶颈
  18. Flutter Web 鼠标样式修改
  19. 黑客成员煽动DDos攻击全球银行,多个国内银行赫然在列
  20. oracle crs 4535,11gRAC报错CRS-4535,CRS-4000解决

热门文章

  1. img格式镜像转ISO格式
  2. Docker学习2——Docker高级
  3. 点积和叉积【计算集合】
  4. ​终于有人把Spark大数据分析与挖掘讲明白了
  5. linux 线程优先级 rt_prio static_prio prio normal_prio
  6. MySQL MVCC底层原理详解
  7. ubuntu查看内存占用和查看cpu使用情况的2种方法
  8. 经典测试用例设计-带广告图案的花纸杯​​​​​​​
  9. Java实现pdf转tex_使用Free Spire.PDF在Java程序中创建和转换PDF文件
  10. Bomb Lab!!!