HDU 1890 Robotic Sort (Splay)

题意:将一列数字排序  排序规则是  每次找到最小值的位置loc  将1~loc所有数字颠倒  然后删掉第一位  直到排好序  排序要求是稳定的。

析:直接用splay来维护最小值,然后插入删除即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
  int val, id;
  bool operator < (const Node &p) const{
    return val < p.val || (val == p.val && id < p.id);
  }
  bool operator == (const Node &p) const{
    return val == p.val && id == p.id;
  }
};
Node a[maxn], key[maxn], minv[maxn];

#define Key_value ch[ch[root][1]][0]
int pre[maxn], ch[maxn][2], sz[maxn];
int root, tot1;
int rev[maxn];
int s[maxn], tot2;

void NewNode(int &rt, int fa, Node x){
  if(tot2)  rt = s[tot2--];
  else  rt = ++tot1;
  pre[rt] = fa;
  key[rt] = x;
  ch[rt][0] = ch[rt][1] = 0;
  rev[rt] = 0;
  sz[rt] = 1;
}

void push_up(int rt){
  int l = ch[rt][0], r = ch[rt][1];
  sz[rt] = sz[l] + sz[r] + 1;
  minv[rt] = key[rt];
  if(minv[l] < minv[rt])  minv[rt] = minv[l];
  if(minv[r] < minv[rt])  minv[rt] = minv[r];
}

void update_rev(int rt){
  if(!rt)  return ;
  swap(ch[rt][0], ch[rt][1]);
  rev[rt] ^= 1;
}

void push_down(int rt){
  if(rev[rt]){
    update_rev(ch[rt][0]);
    update_rev(ch[rt][1]);
    rev[rt] = 0;
  }
}

void Build(int &rt, int l, int r, int fa){
  if(l > r)  return ;
  int m = l+r >> 1;
  NewNode(rt, fa, a[m]);
  Build(ch[rt][0], l, m-1, rt);
  Build(ch[rt][1], m+1, r, rt);
  push_up(rt);
}

void Init(){
  tot1 = root = tot2 = 0;
  ch[root][0] = ch[root][1] = sz[root] = pre[root] = 0;
  rev[root] = 0;  key[root] = (Node){INF, INF};
  NewNode(root, 0, (Node){INF, INF});
  NewNode(ch[root][1], root, (Node){INF, INF});
  for(int i = 0; i < n; ++i){
    scanf("%d", &a[i].val);
    a[i].id = i+1;
  }
  Build(Key_value, 0, n-1, ch[root][1]);
  push_up(ch[root][1]);
  push_up(root);
}

int Get_kth(int rt, int k){
  push_down(rt);
  int t = sz[ch[rt][0]] + 1;
  if(t == k)  return rt;
  if(t > k)  return Get_kth(ch[rt][0], k);
  return Get_kth(ch[rt][1], k-t);
}

void Rotate(int x, int k){
  int y = pre[x];
  push_down(y);
  push_down(x);
  ch[y][!k] = ch[x][k];
  pre[ch[x][k]] = y;
  if(pre[y])  ch[pre[y]][ch[pre[y]][1]==y] = x;
  pre[x] = pre[y];
  ch[x][k] = y;
  pre[y] = x;
  push_up(y);
}

void Splay(int rt, int goal){
  push_down(rt);
  while(pre[rt] != goal){
    if(pre[pre[rt]] == goal){
      push_down(pre[rt]);
      push_down(rt);
      Rotate(rt, ch[pre[rt]][0] == rt);
      continue;
    }
    push_down(pre[pre[rt]]);
    push_down(pre[rt]);
    push_down(rt);
    int y = pre[rt];
    int k = ch[pre[y]][0] == y;
    if(ch[y][k] == rt){
      Rotate(rt, !k);
      Rotate(rt, k);
    }
    else{
      Rotate(y, k);
      Rotate(rt, k);
    }
  }
  push_up(rt);
  if(goal == 0)  root = rt;
}

void Insert(int pos, int tot){
  Splay(Get_kth(root, pos+1), 0);
  Splay(Get_kth(root, pos+2), root);
  Build(Key_value, 0, tot-1, ch[root][1]);
  push_up(ch[root][1]);
  push_up(root);
}

