ZZX and Permutations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 771    Accepted Submission(s): 243

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
 
Author
XJZX
 
Source
 
解题:线段树+set
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct node {
int lt,rt,lazy,maxv;
} tree[maxn<<];
int d[maxn],val2index[maxn],n;
void pushup(int v) {
tree[v].maxv = max(tree[v<<].maxv,tree[v<<|].maxv);
}
void pushdown(int v) {
if(tree[v].lazy > -) {
tree[v<<].lazy = tree[v<<|].lazy = tree[v].lazy;
tree[v<<].maxv = tree[v<<|].maxv = tree[v].lazy;
tree[v].lazy = -;
}
}
void build(int lt,int rt,int v) {
tree[v].lt = lt;
tree[v].rt = rt;
tree[v].lazy = -;
if(lt == rt) {
tree[v].maxv = d[lt];
return;
}
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
pushup(v);
}
void update(int lt,int rt,int v) {
if(lt <= tree[v].lt && rt >= tree[v].rt) {
tree[v].lazy = tree[v].maxv = ;
return;
}
pushdown(v);
if(lt <= tree[v<<].rt) update(lt,rt,v<<);
if(rt >= tree[v<<|].lt) update(lt,rt,v<<|);
pushup(v);
}
int query(int lt,int rt,int v) {
if(lt <= tree[v].lt && rt >= tree[v].rt) return tree[v].maxv;
pushdown(v);
int ret = ;
if(lt <= tree[v<<].rt) ret = max(ret,query(lt,rt,v<<));
if(rt >= tree[v<<|].lt) ret = max(ret,query(lt,rt,v<<|));
pushup(v);
return ret;
}
set<int>st;
bool used[maxn];
int ret[maxn];
int main() {
int kase;
scanf("%d",&kase);
while(kase--) {
st.clear();
memset(used,false,sizeof used);
memset(ret,,sizeof ret);
memset(d,,sizeof d);
scanf("%d",&n);
for(int i = ; i <= n; ++i) {
scanf("%d",d+i);
val2index[d[i]] = i;
}
build(,n,);
st.insert();
for(int i = ; i <= n; ++i) {
if(ret[i]) continue;
int index = val2index[i],mx = ;
if(!used[d[index+]]) mx = max(d[index+],mx);
auto it = st.lower_bound(index);
if(it != st.begin()) --it;
int val = query((*it),index,);
mx = max(mx,val);
if(mx == d[index+]) {
used[d[index+]] = true;
ret[i] = d[index+];
update(index+,index+,);
continue;
}
ret[i] = mx;
for(int i = val2index[mx]; i < index; ++i) {
ret[d[i]] = d[i+];
used[d[i]] = true;
}
update(val2index[mx],index,);
used[d[index]] = true;
for(int i = val2index[mx]; i <= index; ++i) st.insert(i);
}
for(int i = ; i <= n; ++i)
printf("%d%c",ret[i],i==n?'\n':' ');
}
return ;
}
 

2015 Multi-University Training Contest 4 hdu 5338 ZZX and Permutations的更多相关文章

  1. HDU 5338 ZZX AND PERMUTATIONS 线段树

    pid=5338" target="_blank" style="text-decoration:none; color:rgb(45,125,94); bac ...

  2. hdu 5338 ZZX and Permutations (贪心+线段树+二分)

    ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...

  3. 线段树+树状数组+贪心 HDOJ 5338 ZZX and Permutations

    题目传送门 /* 题意:不懂... 线段树+树状数组+贪心:贪心从第一位开始枚举,一个数可以是循环节的末尾或者在循环节中,循环节(循环节内部是后面的换到前面,最前面的换到最后面).线段树维护最大值,树 ...

  4. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  5. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  6. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  7. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  8. 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  9. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

随机推荐

  1. jQuery(事件对象)

    冒泡与阻止冒泡 阻止默认行为

  2. Tensorflow MNIST 数据集测试代码入门

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50614444 测试代码已上传至GitH ...

  3. 为什么网络银行不支持GNU/Linux操作系统下的浏览器操作

    当年Linux没出时.银行就開始信息化建设了. 所为信息化,就是指用计算机工作了.服务客户了. 顺带着,慢慢的建server,连网(内部网).外网(网上银行) 这样下来, unix, dos, win ...

  4. wpf 全局异常捕获处理

    /// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : Application { ...

  5. iOS_第3方类库MBprogressHUD

    1,将下载好的第3方类库MBprogressHUD源代码包增加到project(事实上就是一个.h和.m文件) 2,进入project的Build Phases,将源代码包里面的所有.m文件所有加入到 ...

  6. 英语发音规则---N字母

    英语发音规则---N字母 一.总结 一句话总结: 1.位于词尾的n在m后面时不发音? autumn /'ɔːtəm/ n. 秋天 column /'kɒləm/ n. 纵队 2.在音素/k//g/前面 ...

  7. 【HNOI 2004】宠物收养所

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1208 [算法] 建两棵平衡树维护领养者和宠物的特点值,这两棵平衡树支持 插入删除,查 ...

  8. Jquery 随笔

    jQuery中 遍历 var arr = ['a','b','c'];    $.each(arr,function(k,v){    console.log(k); //键 console.log( ...

  9. 解决有关null闪退及json解析数据中null的问题

    程序在获取某些数据之后莫名崩溃.其实很早就发现了原因: 由于服务器的数据库中有些字段为空, 然后以Json形式返回给客户端时就会出现这样的数据: "somevalue":null ...

  10. Windows2003 安装MVC4 环境的步骤

    一.作为部署服务器的安装步骤 1.服务器上安装SP2 和 IIS6 2.安装.Net Framework3.5 SP1(完整安装包,包含2.0 2.0SP1,237MB那个安装包) 3.安装.Net ...