2015 Multi-University Training Contest 4 hdu 5338 ZZX and Permutations
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
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.
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.
Don't output space after the last number of a line.
#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的更多相关文章
- HDU 5338 ZZX AND PERMUTATIONS 线段树
pid=5338" target="_blank" style="text-decoration:none; color:rgb(45,125,94); bac ...
- hdu 5338 ZZX and Permutations (贪心+线段树+二分)
ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- 线段树+树状数组+贪心 HDOJ 5338 ZZX and Permutations
题目传送门 /* 题意:不懂... 线段树+树状数组+贪心:贪心从第一位开始枚举,一个数可以是循环节的末尾或者在循环节中,循环节(循环节内部是后面的换到前面,最前面的换到最后面).线段树维护最大值,树 ...
- 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 ...
- 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: ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Ajax json jquery实现菜单案例
需求: 运用AJAX请求文件menu.json,配置菜单栏,并实现以下功能点: 1. 点击向左箭头,菜单向左移动,隐藏 2. 点击向右箭头,菜单向右移动,显示3. 点击一级菜单,被点击菜单的子菜单显示 ...
- validate命令---rman进行备份和回复的验证
rman作为oracle备份与恢复工具,为我们提供了强大的功能.当中包含对数据文件的物理和逻辑检測以及备份文件的有效性检測. 首先.来看一下rman对数据文件的检測. 我们知道,rman在备份数据时, ...
- 可编程数据平面将OpenFlow扩展至电信级应用(二)
可编程数据平面将OpenFlow扩展至电信级应用(二) 案例:基于WinPath网络处理器的电信极OpenFlow (CG-OF)client实现 作者:Liviu Pinchas, Tao Lang ...
- 项目记录22-- tolua基于lua框架事件派发
每天晚上抽点时间写一点点就一点点,曾经不写博客可是如今.不为别的仅仅是为了告诉别人我还存在. 这几天在地铁上发现好多人都还在玩消除游戏,今天起码看到5个人,可是玩的版本号都不一样.看 ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
- 重构版机房收费系统之分层、接口、数据库连接、反射+工厂(vb.net)
分层 分层是为了减少层与层之间的依赖,添加程序的可读性,让整个系统结构清晰明白.还可大大减少维护成本,可是分层也有一定的缺点,有些能够直接訪问数据库的层,却要通过负责訪问数据库的层进行訪问.这样,在訪 ...
- poj--1383--Labyrinth(树的直径)
Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4062 Accepted: 1529 Descrip ...
- CodeForcess--609B--The Best Gift(模拟水题)
The Best Gift Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submi ...
- [HDU 5542] The Battle of Chibi
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5542 [算法] 树状数组优化DP [代码] #include<bits/stdc++.h&g ...
- 【POJ 2482】 Stars in Your Windows
[题目链接] http://poj.org/problem?id=2482 [算法] 线段树 + 扫描线 [代码] #include <algorithm> #include <bi ...