Rokua P3165 [CQOI2014] Sorting picker

Source: Internet
Author: User

Title Description

In order to rank items from low to high in the factory, engineers invented a sort of robotic arm. It follows a simple collation, the first operation to find the position of the lower object P1, and the left from the first to the P1 between the object, the second time to find the second low of the location of the object P2, and the left from the second to the P2 between the items reverse order ... Eventually all items will be sorted.

To give an example, the first _ operation, the lower of the object in position 4, so the 1th to 4th of the object reversed, the second operation, the second low item in the move place 6, so the 2nd to 6th object reverse order ...

Your task is to write a program that identifies a sequence of operations, that is, the Pi at the position of the lower item before each operation, so that the picker can work. It is important to note that if you have items of the same height, you must ensure that the relative position of the order is the same as the original.

Input output Format input format:

The first line contains the positive integer n, which indicates the number of items that need to be sorted.

The second line contains n space-delimited integer AI, which represents the height of each item.

Output format:

The output line contains n spaces separated by an integer pi.

Input and Output sample input sample #: Copy

6
3 4 5 1 6 2

Output Example # #: Replication

4 6 4 5 6 6

It's a very sick question.
We can find a very obvious pattern:
\ (i-1\) small number must not be in the \ (i\) interval within a query
so we can maintain the minimum value, find the minimum value in the interval for each query,
and then mark it up.

#include <iostream> #include <cstdio> #include <algorithm>using namespace std;const int maxn=1e5+10; const int MAXN=0X7FFFFF;CONST int inf=0x7fffffff; #define LS tree[x].ch[0] #define RS tree[x].ch[1]inline char NC () {stat    IC Char buf[maxn],*p1=buf,*p2=buf; Return p1==p2&& (p2= (p1=buf) +fread (Buf,1,maxn,stdin), P1==P2)? eof:*p1++;}    inline int read () {char c=nc (); int x=0,f=1; while (c< ' 0 ' | |    C> ' 9 ') {C=NC ();}    while (c>= ' 0 ' &&c<= ' 9 ') {x=x*10+c-' 0 '; C=nc ();} return x*f;} int n,m;struct q{int val,pos;}    a[maxn];struct node{int tot,fa,ch[2],mn,mnpos,v; BOOL Rev;}    Tree[maxn];int tot,point;int root;int posl,posr;inline void Connect (int x,int fa,bool how) {tree[x].fa=fa; Tree[fa].ch[how]=x;}    inline void update (int k) {if (!k) return;    tree[k].tot=tree[tree[k].ch[0]].tot+tree[tree[k].ch[1]].tot+1; TREE[K].MN=TREE[K].V;    Tree[k].mnpos=k; if (tree[k].ch[0]&&tree[tree[k].ch[0]].mn<tree[k].mn) tree[k].mn=tree[tree[k].ch[0]].mn,tree[k].mnpos=tree[tree[k].ch[0]].mnpos; if (tree[k].ch[1]&&tree[tree[k].ch[1]].mn<tree[k].mn) Tree[k].mn=tree[tree[k].ch[1]].mn,tree[k].mnpos =tree[tree[k].ch[1]].mnpos;}    inline int buildtree (int l,int r) {if (l>r) return 0;    int mid= (L+R) >>1;        if (l!=r) {Connect (Buildtree (l,mid-1), mid,0);      Connect (Buildtree (mid+1,r), mid,1);    } tree[mid].rev=0;    Tree[mid].mn=a[mid].val;    Tree[mid].v=a[mid].val;    Tree[mid].mnpos=mid;    Update (mid); return mid;} inline bool Ident (int x) {return tree[tree[x].fa].ch[1]==x;}        inline void pushdown (int x) {if (Tree[x].rev) {swap (tree[x].ch[0],tree[x].ch[1]);        Tree[tree[x].ch[0]].rev^=1;         Tree[tree[x].ch[1]].rev^=1;    tree[x].rev=0;    }}inline void rotate (int X) {pushdown (TREE[X].FA);p Ushdown (X);    int Y=TREE[X].FA;    if (y==root) root=x;    int R=TREE[Y].FA;    BOOL Yson=ident (X);    BOOL Rson=ident (Y);    int b=tree[x].ch[yson^1];    Connect (B,y,yson);Connect (y,x,yson^1);    Connect (X,r,rson); Update (Y); update (X);}    inline void splay (int x,int to) {TO=TREE[TO].FA;        while (tree[x].fa!=to) {if (tree[tree[x].fa].fa==to) rotate (x);        else if (ident (x) ==ident (TREE[X].FA)) rotate (TREE[X].FA), rotate (x);    else rotate (x), rotate (x); } update (x);}    int find (int x, int rk) {pushdown (x);//Tag Next pass if (TREE[LS].TOT+1==RK) return x;    else if (Tree[ls].tot >= RK) return find (LS, RK); else return find (RS, rk-tree[ls].tot-1);} const INT Comp (const q A,const q b) {return a.val<b.val| | (A.val==b.val&&a.pos<b.pos);} const int COMP2 (const q A,const q b) {return a.pos<b.pos;}    int querymin (int l,int r) {int x1=find (root,l);    int X2=find (root,r+2);    Splay (X1,root);    Splay (x2,tree[x1].ch[1]); Return tree[tree[x2].ch[0]].mnpos;}    inline void rever (int l,int r) {int X1=find (root,l), X2=find (root,r+2);    Splay (X1,root); splay (x2,tree[x1].ch[1]); Tree[tree[x2].ch[0]].rev^=1;} int main (){#ifdef WIN32 freopen ("a.in", "R", stdin);    #else #endif int n=read ();    A[1].val=a[n+2].val=inf;    for (int i=2;i<=n+1;i++) A[i].val=read (), a[i].pos=i;    Sort (a+2,a+n+2,comp);    for (int i=2;i<=n+1;i++) a[i].val=i-1;    Sort (A+2,A+N+2,COMP2);    for (int i=1;i<=n+2;i++) printf ("%d", A[i].pos);p rintf ("\ n");    Buildtree (1,n+2);    root= (n+3) >>1;        for (int i=1;i<=n;i++) {int x=querymin (i,n);        Splay (X,root);        int Out=tree[tree[x].ch[0]].tot;        Rever (i,out);    printf ("%d", out); }}

Rokua P3165 [CQOI2014] Sorting picker

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.