Sequence
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7923   Accepted: 1801
Case Time Limit: 2000MS

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}

Source

题意:给一个数列,将其分三段,使得每段分别反转组合后的字典序最小。
思路:P381
代码:
 //#include"bits/stdc++.h"
#include"cstdio"
#include"map"
#include"set"
#include"cmath"
#include"queue"
#include"vector"
#include"string"
#include"cstring"
#include"ctime"
#include"iostream"
#include"cstdlib"
#include"algorithm"
#define db double
#define ll long long
#define ull unsigned long long
#define vec vector<ll>
#define mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
//#define rep(i, x, y) for(int i=x;i<=y;i++)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int N = 2e5 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const int inf = 0x3f3f3f3f;
const db PI = acos(-1.0);
const db eps = 1e-;
int sa[N],rev[N];
int r[N];
int tmp[N];
int a[N];
int n,k;
int p1,p2;
bool cmp(int i,int j){
if(r[i] != r[j]) return r[i]<r[j];
else
{
int ri=i+k<=n?r[i+k]:-;
int rj=j+k<=n?r[j+k]:-;
return ri<rj;
}
}
void bulid(int s[N],int l,int *sa)
{ for(int i=;i<=l;i++){
sa[i]=i;
r[i]=i<l?s[i]:-;
}
for(k=;k<=l;k*=){
sort(sa,sa+l+,cmp);
tmp[sa[]]=;
for(int i=;i<=l;i++){
tmp[sa[i]]=tmp[sa[i-]]+(cmp(sa[i-],sa[i])?:);
}
for(int i=;i<=l;i++){
r[i]=tmp[i];
}
}
}
void cal()
{
memset(rev,, sizeof(rev));
memset(r,, sizeof(r));
memset(sa,, sizeof(sa));
reverse_copy(a,a+n,rev);
bulid(rev,n,sa);
for(int i=;i<=n;i++){
p1=n-sa[i];
if(p1>=&&n-p1>=){
break;
}
}
int m=n-p1;
reverse_copy(a+p1,a+n,rev);
reverse_copy(a+p1,a+n,rev+m);
bulid(rev,*m,sa);
for(int i=;i<=*m;i++){
p2=p1+m-sa[i];
if(p2-p1>=&&n-p2>=){
break;
}
}
reverse(a,a+p1);
reverse(a+p1,a+p2);
reverse(a+p2,a+n);
for(int i=;i<n;i++) printf("%d\n",a[i]);
}
int main()
{
ci(n);
for(int i=;i<n;i++) ci(a[i]);
cal();
return ;
}

POJ 3581 三段字符串(后缀数组)的更多相关文章

  1. POJ 3581 Sequence(后缀数组)题解

    题意: 已知某字符串\(str\)满足\(str_1 > max\{str_2,str_3 \cdots str_n\}\),现要求把这个字符串分成连续的三组,然后每组都翻转,问字典序最小是什么 ...

  2. POJ 3581 Sequence(后缀数组)

    [题目链接] http://poj.org/problem?id=3581 [题目大意] 给出一个数列,将这个数列分成三段,每段分别翻转,使得其字典序最小,输出翻转后的数列. [题解] 首先,第一个翻 ...

  3. poj 3518 Corporate Identity 后缀数组->多字符串最长相同连续子串

    题目链接 题意:输入N(2 <= N <= 4000)个长度不超过200的字符串,输出字典序最小的最长公共连续子串; 思路:将所有的字符串中间加上分隔符,注:分隔符只需要和输入的字符不同, ...

  4. POJ 1226 Substrings(后缀数组+二分答案)

    [题目链接] http://poj.org/problem?id=1226 [题目大意] 求在每个给出字符串中出现的最长子串的长度,字符串在出现的时候可以是倒置的. [题解] 我们将每个字符串倒置,用 ...

  5. POJ 3080 Blue Jeans 后缀数组, 高度数组 难度:1

    题目 http://poj.org/problem?id=3080 题意 有m个(2<=m<=10)不包含空格的字符串,长度为60个字符,求所有字符串中都出现过的最长公共子序列,若该子序列 ...

  6. Poj 3294 Life Forms (后缀数组 + 二分 + Hash)

    题目链接: Poj 3294 Life Forms 题目描述: 有n个文本串,问在一半以上的文本串出现过的最长连续子串? 解题思路: 可以把文本串用没有出现过的不同字符连起来,然后求新文本串的heig ...

  7. POJ 1743 Musical Theme 后缀数组 最长重复不相交子串

    Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...

  8. Bzoj4556: [Tjoi2016&Heoi2016]字符串 后缀数组

    4556: [Tjoi2016&Heoi2016]字符串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 169  Solved: 87[Sub ...

  9. 【BZOJ 3473】 字符串 (后缀数组+RMQ+二分 | 广义SAM)

    3473: 字符串 Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串 ...

随机推荐

  1. JS条件语句优化

    1.对多个条件使用Array.includes eg: function test(fruit){                                                    ...

  2. (C#) 线程之 AutoResetEvent, EventHandle.

    AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号.如果 AutoRese ...

  3. Java笔记 —— 方法重载和方法重写

    Java笔记 -- 方法重载和方法重写 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red ...

  4. 【Android学习入门】Android studio基本设置

    1.背景设置 依次选择File->Settings-->Appearance & Behaviour->Apprearance,然后勾选 show line number. ...

  5. [SVN]TortoiseSVN工具培训1─为什么要用SVN?

    你是否有经历过以下情况: SVN的使用理由/好处:

  6. HCNA配置console线路密码password认证

    1.华为设备配置主机名<Huawei>system    <Huawei>system-view Enter system view, return user view wit ...

  7. vos优化客户线路数量

    问题: 客户A打电话过来说之前给开的300线路不够用了,今天好多电话呼不出去了,线路太挤了! 麻烦增加线路,如何操作? 方法: 登陆VOS——在线对接——(客户A的对接网关ID)——双击 打开网关管理 ...

  8. Tensorflow ValueError: Protocol message RewriterConfig has no "layout_optimizer" field

    I changed models/research/object_detection/exporter.py line 71/72 from: rewrite_options = rewriter_c ...

  9. April 10 2017 Week 15 Monday

    He alone is poor who does not possess knowledge. 没有知识,才是贫穷. Knowledge, as a important part of wisdom ...

  10. Codeforces Round #261 (Div. 2) - E (459E)

    题目连接:http://codeforces.com/contest/459/problem/E 题目大意:给定一张有向图,无自环无重边,每条边有一个边权,求最长严格上升路径长度.(1≤n,m≤3 * ...