void Erase(int rt){
  if(!rt)  return ;
  s[++tot2] = rt;
  Erase(ch[rt][0]);
  Erase(ch[rt][1]);
}

void Reverse(int pos, int tot){
  Splay(Get_kth(root, pos), 0);
  Splay(Get_kth(root, pos+tot+1), root);
  update_rev(Key_value);
  push_up(ch[root][1]);
  push_up(root);
}

int Get_min(int rt, Node x){
  if(key[rt] == x)  return rt;
  if(minv[ch[rt][0]] == x)  return Get_min(ch[rt][0], x);
  return Get_min(ch[rt][1], x);
}

int query(Node x){
  int t = Get_min(root, x);
  Splay(t, 0);
  int ans = sz[ch[root][0]];
  Reverse(1, ans);
  Splay(Get_kth(root, 1), 0);
  Splay(Get_kth(root, 3), root);
  Erase(Key_value);
  pre[Key_value] = 0;
  Key_value = 0;
  push_up(ch[root][1]);
  push_up(root);
  return ans;
}

int main(){
  while(scanf("%d", &n) == 1 && n){
    memset(minv, INF, sizeof minv);
    Init();
    sort(a, a + n);
    for(int i = 0; i < n; ++i){
      if(i)  putchar(‘ ‘);
      printf("%d", query(a[i])+i);
    }
    printf("\n");
  }
  return 0;
}

  

时间: 2024-10-12 14:40:02

HDU 1890 Robotic Sort (Splay)的相关文章

hdu 1890 Robotic Sort(splay 区间反转+删点)

题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的splay区间翻转+删点. 我们把数据排序,然后记录一下每个数原来的位置,然后splay建树的时候用原来的位置来对应,这样val[i].second就直接是这个数在splay中的那个节点. (当然你也可以普通建树,然后手动记录位置). 然后我们把要找的那个数对应的节点旋转到根,然后根左边的size+i就

HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description Somewhere deep in the Czech Technical University buildings, there are laboratories for examining

HDU 1890 Robotic Sort

题意: 将一列数字排序  排序规则是  每次找到最小值的位置loc  将1~loc所有数字颠倒  然后删掉第一位  直到排好序  排序要求是稳定的 思路: 这题要做的是  寻找区间最小值位置  翻转区间  的操作  因此可以想到用splay 只需要每个节点记录一个small  就可以实现找到最小值位置 翻转区间操作就是将splay的超级头转到最上面使之成为根  再把loc转到根下面  这时根的右儿子的左儿子就是需要翻转的区间  用一个rev延迟更新  然后将loc转到最上面是指成为根  删掉根

HDU 1890 Robotic Sort 伸展树的区间反转与延迟标记

延迟标记像极了线段树,不再多说. 区间反转在树伸展到位之后,也变成了简单的递归交换左右儿子. 愈发感觉到伸展树简直太漂亮了,伸展操作更是诱惑到不行 ,总之数据结构太有魅力了. 比较简单,就直接上模板了. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #in

数据结构(Splay平衡树):HDU 1890 Robotic Sort

Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3456    Accepted Submission(s): 1493 Problem Description Somewhere deep in the Czech Technical University buildings, there are labora

Splay练习题 [HDU 1890] Robotic Sort

Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2495    Accepted Submission(s): 1107 Problem Description Somewhere deep in the Czech Technical University buildings, there are labor

HDU 1890 Robotic Sort(splay)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i的所在位置.相等的两个数排序后相对位置不变. [思路] 由于相对位置不变,所以可以根据数值与位置重编号. 依旧使用直接定位从上到下旋转至根的splay写法.每次将i结点旋转至根,则答案为左儿子大小+i,然后将i删掉合并左右儿子. 需要注意合并时判断左右儿子是否为空,以及各种pushdown下传标记.

HDU1890 Robotic Sort[splay 序列]

Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3913    Accepted Submission(s): 1717 Problem Description Somewhere deep in the Czech Technical University buildings, there are labora

BZOJ 1552: [Cerc2007]robotic sort( splay )

kpm大神说可以用块状链表写...但是我不会...写了个splay.... 先离散化 , 然后splay结点加个min维护最小值 , 就可以了... ( ps BZOJ 3506 题意一样 , 双倍经验 ) ----------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #inclu