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

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}

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 ;
}

最新文章

  1. commons configuration管理项目的配置文件
  2. 《Node web开发》笔记
  3. Flume_使用
  4. 在iframe中获取本iframe DOM引用
  5. ASP.NET Web API 通过Authentication特性来实现身份认证
  6. 安装微软ASP.NET MVC 4,运行以下的包管理器控制台命令
  7. highcharts 结合phantomjs纯后台生成图片系列二之php2
  8. 8张图带你理解Java整个只是网络(转载)
  9. C++中枚举定义运算符
  10. Java 8 vs. Scala(一): Lambda表达式
  11. mysql sql语句
  12. 多线程面试题系列(7):经典线程同步 互斥量Mutex
  13. 安卓9.0系统机器(亲测有效)激活Xposed框架的步骤
  14. company_credit
  15. win10 mac随机功能测试
  16. js异步下载文件请求
  17. 在CentOS 7中安装与配置Tomcat-8.5方法
  18. Linux 入侵检测小结
  19. [快速幂][NOIP2012]转圈游戏
  20. 【持续更新】NOIP注意事项

热门文章

  1. Hibernate课程 初探多对多映射2-4 测试
  2. [一个小问题]Mainfest配置文件的version问题小结
  3. org.springframework.beans.factory.BeanNotOfRequiredTypeException
  4. 磁盘空间满了之后MySQL会怎样
  5. simotion byte/word ASCII码转换为字符、字符串
  6. 关于(void**)及其相关的理解
  7. Android 中间白色渐变到看不见的线的Drawable
  8. 神奇的暴力数据结构——ODT
  9. 1.3配置存储单元(nbu重删池)
  10. P1540 机器翻译