1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <bits/stdc++.h> using namespace std;
const int N = 6e5 + 5;
void sort(int* sa, int* rk, int* tp, int n, int m) { static int s[N]; for (int i = 0; i < m; i++) s[i] = 0; for (int i = 0; i < n; i++) s[rk[i]]++; for (int i = 1; i < m; i++) s[i] += s[i - 1]; for (int i = n - 1; i >= 0; i--) sa[--s[rk[tp[i]]]] = tp[i]; }
void getsa(int* sa, int* s, int n, int m) { static int rk[N], tp[N]; for (int i = 0; i < n; i++) rk[i] = s[i]; for (int i = 0; i < n; i++) tp[i] = i; sort(sa, rk, tp, n, m); for (int k = 1; k <= n; k <<= 1) { for (int i = 0; i < k; i++) tp[i] = n - 1 - i; for (int t = k, i = 0; i < n; i++) if (sa[i] >= k) tp[t++] = sa[i] - k;
sort(sa, rk, tp, n, m); for (int i = 0; i < n; i++) tp[i] = rk[i]; rk[sa[0]] = 0; for (int i = 1; i < n; i++) { int x = sa[i], y = sa[i - 1]; if (tp[x] == tp[y] && x + k < n && y + k < n && tp[x + k] == tp[y + k]) rk[x] = rk[y]; else rk[x] = rk[y] + 1; } } }
int main() { int n; scanf("%d", &n); vector<int> v(n); for (int i = 0; i < n; i++) scanf("%d", &v[i]); vector<int> dist = v; sort(dist.begin(), dist.end()); dist.erase(unique(dist.begin(), dist.end()), dist.end()); for (int& x : v) x = lower_bound(dist.begin(), dist.end(), x) - dist.begin(); for (int i = 0; i < n; i++) v.push_back(v[i]); vector<int> sa(v.size()); getsa(sa.data(), v.data(), v.size(), v.size()); int ans = 0; while (sa[ans] >= n) ans++; for (int i = 0; i < n; i++) { printf("%d ", dist[v[sa[ans] + i]]); } }
|