hdu1890 Robotic Sort(Splay)

来源:互联网 发布:淘宝客链接转化api 编辑:程序博客网 时间:2024/06/06 10:49

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1890

题意:将给定的数组排序,你可以用的操作是反转一段区间。输出你的操作。

Robotic Sort

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3275    Accepted Submission(s): 1394


Problem Description
Somewhere deep in the Czech Technical University buildings, there are laboratories for examining mechanical and electrical properties of various materials. In one of yesterday’s presentations, you have seen how was one of the laboratories changed into a new multimedia lab. But there are still others, serving to their original purposes. 

In this task, you are to write software for a robot that handles samples in such a laboratory. Imagine there are material samples lined up on a running belt. The samples have different heights, which may cause troubles to the next processing unit. To eliminate such troubles, we need to sort the samples by their height into the ascending order. 

Reordering is done by a mechanical robot arm, which is able to pick up any number of consecutive samples and turn them round, such that their mutual order is reversed. In other words, one robot operation can reverse the order of samples on positions between A and B. 

A possible way to sort the samples is to find the position of the smallest one (P1) and reverse the order between positions 1 and P1, which causes the smallest sample to become first. Then we find the second one on position P and reverse the order between 2 and P2. Then the third sample is located etc. 



The picture shows a simple example of 6 samples. The smallest one is on the 4th position, therefore, the robot arm reverses the first 4 samples. The second smallest sample is the last one, so the next robot operation will reverse the order of five samples on positions 2–6. The third step will be to reverse the samples 3–4, etc. 

Your task is to find the correct sequence of reversal operations that will sort the samples using the above algorithm. If there are more samples with the same height, their mutual order must be preserved: the one that was given first in the initial order must be placed before the others in the final order too.
 

Input
The input consists of several scenarios. Each scenario is described by two lines. The first line contains one integer number N , the number of samples, 1 ≤ N ≤ 100 000. The second line lists exactly N space-separated positive integers, they specify the heights of individual samples and their initial order. 

The last scenario is followed by a line containing zero.
 

Output
For each scenario, output one line with exactly N integers P1 , P1 , . . . PN ,separated by a space.
Each Pi must be an integer (1 ≤ Pi ≤ N ) giving the position of the i-th sample just before the i-th reversal operation. 

Note that if a sample is already on its correct position Pi , you should output the number Pi anyway, indicating that the “interval between Pi and Pi ” (a single sample) should be reversed. 
 

Sample Input
63 4 5 1 6 243 3 2 10
 

Sample Output
4 6 4 5 6 64 2 4 4
 

Source
2008 “Shun Yu Cup” Zhejiang Collegiate Programming Contest - Warm Up(2)
 

Recommend
linle   |   We have carefully selected several similar problems for you:  1882 1883 1887 1889 1888 
 

Statistic | Submit | Discuss | Note

分析:回家颓废了几天,果然在家没有心思学习。。。。

题目算比较简单的了。

由于每次找的都是最小值(除开已经排好序了那部分),可以用Splay维护区间最小值。

每次将最小值删掉,然后反转区间。反转区间的话,还是老方法,用懒惰标记即可。

代码:

