标签:wap 代码 scanf lib 位置 return ble tin ase
题意:将一列数字排序 排序规则是 每次找到最小值的位置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; }
标签:wap 代码 scanf lib 位置 return ble tin ase
原文地址:http://www.cnblogs.com/dwtfukgv/p/6783441.html