POJ-2182 Lost Cows (二分 + 树状数组 或者平衡树)
来源:互联网 发布:3d智能试衣镜 知乎 编辑:程序博客网 时间:2024/06/06 21:02
Lost Cows
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12063 Accepted: 7760
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.
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.
* 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.
Sample Input
51210
Sample Output
24531
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std;#define maxn 8005int a[maxn], c[maxn], ans[maxn];inline int lowbit(int x){ return x & (-x);}int getsum(int x){int ans = 0;while(x){ans += c[x];x -= lowbit(x);}return ans;}void add(int x, int n, int v){while(x <= n){c[x] += v;x += lowbit(x);}}int main(){int n, l, r, mid;scanf("%d", &n);memset(c, 0, sizeof(c));a[1] = 0;for(int i = 2; i <= n; ++i){scanf("%d", &a[i]);}for(int i = n; i >= 1; --i){l = 1; r = n;while(r > l){mid = l + r >> 1;if(mid - getsum(mid) > a[i]) r = mid;else l = mid + 1;}ans[i] = l;add(l, n, 1);}for(int i = 1; i <= n; ++i){printf("%d\n", ans[i]);}}/*题意:给出序列上每个数前面有多少个数比它小,让你还原这个序列。思路:做这题需要发现首先最后一位是可以唯一确定的,那么我们倒着来还原就行了。对于每一位,我们需要找出在还未使用的数字中,排第a[i]位的数字是多少。暴力枚举哪些数字没用的话是n方了。这题用平衡树来写清晰一点,但可以用二分树状数组来做。二分答案mid,如果前面没用的数字大于a[i]就向前枚举,否则向后枚举。用过的数就在相应位置加1,这样就可以用树状数组求前缀和来统计没用过的数。下面也给出平衡树splay写的AC代码。*/
Splay代码:
#include <stdio.h>#define maxn 10000int pre[maxn], key[maxn], ch[maxn][2], sz[maxn], rt, tot, a[maxn], ans[maxn];void newNode(int& r, int father){ r = ++tot;pre[r] = father;sz[r] = 1;ch[r][0] = ch[r][1] = 0;}void rotate(int x, int kind){int y = pre[x];ch[y][!kind] = ch[x][kind];pre[ch[x][kind]] = y;if(pre[y]){ //do it when y is not rootch[pre[y]][ch[pre[y]][1] == y] = x;}pre[x] = pre[y];ch[x][kind] = y;pre[y] = x;sz[x] = sz[y];sz[y] = sz[ch[y][0]] + sz[ch[y][1]] + 1;}void splay(int r, int goal){ while(pre[r] != goal){if(pre[pre[r]] == goal){rotate(r, ch[pre[r]][0] == r);}else{int y = pre[r];int kind = ch[pre[y]][0] == y;if(ch[y][kind] == r){rotate(r, !kind);rotate(r, kind);}else{rotate(y, kind);rotate(r, kind);}}}if(goal == 0) rt = r;}void insert(int x){int r = rt;while(1){if(x < r){if(ch[r][0]) r = ch[r][0];else break;}else{if(ch[r][1]) r = ch[r][1];else break;}}newNode(ch[r][x > r], r);splay(ch[r][x > r], 0);}void find(int x){int r = rt;while(sz[ch[r][0]] != x){if(x > sz[ch[r][0]]){x -= sz[ch[r][0]] + 1;r = ch[r][1];}else{r = ch[r][0];}}splay(r, 0);}void remove(){if(ch[rt][0] == 0){rt = ch[rt][1];}else{int y = ch[rt][0];while(ch[y][1]){y = ch[y][1];}splay(y, rt);ch[y][1] = ch[rt][1];pre[ch[rt][1]] = y;rt = y;}pre[rt] = 0;}int main(){int n, x;scanf("%d", &n);a[1] = 0;for(int i = 2; i <= n; ++i){scanf("%d", &a[i]);}rt = tot = 0;newNode(rt, 0);for(int i = 2; i <= n; ++i){insert(i);}for(int i = n; i >= 1; --i){find(a[i]);ans[i] = rt;remove();}for(int i = 1; i <= n; ++i){printf("%d\n", ans[i]);}}
阅读全文
0 0
- POJ-2182 Lost Cows (二分 + 树状数组 或者平衡树)
- poj 2182 lost cows 树状数组,二分
- poj 2182 Lost Cows--树状数组+二分
- POJ 2182 Lost Cows (树状数组+二分 / 线段树 / 枚举)
- POJ 题目2182 Lost Cows(树状数组+二分)
- Poj 2182-Lost Cows(Treap||树状数组+二分答案)
- POJ 2182 Lost Cows(树状数组+二分)
- poj 2182 Lost Cows(树状数组)
- POJ 2182 Lost Cows(树状数组)
- POJ 2182 Lost Cows(树状数组)
- Lost Cows(树状数组+二分)
- POJ 2182 Lost Cows 树状数组
- poj 2182 Lost Cows 树状数组
- poj 2182 Lost Cows 树状数组
- [POJ 2182] Lost Cows · 树状数组
- poj 2182 Lost Cows 树状数组
- POJ 2182 Lost Cows树状数组
- POJ 2182 Lost Cows树状数组 *
- OkHttp网络框架的使用
- [Lintcode] #2 尾部的零
- 【luogu 2863】[USACO06JAN]牛的舞会The Cow Prom题目描述
- php return 和 return false区别
- 【Java】JAXB 处理 复杂XML 与JavaBean 互相转化过程
- POJ-2182 Lost Cows (二分 + 树状数组 或者平衡树)
- 解读GAN及其 2016 年度进展
- android studio在ADMcpu的电脑上无法开启虚拟机的问题
- windows下安装python版本的opencv
- js单张图片平移切换
- C++中break语句和continue语句讲解及使用实例
- Oracle存储过程
- Collection,List,Set
- iOS设置圆角矩形和阴影效果