HDOJ 5338 ZZX and Permutations 線段樹+樹狀數組

來源:互聯網
上載者:User

標籤:


[題意]:

給一個排列加上表示迴圈的括弧,問如何讓1到n的對應的字典序最大.



從1開始貪心每個數字可以往三個地方走,右邊第一個,跳轉到左邊的某一個,和自己構成迴圈


對於走到右邊第一個的情況,只要判斷右邊的那個有沒有被佔據就可以了,如果可以和右邊的互換,那麼需要線上段樹中將右邊的數置為0


跳轉到左邊的某一個,一個數如果跳轉到左邊的某一個則說明左邊的那個是括弧開頭這個數是括弧結尾,用一個線段樹查詢區間裡的最大值,由於括弧間是不能重疊的,所以需要用樹狀數組二分一下左邊界.如果這個數是要跳轉到左邊的某個數,則要對右括弧的位置在樹狀數組裡+1,表示這裡已經有一個完整的括弧了


對於2 1 3 這種情況(2)(1 3)的話得到的字典序最大,所以一個數有時候也是可以跳轉到自己的



ZZX and PermutationsTime Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 775    Accepted Submission(s): 244


Problem DescriptionZZX 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. 
InputFirst 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. 
OutputOutput n space-separated numbers in a line for each testcase.
Don‘t output space after the last number of a line. 
Sample Input
261 4 5 6 3 221 2
 
Sample Output
4 6 2 5 1 32 1
 
AuthorXJZX 
Source2015 Multi-University Training Contest 4 

/* ***********************************************Author        :CKbossCreated Time  :2015年08月03日 星期一 15時51分35秒File Name     :HDOJ5338.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;const int maxn=100100;int n;int nb[maxn],wz[maxn];int sed[maxn<<2];int mx[maxn<<2];#define lrt rt<<1#define rrt rt<<1|1#define lson l,m,lrt#define rson m+1,r,rrtvoid push_up(int rt){mx[rt]=max(mx[lrt],mx[rrt]);}void push_down(int rt){if(sed[rt]){mx[rrt]=mx[lrt]=0;sed[rrt]=sed[lrt]=0;sed[rt]=0;}}void build(int l,int r,int rt){sed[rt]=mx[rt]=0;if(l==r){mx[rt]=nb[l];return ;}int m=(l+r)/2;build(lson); build(rson);push_up(rt);}int query(int L,int R,int l,int r,int rt){if(L<=l&&r<=R) return mx[rt];push_down(rt);int m=(l+r)/2;int ret=0;if(L<=m) ret=max(ret,query(L,R,lson));if(R>m) ret=max(ret,query(L,R,rson));return ret;}void update(int L,int R,int l,int r,int rt){if(L<=l&&r<=R){sed[rt]=1;mx[rt]=0;return ;}push_down(rt);int m=(l+r)/2;if(L<=m) update(L,R,lson);if(R>m) update(L,R,rson);push_up(rt);}void show(int l,int r,int rt){printf("%d: %d <----> %d mx: %d\n",rt,l,r,mx[rt]);if(l==r) return;int m=(l+r)/2;show(lson); show(rson);}int ans[maxn];bool used[maxn];int tree[maxn];inline int lowbit(int x) { return x&(-x); }void add(int p,int v=1){for(int i=p;i<maxn;i+=lowbit(i))tree[i]+=v;}int sum(int p){int ret=0;for(int i=p;i;i-=lowbit(i))ret+=tree[i];return ret;}int get_left(int x){int low=0,high=x-1;int ans=1,mid;int bz=sum(x);if(bz==0) return 0;while(low<=high){mid=(low+high)/2;if(sum(mid)<bz){low=mid+1;}else{ans=mid;high=mid-1;}}return ans;}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int T_T;scanf("%d",&T_T);while(T_T--){memset(tree,0,sizeof(tree));scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",nb+i);wz[nb[i]]=i;ans[i]=0; used[i]=0;}if(n==1){printf("%d\n",nb[1]); continue;}build(1,n,1);for(int i=1;i<=n;i++){if(ans[i]) continue;int left=0,right=0,self=0;/// link to selfif(used[wz[i]]==false) self=i;/// link to right if(wz[i]!=n){int pright=wz[i]+1;if(used[pright]==false){right=nb[pright];}}/// link to leftif(wz[i]!=1){int pleft=get_left(wz[i])+1;left=query(pleft,wz[i],1,n,1);}if(left>self||right>self){if(right>left){ans[i]=right;used[wz[right]]=true;update(wz[right],wz[right],1,n,1);}else{ans[i]=left;/// link circlefor(int j=wz[left];j<wz[i];j++){ans[nb[j]]=nb[j+1];used[j]=true;}used[wz[i]]=true;/// set markadd(wz[i]);}}else{ans[i]=self;used[wz[i]]=true;add(wz[i]);}}for(int i=1;i<=n;i++)printf("%d%c",ans[i],(i==n)?10:32);}        return 0;}




著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

HDOJ 5338 ZZX and Permutations 線段樹+樹狀數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.