转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

ZZX and Permutations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 181    Accepted Submission(s): 38

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

和题解的解法几乎一致,然而却在比赛结束后才意识到自己跪在了一个傻逼的地方,真是2333333

贪心,先放第一个位置,放尽可能大的值,然后依次往后,对于每个值,看其后面的一个位置,或者前面没有使用过的连续区间

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
int Scan() {
int res=, ch;
while(ch=getchar(), ch<''||ch>'');
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return res;
}
void Out(int a) {
if(a>)
Out(a/);
putchar(a%+'');
}
int a[];
int vis[];
int pos[];
int fa[];
int dp[];
int ans[];
set<int> ms;
int n;
set<int>::IT it;
int find(int x){
if(fa[x]!=x)fa[x] = find(fa[x]);
return fa[x];
}
void push_up(int cur){
dp[cur] = max(dp[cur<<],dp[cur<<|]);
}
void build(int l,int r,int cur){
if(l==r){
dp[cur] = a[l];
return;
}
int mid = (l+r)>>;
build(l,mid,cur<<);
build(mid+,r,cur<<|);
push_up(cur);
}
void update(int l,int r,int cur,int x,int inc){
if(l==r&&l==x){
dp[cur] = inc;
return;
}
int mid = (l+r)>>;
if(x<=mid)update(l,mid,cur<<,x,inc);
else update(mid+,r,cur<<|,x,inc);
push_up(cur);
}
void update(int x,int num){
update(,n,,x,num);
}
int query(int l,int r,int lx,int rx,int cur){
if(l>=lx&&r<=rx)return dp[cur];
if(lx>r||rx<l)return ;
int mid = (l+r)>>;
return max(query(l,mid,lx,rx,cur<<),query(mid+,r,lx,rx,cur<<|));
}
int query(int l,int r){
return query(,n,l,r,);
}
int main(){
int t;
t = Scan();
while(t--){
n = Scan();
for(int i = ;i<=n;i++){
a[i] = Scan();
vis[i] = ;
pos[a[i]] = i;
fa[i] = i;
}
for(int i=;i<=*n;i++)dp[i] = ;
build(,n,);
ms.clear();
ms.insert();
ms.insert(-INF);
for(int i=;i<=n;i++){
if(vis[i])continue;
int l;
int posi = pos[i];
int maxx = find(i);
int p =pos[maxx];
if(posi+<=n&&!vis[a[posi+]]){
if(a[posi+]>maxx){
p = posi+;
maxx = a[p];
}
}
if(posi>){
it = ms.upper_bound(-posi);
l = *it;
l = -l;
l++;
int tmp = query(l,posi);
//tmp = find(tmp);
//cout<<l<<" "<<tmp<<endl;
if(tmp>maxx){
maxx = tmp;
p = pos[tmp];
}
}
if(p==posi){
ans[i] = i;
vis[i] = ;
ms.insert(-posi);
}else if(p==posi+){
fa[maxx] = fa[i];
//ans[posi] = maxx;
update(p,);
}else{
ans[i] = a[p];
vis[a[p]] = ;
for(int j=p+;j<=posi;j++){
ans[a[j-]] = a[j];
vis[a[j]] = ;
}
ms.insert(-posi);
}
/* cout<<i<<" "<<p<<" "<<maxx<<endl;
for(int i=1;i<=n;i++){
cout<<ans[i]<<" ";
}
cout<<endl;*/
}
for(int i=;i<=n;i++){
if(i!=)putchar(' ');
Out(ans[i]);
}
puts("");
}
return ;
}

