ZZX and Permutations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 888    Accepted Submission(s): 278

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
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5416 

pid=5415">5415 

pid=5414">5414 

pid=5413">5413 5412 

 

从第1位開始贪心,每位要么取前面的(包含自己)要么取后一个。

每次找到前面能取的最大值。

假设取后面的那么无论,仅仅是以后不能取后一位,

若取前面的则拿走一段

注意反例

6 7 1 2

要用set找出前面没间隔的部分

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<set>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEM2(a,i) memset(a,i,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
class SegmentTree
{
ll a[MAXN*4],minv[MAXN*4],sumv[MAXN*4],maxv[MAXN*4],addv[MAXN*4],setv[MAXN*4];
int n;
public:
SegmentTree(){MEM(a) MEM(minv) MEM(sumv) MEM(maxv) MEM(addv) MEM2(setv,-1) }
SegmentTree(int _n):n(_n){MEM(a) MEM(minv) MEM(sumv) MEM(maxv) MEM(addv) MEM2(setv,-1) }
void mem(int _n)
{
n=_n;
MEM(a) MEM(minv) MEM(sumv) MEM(maxv) MEM(addv) MEM2(setv,-1)
} void maintain(int o,int L,int R)
{ sumv[o]=maxv[o]=minv[o]=0;
if (L<R) //仅仅考虑左右子树
{
sumv[o]=sumv[Lson]+sumv[Rson];
minv[o]=min(minv[Lson],minv[Rson]);
maxv[o]=max(maxv[Lson],maxv[Rson]);
} //仅仅考虑add操作
if (setv[o]>=0) sumv[o]=setv[o]*(R-L+1),minv[o]=maxv[o]=setv[o]; minv[o]+=addv[o];maxv[o]+=addv[o];sumv[o]+=addv[o]*(R-L+1);
} int y1,y2,v;
void update(int o,int L,int R) //y1,y2,v
{
if (y1<=L&&R<=y2) {
addv[o]+=v;
}
else{
pushdown(o);
int M=(R+L)>>1;
if (y1<=M) update(Lson,L,M); else maintain(Lson,L,M);
if (M< y2) update(Rson,M+1,R); else maintain(Rson,M+1,R);
} maintain(o,L,R); }
void update2(int o,int L,int R)
{
if (y1<=L&&R<=y2) {
setv[o]=v;addv[o]=0;
}
else{
pushdown(o);
int M=(R+L)>>1;
if (y1<=M) update2(Lson,L,M); else maintain(Lson,L,M); //维护pushodown,再次maintain
if (M< y2) update2(Rson,M+1,R); else maintain(Rson,M+1,R);
} maintain(o,L,R);
} void pushdown(int o)
{
if (setv[o]>=0)
{
setv[Lson]=setv[Rson]=setv[o];
addv[Lson]=addv[Rson]=0;
setv[o]=-1;
}
if (addv[o])
{
addv[Lson]+=addv[o];
addv[Rson]+=addv[o];
addv[o]=0;
}
}
ll _min,_max,_sum; void query2(int o,int L,int R,ll add)
{
if (setv[o]>=0)
{
_sum+=(setv[o]+addv[o]+add)*(min(R,y2)-max(L,y1)+1);
_min=min(_min,setv[o]+addv[o]+add);
_max=max(_max,setv[o]+addv[o]+add);
} else if (y1<=L&&R<=y2)
{
_sum+=sumv[o]+add*(R-L+1);
_min=min(_min,minv[o]+add);
_max=max(_max,maxv[o]+add);
} else {
// pushdown(o);
int M=(L+R)>>1;
if (y1<=M) query2(Lson,L,M,add+addv[o]);// else maintain(Lson,L,M);
if (M< y2) query2(Rson,M+1,R,add+addv[o]);// else maintain(Rson,M+1,R);
}
//maintain(o,L,R);
} void query(int o,int L,int R,ll add) //y1,y2
{
if (y1<=L&&R<=y2)
{
_sum+=sumv[o]+add*(R-L+1);
_min=min(_min,minv[o]+add);
_max=max(_max,maxv[o]+add);
}
else{
int M=(R+L)>>1;
if (y1<=M) query(Lson,L,M,add+addv[o]);
if (M< y2) query(Rson,M+1,R,add+addv[o]);
}
} void add(int l,int r,ll v)
{
y1=l,y2=r;this->v=v;
update(1,1,n);
}
void set(int l,int r,ll v)
{
y1=l,y2=r;this->v=v;
update2(1,1,n);
}
ll ask(int l,int r,int b=0)
{
_sum=0,_min=INF,_max=-1;
y1=l,y2=r;
query2(1,1,n,0);
// cout<<_sum<<' '<<_max<<' '<<_min<<endl; switch(b)
{
case 1:return _sum;
case 2:return _min;
case 3:return _max;
default:break;
}
}
void print()
{
For(i,n)
cout<<ask(i,i,1)<<' ';
cout<<endl; } //先set后add
}S;
int h[MAXN],n,a[MAXN];
int b[MAXN],ans[MAXN];
set<int> S2;
set<int>::iterator it; int main()
{
// freopen("L.in","r",stdin); int T;cin>>T;
while(T--) {
cin>>n;
For(i,n) b[i]=1;
S.mem(n);
S2.clear();S2.insert(1);
For(i,n) scanf("%d",&a[i]),S.add(i,i,a[i]);
a[0]=a[n+1]=0;
For(i,n) h[a[i]]=i; For(i,n) {
int t=h[i];
if (!b[t]) continue; it=S2.upper_bound(t);
it--;
int l=(*it);
ll premax = S.ask(l,t,3); if (premax>a[t+1]*b[t+1]) {
int t2=h[premax];
Fork(j,t2,t) b[j]=0;
Fork(j,t2,t-1) ans[a[j]]=a[j+1]; ans[a[t]]=a[t2]; S.set(t,t2,0);
S2.insert(t+1); } else {
S.set(t+1,t+1,0);
} } For(i,n-1) printf("%d ",ans[i]);
printf("%d\n",ans[n]); } return 0;
}

HDU 5338(ZZX and Permutations-用线段树贪心)的更多相关文章

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

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

  2. HDU 5338 ZZX AND PERMUTATIONS 线段树

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

  3. 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/O ...

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

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

  5. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  6. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  8. BZOJ_1826_[JSOI2010]缓存交换 _线段树+贪心

    BZOJ_1826_[JSOI2010]缓存交换 _线段树+贪心 Description 在计算机中,CPU只能和高速缓存Cache直接交换数据.当所需的内存单元不在Cache中时,则需要从主存里把数 ...

  9. Bzoj5251 线段树+贪心

    Bzoj5251 线段树+贪心 记录本蒟蒻省选后的第一篇题解!国际惯例的题面:首先这个东西显然是一棵树.如果我们把数值排序,并建立这棵树的dfs序,显然dfs序上的一个区间对应数值的一个区间,且根为数 ...

  10. 2018.10.20 NOIP模拟 蛋糕(线段树+贪心/lis)

    传送门 听说是最长反链衍生出的对偶定理就能秒了. 本蒟蒻直接用线段树模拟维护的. 对于第一维排序. 维护第二维的偏序关系可以借助线段树/树状数组维护逆序对的思想建立权值线段树贪心求解. 代码

随机推荐

  1. vim的使用技巧--模式入门

    vim作为编辑器之神,一直都是程序爱好者的最爱,与一般的编辑器的最大不同就是对模式的把握更加的细腻和得当.普通编辑主要分为使用菜单和使用键盘,菜单就是输入命令作用,键盘主要用来输入文本,中间穿插着使用 ...

  2. FZU2187 回家种地(矩形面积并)

    矩形面积并(只覆盖一次的面积)的裸题.好久没写代码debug了我太久,太辛酸了. #pragma warning(disable:4996) #include <iostream> #in ...

  3. hdu 2654(欧拉函数)

    Become A Hero Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. Spark-RDD之 zip

    zip这个函数是在scala中的,spark中也应用于RDD类型 val posLeftRDD = FreqDic1.map(line => line._1) zip sc.paralleliz ...

  5. 玲珑杯 Round #5 Problem E Tetration (枚举 + 欧拉公式)

    题目链接  Tetration 题意  给定一个排列  现在可以任意调整这个排列的顺序 求$a_{1}^{a_{2}^{a_{3}^{...^{a_{n}}}}}$对$p$取模的最小值 直接枚举$a$ ...

  6. Zabbix 企业Nginx监控

    Zabbix监控Nginx状态 1 修改Nginx配置文件,开启Nginx监控 location /nginx_status { stub_status on; access_log off; all ...

  7. ListView设置某一项item的文本居中

    使用ListView和volley写了一个使用网络获取天气的demo ListView中Item的文本模式都是左侧对齐 我这边需要一些标题文本居中对齐 网上也找不到示例,不过找到了getView这个函 ...

  8. 一次程序bug的排查

    这周准备下一个QA测试的版本,把版本发到测试环境就开始发现各种问题,修修补补搞了一周,总算告一段落了.   分析一下几个bug的问题,都集中在程序模块的整合中.一个模块的一个小的修改,造成另一个模块的 ...

  9. 标签页的切换方法(DOM)

    效果: 1.点击“JAVA语言” 2.点击“C语言” 3.点击C++语言 代码: <!doctype html> <html> <head> <meta ch ...

  10. java中的堆、栈和常量池简介

    一.它们各自存放的数据类型: 堆:存放所有new出来的对象. 栈:存放基本类型的变量数据和对象的应用,对象(new出来的对象)本身并不存在栈中,而是存放在堆中或者常量池中(字符串常量对象存放在常量池中 ...