转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Sequence
Time Limit: 5000MS   Memory Limit: 65536K
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}

题意:

给出n个数,把这个数列分为三段,再把三段反转后连接在一起成为一个新串,求字典序最小的新串。

思路:

由于第一个数保证比其他所有数要大,在取第一段时直接取反转后的字典序最小的后缀即可。利用后缀数组即可求得。

而后把剩下的一个字符串中分成两段,使得其字典序最小。首先我们会很容易的想到向第一段一样的方法,即取反转之后的字典序最小的后缀。

但对于这种方法,我们很容易就能找到一组反例,即10 1 2 2 3

按照上述的方法,我们会取得第一段为10 1,反转,变为1 10

而后剩下的字符串为2 2 3,对于此串,反转之后为3 2 2,字典序最小的为2.整个串则变为1 10 2 3 2

显然当我们取2 2的时候整个串的字典序才是最小的,为1 10 2 2 3。

对于一个长度为m的串s[1]……s[m],设我们取将其分成s[1]……s[k]和s[k+1]……s[m]

将其反转之后则为s[k]……s[1]s[m]……s[k+1],我们可以发现,这个串正是s[m]……s[k+1]s[k]……s[1]s[m]……s[k+1]s[k]……s[1]的子串,并且我们可以通过找这个串的长度大于m字典序最小的后缀从而得到答案。

对于上例3 2 2,可变成3 2 2 3 2 2,长度大于m的字典序最小的后缀就是2 2 3 2 2。取其前一段即为所求的第二段。

另外,本题必须为单组输入,否则会WA,这点坑了我好久。。。。。

 #include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAXN 400010
int n,k;
int sa[MAXN],rank[MAXN],a[MAXN],b[MAXN],c[MAXN],tmp[MAXN];
bool cmp(int i,int j){
if(rank[i]!=rank[j])return rank[i]<rank[j];
else {
int ri=i+k<=n?rank[i+k]:-1e8;
int rj=j+k<=n?rank[j+k]:-1e8;
return ri<rj;
}
}
void build(int len,int *s){
n=len;
for(int i=;i<=n;i++)sa[i]=i,rank[i]=i<n?s[i]:-1e8;
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++)rank[i]=tmp[i];
}
}
int main()
{
int N;
scanf("%d",&N);
//while(scanf("%d",&N)!=EOF){
for(int i=;i<N;i++)scanf("%d",a+i);
for(int i=;i<N;i++)b[i]=a[N--i];
build(N,b);
int p1;
for(int i=;i<=N;i++){
p1=N-sa[i]-;
if(p1>=&&p1+<=n)break;
}
int m=N-p1-;
for(int i=;i<m;i++)c[i]=a[i+p1+];
for(int i=;i<m;i++)b[i]=b[i+m]=c[m--i];
build(*m,b);
int p2;
for(int i=;i<=*m;i++)
{
p2=m-sa[i]-;
if(p2>=&&p2<=m-)break;
}
p2+=p1+;
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]);
//}
return ;
}

代码君

poj3581Sequence(后缀数组)的更多相关文章

  1. 后缀数组的倍增算法(Prefix Doubling)

    后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...

  2. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  3. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  4. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  5. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  6. 后缀数组(suffix array)详解

    写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具. 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料. 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, ...

  7. 【UOJ #35】后缀排序 后缀数组模板

    http://uoj.ac/problem/35 以前做后缀数组的题直接粘模板...现在重新写一下模板 注意用来基数排序的数组一定要开到N. #include<cstdio> #inclu ...

  8. 【BZOJ-2119】股市的预测 后缀数组

    2119: 股市的预测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 334  Solved: 154[Submit][Status][Discuss ...

  9. 【BZOJ-4698】Sandy的卡片 后缀数组

    4698: Sdoi2008 Sandy的卡片 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 140  Solved: 55[Submit][Stat ...

随机推荐

  1. 【0】Laravel 5.1 简介

    1.简介 Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的网络APP,而且每行代码都可以 ...

  2. python 杂记

    class TestA(object): def __init__(self): print("A is initing"); def foo(self): print(" ...

  3. 使用pip install 或者easy_install安装Python的各种包出现cc failed with exit status 1

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  4. asp编程中获取上下两个月第一天和最后一天的代码

    经常在asp编程遇到要获取上个月第一天和最后一天的日期,获取下个月第一天和最后一天的日期.这里总结了一下,将这些asp代码全部列出来了,以便以后遇到的时候使用.    上个月第一天:<%=dat ...

  5. 双人贪吃蛇小游戏C++原创

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang /*贪吃蛇*/ #include<stdio.h> #include<time. ...

  6. CoreData概略

    CoreData概略 分类: IOS开发2013-10-25 16:19 178人阅读 评论(0) 收藏 举报 CoreData是IOS对数据进行对象化管理的一种高效方式,提供了对数据的增.删.查.改 ...

  7. LODS LODSB LODSW LODSD 例子【载入串指令】

    http://qwop.iteye.com/blog/1958761 // lodsb.cpp : Defines the entry point for the console applicatio ...

  8. Database JDBC Developer's Guide

    http://docs.oracle.com/database/121/JJDBC/datacc.htm#JJDBC28363

  9. 全局函数的Result一定要每次都初始化,否则上次的结果会被保持到下一次继续使用

    测试半天,原来是因为这个原因.下面例子中,Result:=''必须写,否则其结果会被累计,真是昏倒!! function MyPaths(tache: IXMLTaskType) : String; ...

  10. Intel 凌动 D525 产品参数Intel 凌动 Z3735F 产品参数

    https://item.taobao.com/item.htm?spm=a230r.1.14.8.kauehT&id=40450541158&ns=1&abbucket=19 ...