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 ...
随机推荐
- Elasticsearch 三种分页方式
from + size 浅分页 "浅"分页可以理解为简单意义上的分页.它的原理很简单,就是查询前20条数据,然后截断前10条,只返回10-20的数据.这样其实白白浪费了前10条的查 ...
- BA-siemens-apogee总线不稳定解决方法
状况一:BLN下的火车头在线,但是下面的模块(包括UEC或者PPM)全部掉线 尝试方法: 使用挨个DDC箱断线的方法测试总线是否上线(可以解决由于总线短路引起的总线故障,施工中总线压冷压端子的话就不容 ...
- POSIX 线程编程(二)线程建立与终止
创建与终止线程 线程的管理常用的API有: pthread_create(thread,attr,start_routine,arg) pthread_exit(status) pthread_can ...
- 简单易学的机器学习算法——神经网络之BP神经网络
一.BP神经网络的概念 BP神经网络是一种多层的前馈神经网络,其基本的特点是:信号是前向传播的,而误差是反向传播的.详细来说.对于例如以下的仅仅含一个隐层的神经网络模型: watermark/ ...
- ASPX和Razor
ASPX ASPX文件是微软的在server端运行的动态网页文件,通过IIS解析运行后能够得到动态页面,是微软推出的一种新的网络编程方法,而不是ASP的简单升级,由于它的编程方法和ASP有非常大的不同 ...
- 用opencv实现的PCA算法,非API调用
理论參考文献:但此文没有代码实现.这里自己实现一下,让理解更为深刻 问题:如果在IR中我们建立的文档-词项矩阵中,有两个词项为"learn"和"study",在 ...
- unity3d教程运行物理机制
首先,我们将把Hooke定律写Euler方法结合在一起找到新坐标.加速和速度. Hooke定律是F=kx,这里的F是指由水流产生的力(记住,我们将把水体表面模拟为水流),k是指水流的常量.x则是位移. ...
- h5-弹出层layer,提示,顶部横条,
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYkAAAI7CAIAAACWVfAJAAAgAElEQVR4nOy9f1ATWb733z3uOA4kIC ...
- Linux操作系统下Oracle主要监控工具介绍
Oracle监控包括有效且完全地监控Oracle数据库的性能.可用性和使用率等统计量,还包括即时的错误通知和纠正措施,并提供全面的报表和图表.本文中主要介绍几种Linux操作系统下Oracle主要监控 ...
- python面向对象与结构成员之间的关系
1面向对象结构分析:----面向对象整体大致分为两块区域:-------第一部分:静态字段(静态变量)部分-------第二部分:方法部分--每个区块可以分为多个小部分 class A: countr ...