转载请注明出处: 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. PHP接收JSON格式的数据

    在API服务中,目前流行采用json形式来交互. 给前端调用的接口输出Json数据,这个比较简单,只需要组织好数据,用json_encode($array) 转化一下,前端就得到json格式的数据. ...

  2. 使用注解@Transient使表中没有此字段

    注意,实体类中要使用org.springframework.data.annotation.Transient 在写实体类时发现有加@Transient注解的 加在属性声明上,但网上有加到get方法上 ...

  3. UITableView属性和方法

    1.初始化一个UITableView - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style struct CGRect { C ...

  4. libc++abi.dylib: terminate_handler unexpectedly threw an exception错误小结

    说法一: 我们在运行xcode工程时,有时候会遇到”libc++abi.dylib: terminate_handler unexpectedly threw an exception”错误,app莫 ...

  5. jsp跳转到servlet

    web.xml中url-pattern的值必须和相关联的jsp页面form中的action的值一样,才会从jsp页面跳转到servlet.

  6. Windows文件居然有解锁一说,并且还会引起SignTool Error,真是昏倒!

    I'm running Windows 7 and when I try to run a batch file, it says, "The publisher could not be ...

  7. linux下ifconfig, DNS以及route配置

    转载:http://blog.csdn.net/wangjingfei/article/details/5283632/ 熟悉使用ifconfig 会非常方便. ifconfig eth0 新ip 然 ...

  8. Android模仿微信语音聊天功能

    项目效果如下: 项目目录结构如下: 代码如下: AudioManager.java import java.io.File; import java.io.IOException; import ja ...

  9. oracle pipelined返回值函数 针对数据汇总统计 返回结果集方法

    近期需要一个汇总统计,由于数据太多,数据量太大所以在java程序中实现比较困难.若用后台程序统计,数据不能保证实时,同时实现周期比较长.顾使用函数返回结果集的方式,在不增加临时表的情况下实时获取数据. ...

  10. 编译不通过:提示XXXX不是类或命名空间名 的解决办法

    手动写了一个类,需要引入预编译头stdafx.h.结果编译时提示XXXX不是类或命名空间名. 处理方法:将#include "stdafx.h"放在最前面.