Topic Link: Click to open the link
The main idea: to give a sequence, divided into a number of permutation groups, requires the final sequence of the largest dictionary order.
The dictionary order is the largest, then from 1 onwards to traverse backward, as far as possible to put a larger number
Gives the sequence A1 A2 A3,,, Ai an
For the number of I, there may be three cases, the first to go forward to reach the maximum value of the sequence of AK, then the AK to AI is a rotation, the second AI itself, or the end of AI, the third by I want to find, for rotation, only in the I position ai+1, then AI and ai+ 1 can only form part of the rotation, and the maximum value is chosen in the three types of values that can be placed, that is, the number that can be placed in the first position
Attention
1, elected itself is, may be one of their own number for a rotation, may also be a rotation of the end, then I position to put this rotation of the first number.
2. For the third case, only one part of the rotation can be determined, and a complete rotation cannot be determined, and the rotated fragments may be connected, and the second case can be rotated intact.
#include <cstdio> #include <cstring> #include <queue> #include <set> #include <vector># Include <cmath> #include <map> #include <stack> #include <algorithm>using namespace std; #pragma Comment (linker, "/stack:102400000,102400000") #define LL __int64#define INF 0x3f3f3f3f#define PI acos ( -1.0) const INT MOD = 1e9+7; Const double EQS = 1e-9; #define MAXN 100000+10#define root 1,n,1#define int_rt int l,int r,int rt#define Lson L, (l+r)/2,rt<<1#define Rson (l+r)/2+1,r,rt<<1|1int cl1[maxn<<2], cl2[maxn<<2]; int A[MAXN], ans [MAXN], VIS[MAXN], ID[MAXN], flag[maxn];void push_up1 (int rt) {Cl1[rt] = max (cl1[rt<<1],cl1[rt<<1|1]); return;} void Create (INT_RT) {if (L = = r) {scanf ("%d", &a[l]); CL1[RT] = a[l]; id[A[l]] = l; return; } create (Lson); Create (Rson); Push_up1 (RT); return;} void update1 (int k,int_rt) {if (L = = r) {Cl1[rt] = 1 ; return; } if ((L+r)/2 >= k) update1 (K,lson); else if (K > (L+R)/2) update1 (K,rson); else return; Push_up1 (RT);} int query1 (int ll,int rr,int_rt) {if (RR < L | | ll > R) return-1; if (ll <= l && RR >= R) return CL1[RT]; Return Max (Query1 (Ll,rr,lson), Query1 (Ll,rr,rson));} void push_up2 (int rt) {Cl2[rt] = max (cl2[rt<<1], cl2[rt<<1|1]); return;} void Update2 (int k,int_rt) {if (L = = r) {Cl2[rt] = l; return; } if (k <= (L+R)/2) Update2 (K,lson); else if (K > (L+R)/2) Update2 (K,rson); else return; Push_up2 (RT); return;} int query2 (int ll,int rr,int_rt) {if (RR < L | | ll > R) return-1; if (ll <= l && RR >= R) return CL2[RT]; Return Max (Query2 (Ll,rr,lson), Query2 (Ll,rr,rson));} int main () {int T, n, I, J; int K1, K2, K3; int L, R; Freopen ("1012.in", "R", stdin); Freopen ("23333.txt", "W", stdout); scanf ("%d", &t); while (t--) {memset (cl1,-1,sizeof (CL1)); memset (cl2,-1,sizeof (Cl2)); memset (vis,0,sizeof (VIS)); memset (flag,0,sizeof (flag)); scanf ("%d", &n); Create (root); A[N+1] =-1; for (i = 1; I <= n; i++) {if (i = = 715) i = 715; if (vis[i] = = 1) continue; L = Query2 (1,id[i]-1,root); l++; if (l <= 0) L = 1; K1 = Query1 (l,id[i]-1,root); if (vis[i] = = 0) K2 = i; else if (Vis[i] < 0 &&!vis[-vis[i]) k2 =-vis[i]; else K2 =-1; K3 = a[id[i]+1]; if (vis[a[id[i]+1]] = = 1) K3 =-1; if (K1 > K2 && K1 > K3) {ans[i] = K1; VIS[K1] = 1; for (j = Id[k1]; j < Id[i]; J + +) {ans[A[j]] = a[j+1]; vis[A[j+1]] = 1; } update2 (Id[i],root); } else if (K2 > K1 && K2 > K3) {ans[i] = K2; vis[Ans[i]] = 1; flag[flag[K2]] = 0; FLAG[K2] = 0; Update2 (Id[i],root); } else if (K3 > K1 && K3 > K2) {if (vis[i] = = 0) {vis[K3] =-I.; Ans[i] = K3; FLAG[K3] = i; Flag[i] = K3; } else {if (flag[K3]) {ans[i] = K3; vis[K3] = 1; flag[Flag[k3]] = flag[i]; flag[Flag[i]] = Flag[k3]; vis[Flag[k3]] = vis[i]; Continue; } else{Flag[k3] = flag[i]; FlAg[flag[i]] = K3; VIS[K3] = vis[i]; Ans[i] = K3; }} update1 (Id[i],root); Update1 (Id[k3],root); }} for (i = 1; i < n; i++) printf ("%d", ans[i]); printf ("%d\n", Ans[n]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu5338 (2015 + school 4)--zzx and permutations (permutation group)