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. String 字符串的追加,数组拷贝

    package chengbaoDemo; import java.util.Arrays; /** *需求:数组的扩容以及数据的拷贝 *分析:因为String的实质是以字符数组存储的,所以字符串的追 ...

  2. ElasticSearch 深度分页解决方案

    常见深度分页方式 from+size 另一种分页方式 scroll scroll + scan search_after 的方式 es 库 scroll search 的实现 常见深度分页方式 fro ...

  3. 跳出$.each()循环

    return false:将停止循环 ,跳出eachreturn true:跳至下一个循环(就像在普通的循环中使用'continue').

  4. ASP.NET - 单元测试

    Assert类的使用 Assert.Inconclusive() 表示一个未验证的测试: Assert.AreEqual() 测试指定的值是否相等,如果相等,则测试通过: AreSame() 用于验证 ...

  5. HDU 1757

    简单的矩阵构造题,参看我前几篇的谈到的矩阵的构造法. #include <iostream> #include <cstdio> #include <cstring> ...

  6. Eclipse中高效的快捷键、调试及Junit

    Eclipse中高效的快捷键 当我知道了这些快捷键的用法之后,感觉真的非常兴奋,没想到Eclipse中还有这么多令人惊喜的功能,真的能够提高效率. 内容提示 Alt+/ 用于输入标准库或者keywor ...

  7. 浅谈cocos2dx(17) 中单例管理模式

    ----我的生活,我的点点滴滴!. 首先明白一个问题.什么是管理者模式,管理类是用来管理一组相关对象的类,他提供了訪问对象的接口,假设这么说比較抽象的话.我们来看下cocos2dx中都有哪些类是管理类 ...

  8. angularjs1-3,工具方法,bootstrap,多个module,引入jquery

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. TS3

    let [first, ...rest] = [1, 2, 3, 4]; console.log(first); // outputs 1 console.log(rest); // outputs ...

  10. BZOJ:3441 乌鸦喝水

    bzoj:3441 乌鸦喝水 题目传送门 Description 一只乌鸦在自娱自乐,它在面前放了n个有魔力的水缸,水缸里装有无限的水. 他准备从第1个水缸飞到第n个水缸,共m次.在飞过一个水缸的过程 ...