天天看點

POJ 3581 Sequence

Description

Given a sequence, {A1, A2, ..., An} which is guaranteed A1 > A2, ..., An,  you are to cut it into three sub-sequences and reverse them separately to form a new one which is the smallest possible sequence in alphabet order.

The alphabet order is defined as follows: for two sequence {A1, A2, ..., An} and {B1, B2, ..., Bn}, we say {A1, A2, ..., An} is smaller than {B1, B2, ..., Bn} if and only if there exists such i ( 1 ≤ i ≤ n) so that we have Ai < Bi and Aj = Bj for each j < i.

Input

The first line contains n. (n ≤ 200000)

The following n lines contain the sequence.

Output

output n lines which is the smallest possible sequence obtained.

Sample Input

5

10

1

2

3

4

Sample Output

1

10

2

4

3

Hint

{10, 1, 2, 3, 4} -> {10, 1 | 2 | 3, 4} -> {1, 10, 2, 4, 3}

把序列分成非空的三段,每段反轉再合并,問字典序最小是多少。

因為A[1]是最大的,是以第一段很簡單,求序列反轉以後最小的字尾就行了,注意,因為非空,這個字尾不能是第一個或第二個開頭的。

然後就是第二段和第三段,在反轉的序列中,第二段和第三段就相當于把序列一分為二,位置反過來合并,

這個就是序列的最小表示,可以用最小表示法搞定也可以把序列翻倍再用字尾數組,同樣要注意非空的問題。

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<string,string>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e8;
const int N = 5e5 + 10;
const int read()
{
  char ch = getchar();
  while (ch<'0' || ch>'9') ch = getchar();
  int x = ch - '0';
  while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
  return x;
}

struct Sa
{
  int s[N], a[N];
  int rk[2][N], sa[N], h[N], w[N], now, n, m;
  int rmq[N][20], lg[N];

  bool GetS()
  {
    n = read();
    rep(i, 1, n)
    {
      scanf("%d", &s[n - i + 1]);
      a[i] = s[n - i + 1];
    }
    sort(a + 1, a + n + 1);
    m = unique(a + 1, a + n + 1) - a;
    rep(i, 1, n) s[i] = lower_bound(a + 1, a + m, s[i]) - a;
    return true;
  }

  void getsa(int z, int &m)
  {
    int x = now, y = now ^= 1;
    rep(i, 1, z) rk[y][i] = n - i + 1;
    for (int i = 1, j = z; i <= n; i++)
      if (sa[i] > z) rk[y][++j] = sa[i] - z;

    rep(i, 1, m) w[i] = 0;
    rep(i, 1, n) w[rk[x][rk[y][i]]]++;
    rep(i, 1, m) w[i] += w[i - 1];
    per(i, n, 1) sa[w[rk[x][rk[y][i]]]--] = rk[y][i];
    for (int i = m = 1; i <= n; i++)
    {
      int *a = rk[x] + sa[i], *b = rk[x] + sa[i - 1];
      rk[y][sa[i]] = *a == *b&&*(a + z) == *(b + z) ? m - 1 : m++;
    }
  }

  void getsa(int m)
  {
    //n = strlen(s + 1);
    rk[1][0] = now = sa[0] = s[0] = 0;
    rep(i, 1, m) w[i] = 0;
    rep(i, 1, n) w[s[i]]++;
    rep(i, 1, m) rk[1][i] = rk[1][i - 1] + (bool)w[i];
    rep(i, 1, m) w[i] += w[i - 1];
    rep(i, 1, n) rk[0][i] = rk[1][s[i]];
    rep(i, 1, n) sa[w[s[i]]--] = i;

    rk[1][n + 1] = rk[0][n + 1] = 0;  //多組的時候容易出bug
    for (int x = 1, y = rk[1][m]; x <= n && y <= n; x <<= 1) getsa(x, y);
    for (int i = 1, j = 0; i <= n; h[rk[now][i++]] = j ? j-- : j)
    {
      if (rk[now][i] == 1) continue;
      int k = n - max(sa[rk[now][i] - 1], i);
      while (j <= k && s[sa[rk[now][i] - 1] + j] == s[i + j]) ++j;
    }
  }

  void getrmq()
  {
    h[n + 1] = h[1] = lg[1] = 0;
    rep(i, 2, n) rmq[i][0] = h[i], lg[i] = lg[i >> 1] + 1;
    for (int i = 1; (1 << i) <= n; i++)
    {
      rep(j, 2, n)
      {
        if (j + (1 << i) > n + 1) break;
        rmq[j][i] = min(rmq[j][i - 1], rmq[j + (1 << i - 1)][i - 1]);
      }
    }
  }

  int lcp(int x, int y)
  {
    int l = min(rk[now][x], rk[now][y]) + 1, r = max(rk[now][x], rk[now][y]);
    return min(rmq[l][lg[r - l + 1]], rmq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
  }

  void work()
  {
    GetS(); getsa(m);
    rep(i, 1, n) if (sa[i] > 2)
    { 
      rep(j, sa[i], n) printf("%d\n", a[s[j]]);
      n = sa[i] - 1;
      rep(j, 1, n) s[j + n] = s[j];
      n <<= 1;  getsa(m);
      rep(j, 1, n) if (sa[j] > 1 && sa[j] * 2 <= n)
      {
        rep(k, sa[j], n / 2) printf("%d\n", a[s[k]]);
        rep(k, 1, sa[j] - 1) printf("%d\n", a[s[k]]);
        break;
      }
      break; 
    }
  }
}sa;

int main()
{
  sa.work();
  return 0;
}