Sequence
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6893   Accepted: 1534
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 ≤ in) 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}
白书上的题目。第一道后缀数组,直接看了答案,但是没看懂。。。
第一段翻转很好求,直接翻转然后后缀数组。第二段第三段则有些问题,因为可能第二段比较短,虽然最小,但是是其他某个串的前缀,可能会出错(自己yy的,网上有反例)

9
8 4 -1 5 0 5 0 2 3
第一步:
3 2 0 5 0 5 -1 4 8 对应输出 -1 4 8
第二步
3 2 0 5 0 5(开始的时候我并没有复制一遍) 对应输出:0 5
第三步
3 2 0 5    对应输出: 3 2 0 5
可以看见这样做是不对的。。
必须要将剩下的字符串复制一遍贴在后面,然后再来求后缀数组。。。
正解:
第一步:
3 2 0 5 0 5 -1 4 8 对应输出 -1 4 8
第二步
3 2 0 5 0 5 3 2 0 5 0 5 对应输出: 0 5 0 5;
第三步
3 2 对应输出:3 2;

最后值得注意的是此题还要用离散化。。因为并没有告诉我们输入的数据有多大。。。。。(其实不用离散化,离散化是因为用的基数排序,快排就没这个问题了)


然后就是把第一段截掉剩余的东西翻转,复制一遍接在后面,再做后缀数组。

程序里的第二个循环值得注意,当p2=m-sa[i]+1+p1 p1<p2<n时成立退出,为什么是这个式子?这里我也不太懂,但是我们可以发现,当sa[i]位于复制后的字符串的前一段是才可以,那么sa[i]肯定是小于m的,

8 7 6 5 4 3 8 7 6 5 4 3 sa[i]=3

注意,是sa[i]=3,所以我们截得的字符串不是8 7 6和5 4 3 而是8 7和6 5 4 3,因为sa[i]表示的是这个位置到结尾的后缀。所以我们的第二串长度是m-sa[i]+1,在原串中的位置是p1+len=p1+m-sa[i]+1

那么我们可以发现p1<p2<n。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 400010
int n,m,k;
int a[N],rank[N],temp[N],sa[N],rev[N];
bool cp(int i,int j)
{
if(rank[i]!=rank[j]) return rank[i]<rank[j];
int ri=i+k<=n?rank[i+k]:-;
int rj=j+k<=n?rank[j+k]:-;
return ri<rj;
}
void Sa(int a[],int n)
{
for(int i=;i<=n;i++)
{
sa[i]=i; rank[i]=a[i];
}
for(k=;k<=n;k*=)
{
sort(sa+,sa+n+,cp);
temp[sa[]]=;
for(int i=;i<=n;i++)
temp[sa[i]]=temp[sa[i-]]+(cp(sa[i-],sa[i]));
for(int i=;i<=n;i++) rank[i]=temp[i];
}
}
void solve()
{
reverse_copy(a+,a+n+,rev+);
Sa(rev,n);
int p1=;
for(int i=;i<=n;i++)
{
p1=n-sa[i]+;
if(p1>&&n-p1>=) break;
}
memset(rev,,sizeof(rev));
int m=n-p1;
reverse_copy(a+p1+,a+n+,rev+);
reverse_copy(a+p1+,a+n+,rev+m+);
Sa(rev,*m);
int p2=;
for(int i=;i<=*m;i++)
{
p2=m-sa[i]++p1;
if(p2>p1&&p2<n) break;
}
for(int i=p1;i>=;i--) printf("%d\n",a[i]);
for(int i=p2;i>p1;i--) printf("%d\n",a[i]);
for(int i=n;i>p2;i--) printf("%d\n",a[i]);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
solve();
return ;
}

poj3581的更多相关文章

  1. [POJ3581]Sequence

    [POJ3581]Sequence 题目大意: 给定序列\(A_{1\sim n}\),其中\(A_1\)为最大的数.要把这个序列分成\(3\)个非空段,并将每一段分别反转,求能得到的字典序最小的序列 ...

  2. POJ3581 Sequence —— 后缀数组

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

  3. POJ3581 Sequence(后缀数组)

    题意:给一个串,串的第一个字符比后面的都大,要把它分成三段,然后反转每一段,求能得到的字典序最小的串是什么. 首先,第一段是可以确定的:把原串反转,因为第一个字符是最大的,它是唯一的,不存在反转串的后 ...

  4. POJ3581:Sequence(后缀数组)

    Description Given a sequence, {A1, A2, ..., An} which is guaranteed A1 > A2, ..., An,  you are to ...

  5. POJ3581 后缀数组

    http://poj.org/problem?id=3581 这题说是给了N个数字组成的序列A1 A2 ..An 其中A1 大于其他的数字 , 现在要把序列分成三段并将每段分别反转求最小字典序 以后还 ...

  6. POJ3581:Sequence——题解

    http://poj.org/problem?id=3581 给一串数,将其分成三个区间并且颠倒这三个区间,使得新数列字典序最小. 参考:http://blog.csdn.net/libin56842 ...

  7. 【后缀数组】poj3581 Sequence

    考虑第一次切割,必然切割的是翻转后字典序最小的前缀,伪证: 若切割位置更靠前:则会导致第一个数翻转后更靠前,字典序必然更大. 若切割位置更靠后,则显然也会导致字典序更大. ↑,sa即可 对于第二次切割 ...

  8. sa learning

    后缀数组之前一直在给队友搞,但是这个类太大了,预感到青岛八成会有,于是自己也学习一下,记录一下做题的历程 所用的模板暂时来自于队友的倍增nlogn da算法 int t1[maxn] , t2[max ...

随机推荐

  1. 自己动手写计算器v1.1

    这个改动主要是使用工厂模式替代了简单工厂模式,这样做的好处是如果以后我们要扩充其他运算时,就不用总是去修改工厂类, 这是可以采取工厂模式,主要是将原来简单工厂类的逻辑判断分离出来,将它作为一个借口,与 ...

  2. [python] File path and system path

    1. get files in the current directory with the assum that the directory is like this: a .py |----dat ...

  3. 【转】ZigBee终端入网方式深入分析

    前述 继之前对终端Direct Join的分析,发现很多东西还很模糊,存在很多问题.终于找到时间继续深入挖下去,这次应该比较完整地搞清了终端的入网机制,并纠正之前的几个认识偏差. 由于Z-Stack网 ...

  4. Eclipse代码和xml文件的智能提示

    一.代码智能提示 Windows → Preferences → Java→ Editor → Content Assist 将 Auto activation delay(ms): 改为 0 将 A ...

  5. Linux下Weblogic创建域方法和步骤

    Weblogic 创建域 以weblogic帐号登录(与创建域目录相对应账户) cd /home/weblogic/bea/weblogic92/common/bin 执行./config.sh进入配 ...

  6. PHP函数基础知识.png

  7. 服务发现与健康监测框架Consul-DNS转发的应用

    关于Consul Consul是一个提供服务注册与发现,健康监测,Key/Value存储以及多数据中心存储的分布式框架.官网地址是https://www.consul.io/,公司初步应用后我们老大觉 ...

  8. iOS开发中常用的设计模式

    常用的设计模式(一)代理模式应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则:开放-封闭原则实例:tableview的 数据源delegate,通过 ...

  9. 用 Excel 测试“绘制两点间连线”的算法

    最近在研究和制作数字示波器,其中涉及一个小算法:需要将 ADC 采样的数值在 TFT LCD 屏幕上面显示并且用“线”连接起来. ADC 按照时序对输入电压采样后,记录的是一个个的数值,如果显示的时候 ...

  10. android AES 加密

    import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import jav ...