HDU 5592 ZYB's Premutation (线段树,构造序列满足前缀逆序对数目)

ZYB’s Premutation

ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ask you to
restore the premutation.

Pair (i,j)(i<j) is considered as a reverse log if Ai>Aj is matched.
Input
In the first line there is the number of testcases T.

For each teatcase:

In the first line there is one number N.

In the next line there are N numbers Ai,describe the number of the reverse logs of each prefix,

The input is correct.

1≤T≤5,1≤N≤50000
Output
For each testcase,print the ans.
Sample Input
1
3
0 1 2
Sample Output
3 1 2

题意: 给出前缀逆序对的数组A,构造一个序列,满足这个条件。

思路: 从后往前进行构造,初始时就是数字 1 ~ n。易知, A [ i ] − A [ i − 1 ] A[i] - A[i - 1] A[i]A[i1] 得到的就是 位置 i i i 的数对整个 i i i 前缀逆序对数目的贡献,那么有 A [ i ] − A [ i − 1 ] + 1 A[i] - A[i - 1] + 1 A[i]A[i1]+1 得到的就是 i i i 位置是当前未用数字的第几大,利用权值线段树维护这个东西即可。

Code:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define ptch putchar
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;
inline LL read() {
    LL s = 0,w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') w = -1;
        ch = getchar();
    }
    while(isdigit(ch))
        s = s * 10 + ch - '0',ch = getchar();
    return s * w;
}
inline void write(LL x) {
    if(x < 0)
        putchar('-'), x = -x;
    if(x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
#ifndef ONLINE_JUDGE
    clock_t prostart = clock();
#endif

const int maxn = 5e4 + 10;
int a[maxn],ans[maxn];
int sum[maxn << 2];

void build(int l,int r,int i){
    if(l == r){
        sum[i] = 1;
        return ;
    }
    int mid = (l + r) >> 1;
    build(l,mid,ls);
    build(mid + 1,r,rs);
    sum[i] = sum[ls] + sum[rs];
}

void update(int l,int r,int pos,int i){
    if(l == r){
        -- sum[i];
        return ;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid) update(l,mid,pos,ls);
    if(pos > mid) update(mid + 1,r,pos,rs);
    sum[i] = sum[ls] + sum[rs];
}

int query(int l,int r,int i,int k){
    if(l == r) return l;
    int mid = (l + r) >> 1;
    if(sum[rs] >= k) return query(mid + 1,r,rs,k);
    else return query(l,mid,ls,k - sum[rs]);
}

int main() {
#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif

    int t = read();
    while(t --){
        int n = read();
        build(1,n,1);
        for(int i = 1;i <= n;++ i) a[i] = read();
        for(int i = n;i >= 1;-- i){
            int k = a[i] - a[i - 1] + 1;
            ans[i] = query(1,n,1,k);
            update(1,n,ans[i],1);
        }

        for(int i = 1;i <= n;++ i){
            write(ans[i]);
            ptch(i == n ? '\n' : ' ');
        }
    }

#ifndef ONLINE_JUDGE
    cout << "time: " << 1.0 * (clock() - prostart) / CLOCKS_PER_SEC << " s" << endl;
#endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值