Lost Cows
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.
Sample Input
5
1
2
1
0
Sample Output
5
1
2
1
0
题意:N头奶牛排队,它们的身高为1~n,知道每头牛前面有多少头比自己矮,求每头牛的身高。
分析:不难发现要从后往前确定每头牛的身高,这样每头牛的身高就是1~n中没被选过的第a[i]+1大的数(因为有a[i]头比自己矮),那么我们只需用树状数组维护一个01序列,没选过的数标为1,每次查询时二分mid,通过树状数组ask(mid)计算前mid个数有几个为1即没被选过的,直到找到a[i]+1。
Submit
结构体实现
/*
线段树求解本题,每个结点表示区间内数字的个数,向左找的过程,实际上是缩小找第pre[n]+1大的数的范围,向右找是找去掉左边的个数m后也就是找第pre[n]+1-m大的数,所以每向右找一次区间个数都要变化,进一步找到ans[n]。
代码实现步骤:建树->查询+更新(分两种情况,找左或找右,递归)->得序列
*/
#include<iostream>
#include<cstdio>
using namespace std;
const int Max = 10000;
struct
{
int l, r, len;//len储存这个区间下数字的个数,即这个结点下牛的数量
} tree[4 * Max];//线段树空间需要
int pre[Max], ans[Max];
void BuildTree(int left, int right, int u)//建树
{
tree[u].l = left;
tree[u].r = right;
tree[u].len = right - left + 1;//更新结点u的值
if (left == right)
return;
BuildTree(left, (left + right) >> 1, u << 1);//递归左子树
BuildTree(((left + right) >> 1) + 1, right, (u << 1) + 1);//递归右子树
}
int query(int u, int num)//查询+维护,所求值为当前区间中左起第num个元素
{
tree[u].len--;//对访问到的区间维护len,即把这个结点上牛的数量减1
if (tree[u].l == tree[u].r)
return tree[u].l;
//情况1:左子区间内牛的个数不够,则查询右子区间中左起第nun-tree(u << 1]. len个元素
if (tree[u << 1].len < num)
return query((u << 1) + 1, num - tree[u << 1].len);
//情况2:左子区间内牛的个数足够,依旧查询左子区间中左起第num个元素
if (tree[u << 1].len >= num)
return query(u << 1, num);
}
int main()
{
int n, i;
scanf("%d", &n);
pre[1] = 0;
for (i = 2; i <= n; i++)
scanf("%d", &pre[i]);
BuildTree(1, n, 1);
for (i = n; i >= 1; i--)
ans[i] = query(1, pre[i] + 1);
//从后往前推断出每次最后一个数字
for (i = 1; i <= n; i++)
printf("%d\n", ans[i]);
return 0;
}
完全二叉树实现
#include<bits/stdc++.h>
using namespace std;
const int Max = 10000;
int pre[Max] = {0}, tree[4 * Max] = {0}, ans[Max] = {0};
void BuildTree(int n, int last_left)//用完全二叉树建一个线段树
{
int i;
for (i = last_left; i < last_left + n; i++)
tree[i] = 1;//给二叉树的最后一行赋值,左边n个结点是n头牛
while (last_left != 1)//从二叉树的最后一行倒推到根结点,根结点的值是牛的总数
{
for (i = last_left / 2; i < last_left; i++)
tree[i] = tree[i * 2] + tree[i * 2 + 1];
last_left = last_left / 2;
}
}
int query(int u, int num, int last_left)
{//u表示结点,num表示剩余序列里第pre[i]+1大的数,用来找ans[i],last_left表示最后一行左边第一个结点的标号
//查询+维护,关键的一点是所求值为当前区间中左起第num个元素m
tree[u]--;//对访问到的区间维护剩下的牛的个数m
if (tree[u] == 0 && u >= last_left)
return u;
//情况1:找右
if (tree[u << 1] < num)
return query((u << 1) + 1, num - tree[u << 1], last_left);
//情况2:找左
if (tree[u << 1] >= num)
return query(u << 1, num, last_left);
}
int main()
{
int n, last_left;//last_left表示最后一行左边第一个结点的标号
scanf("%d", &n);
pre[1] = 0;
last_left = 1 << (int(log(n) / log(2)) + 1);//二叉树最后一行的最左边一个数的计算方法是找离n最近的2的指数,例如3->4, 4->4, 5->8
for (int i = 2; i <= n; i++)
scanf("%d", &pre[i]);
BuildTree(n, last_left);//n=5个数,last_left=8
for (int i = n; i >= 1; i--)//从后往前推断出每次最后一个数字
ans[i] = query(1, pre[i] + 1, last_left) - last_left + 1;//每次都从根结点开始查起
for (int i = 1; i <= n; i++)
printf("%d\n", ans[i]);
return 0;
}