您的位置:首页 > 其它

hdu 5338 ZZX and Permutations 2015多校联合训练赛,贪心,线段树,树状数组

2015-07-30 19:29 471 查看


ZZX and Permutations

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 141 Accepted Submission(s): 24



Problem Description

ZZX likes permutations.

ZZX knows that a permutation can be decomposed into disjoint cycles(see https://en.wikipedia.org/wiki/Permutation#Cycle_notation). For example:

145632=(1)(35)(462)=(462)(1)(35)=(35)(1)(462)=(246)(1)(53)=(624)(1)(53)……

Note that there are many ways to rewrite it, but they are all equivalent.

A cycle with only one element is also written in the decomposition, like (1) in the example above.

Now, we remove all the parentheses in the decomposition. So the decomposition of 145632 can be 135462,462135,351462,246153,624153……

Now you are given the decomposition of a permutation after removing all the parentheses (itself is also a permutation). You should recover the original permutation. There are many ways to recover, so you should find the one with largest lexicographic order.



Input

First line contains an integer t,
the number of test cases.

Then t testcases
follow. In each testcase:

First line contains an integer n,
the size of the permutation.

Second line contains n space-separated
integers, the decomposition after removing parentheses.

n≤105.
There are 10 testcases satisfying n≤105,
200 testcases satisfying n≤1000.



Output

Output n space-separated
numbers in a line for each testcase.

Don't output space after the last number of a line.



Sample Input

2
6
1 4 5 6 3 2
2
1 2




Sample Output

4 6 2 5 1 3
2 1




Source

2015 Multi-University Training Contest 4

题目:

很复杂。不想说

:解法

贪心,从1到n确定该位置能放的最大的数

对于每个位置,只能是后一个数作为自己的后继,或者,在这个位置之前没有被用过的数才能作为后继。

但是:必须从前面没有匹配括号的地方去找最大值

比如

5 4 1 3 2

找1的时候发现了5那么(5,4,1)就是一个循环了。找2的时候只能从3的位置开始

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxm 400007
#define maxn 100011

int tree[maxn];
int Tquery(int p){
    int ans = 0;
    while(p > 0){
        if(ans < tree[p])
            ans = tree[p];
        p -= (p&(-p));
    }
    return ans;
}
int flag;
int Tadd(int p,int x){
    while(p < flag){
        if(tree[p] < x)
            tree[p] = x;
        p += (p&(-p));
    }
    return 0;
}

int lc[maxm],rc[maxm],val[maxm];
int cnt ;
void init(){
    cnt = 1;
    lc[0] = rc[0] = val[0] = 0;
}
int num[maxm];
int pos[maxm];
void update(int u){
    val[u] = max(val[lc[u]],val[rc[u]]);
}
void build(int u,int l,int r){
    lc[u] = rc[u] = val[u] = 0;
    if(l == r){
        val[u] = num[l];
        return ;
    }
    lc[u] = cnt++;
    rc[u] = cnt++;
    int mid = (l+r)/2;
    build(lc[u],l,mid);
    build(rc[u],mid+1,r);
    update(u);
}
//查询区间最大值
int query(int u,int l,int r,int L,int R){
    if(val[u] == 0) return 0;
    if(l == L && r == R) return val[u];
    int mid = (l+r)/2;
    if(mid >= R) return query(lc[u],l,mid,L,R);
    else if(mid < L) return query(rc[u],mid+1,r,L,R);
    return max(query(lc[u],l,mid,L,mid),query(rc[u],mid+1,r,mid+1,R));
}
//将一个结点置为0
void del(int u,int l,int r,int p){
    if(l == r){
        val[u] = 0;
        return ;
    }
    int mid = (l+r)/2;
    if(p > mid)
        del(rc[u],mid+1,r,p);
    else del(lc[u],l,mid,p);
    update(u);
}
//删除一个区间的结点,置为0
void delx(int u,int l,int r,int L,int R){
    if(l == L && r == R) {
        val[u] = 0;
        return ;
    }
    int mid = (l+r)/2;
    if(mid >= R)  delx(lc[u],l,mid,L,R);
    else if(mid < L)  delx(rc[u],mid+1,r,L,R);
    else delx(lc[u],l,mid,L,mid),delx(rc[u],mid+1,r,mid+1,R);
}

int ans[maxn];
int check[maxn];
int ok[maxn];

int main(){
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        num[0] = 0;
        for(int i = 1;i <= n;i++) {
            scanf("%d",&num[i]);
            pos[num[i]] = i;
        }
        flag = n+10;
        init();
        cnt++;
        build(1,0,n);
        if(n > 1000){
            memset(check,0,sizeof(check));
            memset(ok,0,sizeof(ok));
            memset(tree,0,sizeof(tree));
        }
        else {
            for(int i = 0;i <= flag; i++)
                tree[i] = check[i] = ok[i] = 0;
        }

        int u,v;
        for(int i = 1;i <= n; i++){
            if(ok[i]) continue;
            ok[i] = 1;

            int p = pos[i];
            if(p != n && check[p+1] == 0)
                u = num[p+1];
            else u = 0;

            int be = Tquery(p);
            int v = query(1,0,n,be+1,p);
            if(u > v){
                ans[i] = u;
                check[p+1] = 1;
                del(1,0,n,p+1);
            }
            else if(v > 0){
                ans[i] = v;
                check[p] = 1;

                int q = pos[v];
                for(int j = q; j < p; j++){
                    if(!ok[num[j]]){
                        ok[num[j]] = 1;
                        ans[num[j]] = num[j+1];
                    }
                    check[j] = 1;
                }
                delx(1,0,n,q,p);
                Tadd(q,p);
            }
        }
        for(int i = 1;i <= n; i++){
            if(i!=1)printf(" %d",ans[i]);
            else printf("%d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}
/*
4
4
3 4 2 1

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: