一个菜菜的蜕变之路?

时间是个常数,也是个变数

Poj Lost Cows___树状数组+二分

题目大意:

N1N
NAiAii1

2N8000

分析:

可以发现,
a[n]+1
1Na[n]+1a[n1]+1
i
a[i]+1
1N01c
i
01a[i]+1
xxi
01x0

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define N 100005

using namespace std;

int Sum[N], f[N], a[N], n;

void Insert(int x, int num) {
    for (; x <= n ; x += x & (-x)) Sum[x] += num;   
}

int Get_Sum(int x) {
    int Answer = 0;
    for (; x ; x -= x & (-x)) Answer += Sum[x];
    return Answer;
}

int main() {
    scanf("%d", &n);
    for (int i = 2; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) Insert(i, 1);
    f[n] = a[n] + 1;
    Insert(f[n], - 1);

    for (int i = n - 1; i >= 1; i--) {
         int l = 1, r = n, ans = 0;
         while (l <= r) {
                int mid = (l + r) >> 1;
                int cp = Get_Sum(mid);
                if (cp >= a[i] + 1) ans = mid, r = mid - 1;
                               else l = mid + 1;  
         }
         f[i] = ans; 
         if (f[i] > 0) Insert(f[i], - 1);
    }
    for (int i = 1; i <= n; i++) printf("%d\n", f[i]);
    return 0;
}
阅读更多
版权声明:欢迎借鉴,谢绝抄搬。 https://blog.csdn.net/Gx_Man_VIP/article/details/80695348
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