#include <bits/stdc++.h>using namespace std;typedef long long LL;typedef unsigned long long ULL;const LL INF = 1E9+9;const int MI = ~0u>>1;#define lson son[rt][0]#define rson son[rt][1]#define getPos son[son[root][1]][0] const int maxn = 111111;int son[maxn][2],fa[maxn],sz[maxn],val[maxn];int Min[maxn];   //保存区间最小值int flip[maxn];   //懒惰标翻转 int root,cnt;int n,a[maxn];int newnode(int f,int v){++cnt;son[cnt][0]=son[cnt][1]=0;fa[cnt]=f;sz[cnt]=1;val[cnt]=v;Min[cnt]=v;return cnt;}void pushdown(int rt){if(flip[rt]){flip[lson]^=1;flip[rson]^=1;swap(lson,rson);flip[rt]=0; } }void pushup(int rt){sz[rt]=sz[lson]+sz[rson]+1;Min[rt]=val[rt];if(lson) Min[rt]=min(Min[rt],Min[lson]); if(rson) Min[rt]=min(Min[rt],Min[rson]);}void Rotate(int r,int kind){int y=fa[r];pushdown(y);     pushdown(r);son[y][kind^1]=son[r][kind];fa[son[r][kind]] = y;if(fa[y]!=0)son[fa[y]][son[fa[y]][1]==y] = r;fa[r]=fa[y];son[r][kind]=y;fa[y]=r; pushup(y);}void Splay(int r,int goal){pushdown(r); while(fa[r]!=goal){if(goal==fa[fa[r]])Rotate(r,son[fa[r]][0]==r);else{int y=fa[r];int kind=son[fa[y]][0]==y;if(son[y][kind]==r){Rotate(r,kind^1);Rotate(r,kind);}else{Rotate(y,kind);Rotate(r,kind);}}}if(0==goal)root=r;pushup(r);}int get_pre(int rt){pushdown(rt);rt=lson;pushdown(rt);while(rson){rt=rson;pushdown(rt);}return rt;}int findMin(int &num){int MinValue=Min[root];int rt=root;pushdown(rt);while(rt){if(val[rt]==MinValue) {   num+=sz[lson]+1;   return rt;}if(Min[lson]==MinValue) rt=lson;else{ num+=sz[lson]+1; rt=rson; }pushdown(rt);}return -1;}void RotateTo(int k,int goal)  //将代表位置k的节点移到goal下面{int rt=root;while(rt){pushdown(rt);if(sz[lson]+1==k) break;if(k>sz[lson]+1){k-=(sz[lson]+1);rt=rson;}else rt=lson;}Splay(rt,goal);}void F(int L,int R){if(L>R)return ; RotateTo(L,0);RotateTo(R+2,root);flip[getPos]^=1;}void Delete(int r){Splay(r,0);if(son[r][0] && son[r][1]){Splay(get_pre(root),root);int lch=son[r][0];son[lch][1]=son[r][1];fa[son[r][1]]=lch;root=lch;}else if(!son[r][0])root=son[r][1];elseroot=son[r][0];fa[root]=0;pushup(root);}void buildSplay(int l,int r,int &rt,int fa){if(l>r) return ;int mid=l+r>>1;rt=newnode(fa,a[mid]);buildSplay(l,mid-1,lson,rt);buildSplay(mid+1,r,rson,rt);pushup(rt); }void Init(){cnt=root=0;son[root][0]=son[root][1]=fa[root]=sz[root]=flip[root]=0;Min[root]=MI;root=newnode(0,MI);son[root][1]=newnode(root,MI);pushup(root);buildSplay(1,n,getPos,son[root][1]);pushup(son[root][1]);pushup(root); }void debug(int rt){if(rt){pushdown(rt);debug(lson);printf("节点:%d  左孩子:%d 右孩子:%d  |  %d  |  区间最小值:%d \n",rt,lson,rson,val[rt],Min[rt]);debug(rson);}}void solve(){for(int i=1;i<=n;i++){int x=0;int rt=findMin(x);if(i!=1) printf(" ");printf("%d",x-2+i);Delete(rt);F(1,x-2);}printf("\n");}struct node{int v,id;}pii[maxn];bool cmp1(const node &n1,const node &n2){if(n1.v!=n2.v) return n1.v<n2.v;return n1.id<n2.id;}bool cmp2(const node &n1,const node &n2){return n1.id<n2.id;}int main(){while(scanf("%d",&n)!=EOF && n>0){for(int i=1;i<=n;i++){scanf("%d",&a[i]); pii[i].v=a[i];pii[i].id=i;}sort(pii+1,pii+n+1,cmp1);for(int i=1;i<=n;i++)pii[i].v=i;sort(pii+1,pii+n+1,cmp2);for(int i=1;i<=n;i++)a[i]=pii[i].v;Init();solve();}return 0;}


0 0
原创粉丝点击