# rui_er 的博客

Think twice, code once.

## 题解洛谷 P8023【[ONTAK2015] Tasowanie】

//By: Luogu@rui_er(122461)
#include <bits/stdc++.h>
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define per(x,y,z) for(int x=y;x>=z;x--)
#define debug printf("Running %s on line %d...\n",__FUNCTION__,__LINE__)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
using namespace std;
typedef long long ll;
const int N = 4e5+5;

int n1, n2, a[N], b[N], sa[N], rk[N], lst[N<<1], id[N], cnt[N];
template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}

int main() {
scanf("%d", &n1);
rep(i, 1, n1) scanf("%d", &a[i]);
scanf("%d", &n2);
rep(i, 1, n2) scanf("%d", &a[n1+i]);
int n = n1 + n2, m = 1000;
rep(i, 1, n) ++cnt[rk[i] = a[i]];
rep(i, 1, m) cnt[i] += cnt[i-1];
per(i, n, 1) sa[cnt[rk[i]]--] = i;
for(int w=1,p=0;;w<<=1,m=p) {
p = 0;
per(i, n, n-w+1) id[++p] = i;
rep(i, 1, n) if(sa[i] > w) id[++p] = sa[i] - w;
memset(cnt, 0, sizeof(cnt));
rep(i, 1, n) ++cnt[rk[id[i]]];
rep(i, 1, m) cnt[i] += cnt[i-1];
per(i, n, 1) sa[cnt[rk[id[i]]]--] = id[i];
memcpy(lst, rk, sizeof(rk));
p = 0;
rep(i, 1, n) {
if(lst[sa[i]] == lst[sa[i-1]] && lst[sa[i]+w] == lst[sa[i-1]+w]) rk[sa[i]] = p;
else rk[sa[i]] = ++p;
}
if(p == n) {
rep(i, 1, n) sa[rk[i]] = i;
break;
}
}
for(int l=1,r=n1+1,i=1;i<=n;i++) {
if(l > n1) b[i] = a[r++];
else if(r > n) b[i] = a[l++];
else b[i] = (rk[l] < rk[r]) ? a[l++] : a[r++];
}
rep(i, 1, n) printf("%d%c", b[i], " \n"[i==n]);
return 0;
}


posted on 2022-02-09 11:02  rui_er  阅读(11)  评论(0编辑  收藏  举报