hdu 1890 Splay区间最小值、区间翻转
来源:互联网 发布:nginx apache ssl配置 编辑:程序博客网 时间:2024/04/29 13:18
Robotic Sort
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2800 Accepted Submission(s): 1217
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
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)
序列区间翻转操作,用Splay可以很方便的实现。这里翻转还是保持原来的相对顺序,所以记录最小值的时候要比较值和序号。
#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <vector>#include <set>#include <map>#include <stack>#include <queue>using namespace std;#define inf 0x3f3f3f3f#define key_rt ch[ch[root][1]][0]#define maxn 100010#define lc ch[x][0]#define rc ch[x][1]struct Splay{ int ch[maxn][2],par[maxn],size[maxn],key[maxn],mini[maxn], order[maxn]; bool lazy[maxn]; int root, tot; int a[maxn]; int cmp(int a, int b) { if(key[a]!=key[b]) return key[a]<key[b]? a:b; else return order[a]<order[b]?a:b; } void push_up(int x) { size[x]=1+size[lc]+size[rc]; mini[x]=cmp(cmp(mini[lc], mini[rc]),x); } void update_rev(int x) { if(x==0) return; lazy[x]=1-lazy[x]; swap(ch[x][0], ch[x][1]); } void push_down(int x) { if(lazy[x]){ update_rev(lc); update_rev(rc); lazy[x]=0; } } void newnode(int &rt, int p, int k) { tot++; rt=tot; par[rt]=p; ch[rt][0]=ch[rt][1]=0; key[rt]=k; mini[rt]=rt; size[rt]=1; lazy[rt]=0; } void build(int &rt, int l, int r, int p) { if(l>r){ rt=0; return; } int m=(l+r)>>1; newnode(rt, p, a[m]); order[rt]=m; build(ch[rt][0], l, m-1, rt); build(ch[rt][1], m+1, r, rt); push_up(rt); } void init(int n) { for(int i=1; i<=n; i++) scanf("%d", a+i); size[0]=par[0]=ch[0][0]=ch[0][1]=0; key[0]=inf; mini[0]=0; tot=0; newnode(root, 0, inf); newnode(ch[root][1], root, inf); build(key_rt, 1, n, ch[root][1]); push_up(ch[root][1]); push_up(root); } void rotate(int x, int k) { if(x==root) return; int y=par[x]; push_down(y); push_down(x); ch[y][!k]=ch[x][k]; par[ch[x][k]]=y; ch[x][k]=y; if(par[y]) ch[par[y]][ch[par[y]][1]==y]=x; par[x]=par[y]; par[y]=x; if(y==root) root=x; push_up(y); } void splay(int x, int t) { int y,yy; while(par[x]!=t){ y=par[x]; yy=par[y]; if(yy==t){ push_down(y); rotate(x, ch[y][0]==x); } else{ push_down(yy); push_down(y); int k= (ch[yy][0]==y); if(ch[y][k]==x){ rotate(x,!k); rotate(x, k); } else{ rotate(y, k); rotate(x, k); } } } push_up(x); } int get_kth(int k) { int x=root; push_down(x); while(size[lc]!=k-1){ if(size[lc]>k-1) x=lc; else{ k-=size[lc]+1; x=rc; } push_down(x); } return x; } void get(int a, int b) { splay(get_kth(a), 0); splay(get_kth(b+2), root); } int get_min(int x) { int k=0; int t=mini[x]; push_down(x); while(x!=t){ if(mini[lc]==mini[x]){ x=lc; } else{ k+=1+size[lc]; x=rc; } push_down(x); } k+=size[lc]+1; return k; } void solve(int n) { init(n); for(int i=1; i<=n; i++){ // print(root); cout<<endl; get(i, n); int k=get_min(key_rt); k+=size[ch[root][0]]; a[i]=k; get(i, k); update_rev(key_rt); // print(root); cout<<endl<<endl; } for(int i=1; i<=n; i++){ printf("%d", a[i]); if(i!=n) printf(" "); else puts(""); } }};Splay tree;int main(){ int n; while(scanf("%d", &n)==1 && n){ tree.solve(n); } return 0;}
0 0
- hdu 1890 Splay区间最小值、区间翻转
- hdu 1890 splay区间翻转
- HDU 1890 splay区间翻转
- HDU 1890 Splay区间翻转
- hdu 1890 splay 区间翻转
- 【HDU】1890 Robotic Sort 翻转区间【splay】
- HDU 1890 Robotic Sort(Splay 区间翻转)
- hdu 1890伸展树(splay tree)区间翻转
- hdu-1890-Robotic Sort-splay tree-区间翻转
- HDU 1890 Robotic Sort 单点查找区间翻转 Splay裸题
- hdu 1890 Robotic Sort (Splay区间翻转)
- HDU 1890 Robotic Sort [平衡树splay 区间翻转]
- hdu 3487 区间 翻转 切割 插入 splay
- HDU 3487 splay区间翻转切割
- Splay区间翻转
- 区间翻转问题 Splay
- POJ 题目3580 SuperMemo(Splay Tree区间加,区间翻转,区间右移,插入删除,区间最小值)
- HDU3487(splay区间翻转+区间切割)
- HDU 1599 find the mincost route 无向图最小环
- 新的作息时间表
- LeetCode-73 Set Matrix Zeroes(设置零矩阵)
- 面试题49:把字符串转换成整数
- java语言实现创建型设计模式—单例模式
- hdu 1890 Splay区间最小值、区间翻转
- 《C语言及程序设计》实践参考——动态数组排序
- 《C语言及程序设计》实践参考——为动态数组扩容
- Ruby on Rails,使用new和create创建ActiveRecord对象及他们之间的区别
- PADS学习之路08-PADS LOGIC中的三种工具栏
- 使用GPLv2 or ASLv2
- 移动终端app测试点总结
- 上课笔记--商务调研
- shell脚本编程基础