hdu 1890 Robotic Sort
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4415 Accepted Submission(s):
1981
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.
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.
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.
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
6
3 4 5 1 6 2
4
3 3 2 1
0
Sample Output
4 6 4 5 6 6
4 2 4 4
Source
Recommend
题目意思 :
(是不是感觉上面的题目很多余。。)
每输入一个n 输入n的整数
输出第i次操作前i所在位置
裸splay区间翻转
ps:注意做翻转题一定把下标向后移一位,详情请看reverse操作或者getpos操作中的ll=getkth(root,l-1),如果下标从1开始 getkth会炸。(可能仅限于我这种写法)
#include <algorithm> #include <ctype.h> #include <cstring> #include <cstdio> #define N 200000 #define INF 0x7fffffff using namespace std; inline void read(int &x) { x=0;bool f=0; register char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=1; for(; isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+ch-'0'; x=f?(~x)+1:x; } struct Data { int data,pos; bool operator<(Data a)const { if(data==a.data) return pos<a.pos; else return data<a.data; } }d[N]; int mn[N],pos[N],root,cn,data[N],siz[N],flag[N],fa[N],ch[N][2],n; inline void init() { memset(data,0,sizeof(data)); memset(flag,0,sizeof(flag)); memset(siz,0,sizeof(siz)); memset(pos,0,sizeof(pos)); memset(fa,0,sizeof(fa)); memset(ch,0,sizeof(ch)); memset(mn,0,sizeof(mn)); memset(d,0,sizeof(d)); cn=0; } class SB { private: inline int min(int a,int b) {return a>b?b:a;} inline void swap(int &a,int &b) { int tmp=b; b=a; a=tmp; } inline void pushdown(int rt) { if(flag[rt]) { flag[rt]=0; flag[ch[rt][0]]^=1; swap(ch[ch[rt][0]][0],ch[ch[rt][0]][1]); flag[ch[rt][1]]^=1; swap(ch[ch[rt][1]][0],ch[ch[rt][1]][1]); } } inline int son(int x) {return ch[fa[x]][1]==x;} inline void rotate(int x) { int y=fa[x],z=fa[y],b=son(x),c=son(y),a=ch[x][!b]; if(z) ch[z][c]=x;else root=x;fa[x]=z; if(a) fa[a]=y;ch[y][b]=a; ch[x][!b]=y;fa[y]=x; pushup(y);pushup(x); } public: inline void pushup(int rt) { int l=ch[rt][0],r=ch[rt][1]; mn[rt]=min(data[rt],min(mn[l],mn[r])); if(mn[rt]==data[rt]) pos[rt]=rt; else if(mn[rt]==mn[l]) pos[rt]=pos[l]; else pos[rt]=pos[r]; siz[rt]=siz[l]+siz[r]+1; } void splay(int x,int i) { while(fa[x]!=i) { int y=fa[x],z=fa[y]; if(z==i) rotate(x); else { pushdown(z); pushdown(y); pushdown(x); if(son(x)==son(y)) { rotate(y); rotate(x); } else { rotate(x); rotate(x); } } } } int getkth(int rt,int k) { pushdown(rt); int l=ch[rt][0]; if(siz[l]+1==k) return rt; if(k<siz[l]+1) return getkth(ch[rt][0],k); return getkth(ch[rt][1],k-(siz[l]+1)); } int getpos(int l,int r) { int ll=getkth(root,l-1),rr=getkth(root,r+1); splay(ll,0); splay(rr,ll); return pos[ch[rr][0]]; } void reverse(int l,int r) { int ll=getkth(root,l-1),rr=getkth(root,r+1); splay(ll,0); splay(rr,ll); int p=ch[rr][0]; flag[p]^=1; swap(ch[p][0],ch[p][1]); } }; class SB *SYL; int Main() { read(n); for(int a;n;read(n)) { init(); for(int i=2;i<=n+1;i++) { read(d[i].data); d[i].pos=i; } sort(d+2,d+2+n); for(int i=0;i<=n+2;i++) mn[i]=INF; for(int i=2;i<=n+1;i++) data[d[i].pos]=i; data[0]=data[1]=data[n+2]=INF; root=1;siz[0]=0; for(int i=1;i<=n+2;i++) { fa[i]=i-1; ch[i][1]=i+1; } ch[n+2][1]=0; for(int i=n+2;i>=1;i--) SYL->pushup(i); for(int i=1;i<=n;i++) { int p=SYL->getpos(i+1,n+1); SYL->splay(p,0); printf("%d",siz[ch[p][0]]); SYL->reverse(i+1,siz[ch[p][0]]+1); if(i!=n) printf(" "); } printf("\n"); } return 0; } int sb=Main(); int main(){;}
我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。