码迷,mamicode.com
首页 > 编程语言 > 详细

POJ2182 Lost Cows (树状数组+二分)

时间:2020-07-06 17:59:57      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:ring   sample   display   数组初始化   rand   tin   lin   instead   tab   

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

2

4

5

3

1

思路蓝书已经说得比较清楚了。大致就是从后往前确定。首先最后一头牛的身高必定等于An + 1,倒数第二头牛的身高要么等于An-1 + 1(当An-1 < An 时),要么等于An-1 + 2 (当An-1 >= An时),因为每头牛的身高都是互异的。以此类推,如果第k头牛前有Ak头牛身高比他低,那么它的身高就是1~n中第Ak + 1小的没有在已经确定的身高中出现的数,能看出来比较关键的思想就是避开已经存在的身高。由于身高都在1~n范围内且互不相同,因此可以开辟一个长度为n的01数组,初始化为1,然后从后往前遍历Ai,查询第Ai + 1个1的位置作为当前牛的身高,然后在树状数组中把这个位置归零即可。

注意数组初始化要用modify。

 

#include<cstdio>
#include<cstring>
#include<iostream>
#define N 8005
using namespace std;
int n, a[N], b[N],ans[N];
void modify(int x, int y)
{
    for(; x <= n; x += x & (-x))
    {
        b[x] += y;
    }
}
int ask(int x)
{
    int ans=0;
    for(; x; x -= x & -x)
    {
        ans += b[x];
    }
    return ans;
}
int check(int mid, int num)//二分的check函数  
{
    int temp = ask(mid);
    if(temp < num) return 1;//mid位置的前缀和小于a[i] + 1的话需要使左端点向右靠拢 
    else return 0;
}
int main()
{
    cin>>n;
    int i;
    a[1] = 0;
    modify(1, 1);//对于树状数组一定要用modify进行初始化!! 
    for(i=2; i <= n; i++)
    {
        cin>>a[i];
        modify(i, 1);
    }
    for(i = n; i >=  1; i--)
    {
        int l=1,r=n,mid;
        while(l < r)
        {
            mid = (l + r) >> 1;
            int temp = check(mid, a[i] + 1);
            if(temp == 0) r = mid;
            else l = mid + 1;
        }
        ans[i] = r;//更新答案 
        modify(r, -1);//使这个位置归零 
    }
    for(i = 1; i <= n; i++) cout << ans[i] << endl;
    return 0;
}

 

POJ2182 Lost Cows (树状数组+二分)

标签:ring   sample   display   数组初始化   rand   tin   lin   instead   tab   

原文地址:https://www.cnblogs.com/lipoicyclic/p/13256013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!