线段树-Difficult Lost Cows

总时间限制: 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

【分析】

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

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

一种思路是用一个数组,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);
    else
        rt = 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,在递归函数的参数中体现即可。

猜你喜欢

转载自blog.csdn.net/w112348/article/details/109036190