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. 洛谷——P1968 美元汇率

    https://www.luogu.org/problem/show?pid=1968#sub 题目背景 此处省略maxint+1个数 题目描述 在以后的若干天里戴维将学习美元与德国马克的汇率.编写程 ...

  2. 洛谷 1775. [国家集训队2010]小Z的袜子

    1775. [国家集训队2010]小Z的袜子 ★★★   输入文件:hose.in   输出文件:hose.out   简单对比时间限制:1 s   内存限制:512 MB [题目描述] 作为一个生活 ...

  3. 【WaaCaa】一款开源科学作图/数据可视化工具 —— 诞生篇

    作为一个理工男.用过了形形色色能够用于科学作图/数据可视化软件:从大学时做实验课推荐用于分析简单採集数据的 Origin; 毕业论文时用来呈现实验时序信号和离线分析脑电信号的 MATLAB.后面还发现 ...

  4. Swift - 制作一个在线流媒体音乐播放器(使用StreamingKit库)

    在之前的文章中,我介绍了如何使用 AVPlayer 制作一个简单的音乐播放器(点击查看1.点击查看2).虽然这个播放器也可以播放网络音频,但其实际上是将音频文件下载到本地后再播放的. 本文演示如何使用 ...

  5. 机器学习 数据量不足问题----1 做好特征工程 2 不要用太多的特征 3 做好交叉验证 使用线性svm

    来自:https://www.zhihu.com/question/35649122 其实这里所说的数据量不足,可以换一种方式去理解:在维度高的情况下,数据相对少.举一个特例,比如只有一维,和1万个数 ...

  6. 27.Qt时钟

    myclock.h #ifndef MYCLOCK_H #define MYCLOCK_H #include <QObject> #include <QLCDNumber> # ...

  7. B/S发布到服务器

    域名准备好了?准备好就开始跟我操作吧: 1:预先在项目的同目录下新建文件夹 Public 2:找到项目解决方案重新生成 3:项目右击 发布 到 Public 4: 登入服务器 打开 Internet管 ...

  8. 洛谷P3704 [SDOI2017]数字表格

    题目描述 Doris刚刚学习了fibonacci数列.用f[i]f[i] 表示数列的第ii 项,那么 f[0]=0f[0]=0 ,f[1]=1f[1]=1 , f[n]=f[n-1]+f[n-2],n ...

  9. Linux下安装桌面

    1.         安装之前先测试是否有桌面 2.         建立yum源文件   3.         挂载好光盘(/rhel自己创建)   4.         使用yum list 查看 ...

  10. 手机号获取省份,城市api的使用

    function get_mobile_area($mobile){ header('Content-Type:text/html;charset=gbk'); $url = 'http://life ...