hdoj 5592 ZYB's Premutation 【线段树插空】

本文介绍了一种通过已知逆序对数量来恢复原始排列的方法。利用线段树进行插空操作,逆向构建出原全排列序列。适用于解决特定算法问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ZYB's Premutation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 31    Accepted Submission(s): 8


Problem Description
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.

1T5,1N50000
 

Output
For each testcase,print the ans.
 

Sample Input
1 3 0 1 2
 

Sample Output
3 1 2
 



题意:给定1-n某一个全排列的前缀区间逆序对个数,让你还原这个序列。


思路:线段树插空,逆向跑一遍就ok了。


AC代码:


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (50000+10)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
int a[MAXN], b[MAXN];
struct Tree
{
    int l, r, len;
    int sum;
};
Tree tree[MAXN<<2];
void PushUp(int o){
    tree[o].sum = tree[ll].sum + tree[rr].sum;
}
void Build(int o, int l, int r)
{
    tree[o].l = l; tree[o].r = r;
    tree[o].len = r - l + 1;
    tree[o].sum = 1;
    if(l == r)
        return ;
    int mid = (l + r) >> 1;
    Build(lson); Build(rson);
    PushUp(o);
}
int Query(int o, int v)
{
    if(tree[o].l == tree[o].r)
        return tree[o].l;
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(tree[ll].sum >= v)
        return Query(ll, v);
    else
        return Query(rr, v-tree[ll].sum);
}
void Update(int o, int P)
{
    if(tree[o].l == tree[o].r)
    {
        tree[o].sum = 0;
        return ;
    }
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(P <= mid)
        Update(ll, P);
    else
        Update(rr, P);
    PushUp(o);
}
int main()
{
    int t; Ri(t);
    W(t)
    {
        int n; Ri(n);
        for(int i = 1; i <= n; i++)
            Ri(a[i]);
        a[0] = 0; Build(1, 1, n);
        for(int i = n; i >= 1; i--)
        {
            int num = a[i] - a[i-1];
            int P = Query(1, num+1);
            //Pi(P);
            Update(1, P);
            b[i] = n-P+1;
        }
        for(int i = 1; i <= n; i++)
        {
            if(i > 1) printf(" ");
            printf("%d", b[i]);
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值