hdu5338 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. hdu5338 ZZX and Permutations

    hdu5338 ZZX and Permutations 非原创,来自多校题解 不是自己写的,惭愧ing…… 留着以后自己参考…… lower_bound {1,2,4,5} 询问 2,返回的是 2 ...

  3. BZOJ4391 High Card Low Card [Usaco2015 dec](贪心+线段树/set库

    正解:贪心+线段树/set库 解题报告: 算辣直接甩链接qwq 恩这题就贪心?从前往后从后往前各推一次然后找一遍哪个地方最大就欧克了,正确性很容易证明 (这里有个,很妙的想法,就是,从后往前推从前往后 ...

  4. 【题解】P1712 [NOI2016]区间(贪心+线段树)

    [题解]P1712 [NOI2016]区间(贪心+线段树) 一个observe是,对于一个合法的方案,将其线段长度按照从大到小排序后,他极差的来源是第一个和最后一个.或者说,读入的线段按照长度分类后, ...

  5. Codeforces 675E Trains and Statistic(DP + 贪心 + 线段树)

    题目大概说有n(<=10W)个车站,每个车站i卖到车站i+1...a[i]的票,p[i][j]表示从车站i到车站j所需买的最少车票数,求所有的p[i][j](i<j)的和. 好难,不会写. ...

  6. poj 2010 Moo University - Financial Aid (贪心+线段树)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...

  7. Codeforces 626G Raffles(贪心+线段树)

    G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...

  8. UVALive 8519 Arrangement for Contests 2017西安区域赛H 贪心+线段树优化

    题意 等价于给一个数列,每次对一个长度为$K$的连续区间减一 为最多操作多少次 题解: 看样例猜的贪心,10分钟敲了个线段树就交了... 从1开始,找$[i,i+K]$区间的最小值,然后区间减去最小值 ...

  9. BZOJ1828[USACO 2010 Mar Gold 2.Barn Allocation]——贪心+线段树

    题目描述 输入 第1行:两个用空格隔开的整数:N和M * 第2行到N+1行:第i+1行表示一个整数C_i * 第N+2到N+M+1行: 第i+N+1行表示2个整数 A_i和B_i 输出 * 第一行: ...

随机推荐

  1. jquery1.9学习笔记 之选择器(基本元素五)

    多种元素选择器  jQuery("selector1,selector2,selectorN") 例子: <!doctype html> <html lang=' ...

  2. lazy loading img 图片延迟加载

    http://yunpan.cn/cVsjPW6dgbcsh (提取码:b5db)

  3. mac下 配置tomcat

    第一步: 1.打开你的终端:然后输入  pico .bash_profile   回车 第二步: 2. 然后添加你tomcat放入的路径的path 编辑完后,control+x   (保存)    继 ...

  4. 后台管理UI

    后台管理UI 目录 一.EasyUI 二.DWZ JUI 三.HUI 四.BUI 五.Ace Admin 六.Metronic 七.H+ UI 八.Admin LTE 九.INSPINIA 十.Lig ...

  5. FJ省队集训DAY1 T1

    题意:有一堆兔子,还有一个r为半径的圆,要求找到最大集合满足这个集合里的兔子两两连边的直线不经过圆. 思路:发现如果有两个点之间连边不经过圆,那么他们到圆的切线会构成一段区间,那么这两个点的区间一定会 ...

  6. android中button点击频率控制

    public class Utils { private static long lastClickTime; public static boolean isFastDoubleClick() { ...

  7. V$LATCH_PARENT和V$LATCH_CHILDREN

    V$LATCH_PARENT contains statistics about parent latches. The columns of V$LATCH_PARENT are identical ...

  8. DOS批处理命令判断操作系统版本、执行各版本对应语句

    DOS批处理命令判断操作系统版本.执行各版本对应语句   昨天在家里试用  netsh interface ip set address 这些命令更改上网IP.DNS.网关等,今天将那些代码拿来办公室 ...

  9. Linux CentOS 7 YouCompleteMe相关配置。

    CentOS 6.5上面弄了2天,以失败告终!!!当作练手了.在网上看到一篇CentOS7.0上安装YouCompleteMe插件的文章,就重新在虚拟机上安装了一个CentOS7,按那个文章执行了一下 ...

  10. hdu1695:数论+容斥

    题目大意: 求x属于[1,b]和 y属于[1,d]的 gcd(x,y)=k 的方案数 题解: 观察发现 gcd()=k 不好处理,想到将x=x/k,y=y/k 后 gcd(x,y)=1.. 即问题转化 ...