Description

Given a sequence, {A1A2, ..., An} which is guaranteed AA2, ..., 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 {A1A2, ..., An} and {B1B2, ..., Bn}, we say {A1A2, ..., An} is smaller than {B1B2, ..., 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}
题意:将数组划分成三分,分别翻转,求翻转后字典序最小的
 
题解:首先将原数组首尾倒转,后缀数组找出字典序最小的后缀,然后再把剩下的翻转,再用后缀数组找出最小的后缀,然后就稳了
 
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int n,k,pos,a[],b[],c[],sa[],rk[],tmp[]; int cmp(int i,int j)
{
if(rk[i]!=rk[j]) return rk[i]<rk[j];
int ri=i+k<=n?rk[i+k]:-;
int rj=j+k<=n?rk[j+k]:-;
return ri<rj;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[n-i]=a[i];
}
for(int i=;i<n;i++)
{
sa[i]=i;
rk[i]=b[i];
}
sa[n]=n;
rk[n]=-;
for(k=;k<=n;k<<=)
{
sort(sa,sa+n+,cmp);
tmp[sa[]]=;
for(int i=;i<=n;i++)
{
tmp[sa[i]]=tmp[sa[i-]]+cmp(sa[i-],sa[i]);
}
for(int i=;i<=n;i++)
{
rk[i]=tmp[i];
}
}
sort(sa,sa+n+,cmp);
for(int i=;i<=n;i++)
{
if(sa[i]!=&&sa[i]!=&&sa[i]!=n)
{
pos=sa[i];
break;
}
}
int len=n-pos;
int cnt=;
for(int i=n;i>=len+;i--)
{
c[cnt++]=a[i];
}
for(int i=cnt;i<cnt*;i++)
{
c[i]=c[i-cnt];
}
n=cnt*;
for(int i=;i<n;i++)
{
sa[i]=i;
rk[i]=c[i];
}
sa[n]=n;
rk[n]=-;
for(k=;k<=n;k<<=)
{
sort(sa,sa+n+,cmp);
tmp[sa[]]=;
for(int i=;i<=n;i++)
{
tmp[sa[i]]=tmp[sa[i-]]+cmp(sa[i-],sa[i]);
}
for(int i=;i<=n;i++)
{
rk[i]=tmp[i];
}
}
for(int i=len;i>=;i--)
{
printf("%d\n",a[i]);
}
sort(sa,sa+n+,cmp);
for(int i=;i<=n;i++)
{
if(sa[i]+n/<n&&sa[i]!=)
{
for(int j=sa[i];j<=sa[i]+n/-;j++)
{
printf("%d\n",c[j]);
}
break;
}
}
}

POJ 3581 Sequence(后缀数组)的更多相关文章

  1. POJ 3581 Sequence ——后缀数组 最小表示法

    [题目分析] 一见到题目,就有了一个显而易见obviously的想法.只需要每次找到倒过来最小的那一个字符串翻转就可以了. 然而事情并不是这样的,比如说505023这样一个字符串,如果翻转了成为320 ...

  2. POJ 3581 Sequence [后缀数组]

    Sequence Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6911   Accepted: 1543 Case Tim ...

  3. [POJ 3581]Sequence

    [POJ 3581]Sequence 标签: 后缀数组 题目链接 题意 给你一串序列\(A_i\),保证对于$ \forall i \in [2,n],都有A_1 >A_i$. 现在需要把这个序 ...

  4. 后缀数组 POJ 3581 Sequence

    题目链接 题意:把n个数字(A1比其他数字都大)的序列分成三段,每段分别反转,问字典序最小的序列. 分析:因为A1比其他数字都大,所以反转后第一段结尾是很大的数,相当是天然的分割线,第一段可以单独考虑 ...

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

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

  6. POJ3581 Sequence —— 后缀数组

    题目链接:https://vjudge.net/problem/POJ-3581 Sequence Time Limit: 5000MS   Memory Limit: 65536K Total Su ...

  7. POJ 2406 KMP/后缀数组

    题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...

  8. POJ 1743-POJ - 3261~后缀数组关于最长字串问题

    POJ 1743 题意: 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1~~88范围内的整数,现在要找一个重复的主题.“主题”是整个音符序列的一个子串,它需 ...

  9. POJ - 1226 Substrings (后缀数组)

    传送门:POJ - 1226 这个题跟POJ - 3294  和POJ - 3450 都是一样的思路,一种题型. POJ - 3294的题解可以见:https://www.cnblogs.com/li ...

随机推荐

  1. Linux 之rsyslog+LogAnalyzer 日志收集系统

    一.LogAnalyzer介绍 LogAnalyzer工具提供了一个易于使用,功能强大的前端,用于搜索,查看和分析网络活动数据,包括系统日志,事件日志和其他许多日志源.由于它只是将数据展示到我们用户的 ...

  2. 爬虫之 图片懒加载, selenium , phantomJs, 谷歌无头浏览器

    一.图片懒加载 懒加载 :    JS 代码  是页面自然滚动    window.scrollTo(0,document.body.scrollHeight)   (重点) bro.execute_ ...

  3. “microsoft ace oledb 12.0 未注册”疑云

    1. 有人说: 2015也是要安装32位的AccessDataengine,anycpu选32位优先才行,不然就是Microsoft.ACE.OLEDB.12.0未注册. hanstom,一个老调重弹 ...

  4. 5.Redis 发布订阅

    转自:http://www.runoob.com/redis/redis-tutorial.html Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub ...

  5. IOS Background 之 Background Fetch

    http://www.ithao123.cn/content-1363653.html 定期更新数据的app,比如及时通信类,微博等app. 定期后台获取,等打开后获取的快一些. 30分钟后打开手,获 ...

  6. github提交表情包

    emoji-list emoji表情列表 目录 人物 自然 事物 地点 符号 人物 :bowtie: :bowtie: :smile: :smile: :laughing: :laughing: :b ...

  7. linux下使用adb查看android手机的logcat

    root@ubuntu:/home/song# adb logcat -s VLC

  8. WPF DataGrid 控件的运用

    WPF DataGrid 控件的运用 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-23 参考: King Cobra 博客 ...

  9. Unity脚本开发基础 C#

    1. MonoBehaviour 类 常用事件响应函数: 2. 访问游戏对象 (1) 通过名称来查找 (2) 通过标签来查找 上述函数比较费时,应避免在 Update 函数调用. 3. 访问组件 对于 ...

  10. Cassandra修改集群名称

    如果需要在不影响存储数据的情况下,更改cassandra集群名字,可采用如下步骤: 1. 对集群所有节点(for each node)依次连接CQLSH,使用如下命令:  UPDATE system. ...