E - Young Maids


Time limit : 2sec / Memory limit : 256MB

Score : 800 points

Problem Statement

Let N be a positive even number.

We have a permutation of (1,2,…,N)p=(p1,p2,…,pN). Snuke is constructing another permutation of (1,2,…,N)q, following the procedure below.

First, let q be an empty sequence. Then, perform the following operation until p becomes empty:

  • Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.

When p becomes empty, q will be a permutation of (1,2,…,N).

Find the lexicographically smallest permutation that can be obtained as q.

Constraints

  • N is an even number.
  • 2≤N≤2×105
  • p is a permutation of (1,2,…,N).

Input

Input is given from Standard Input in the following format:

N
p1 p2 pN

Output

Print the lexicographically smallest permutation, with spaces in between.


Sample Input 1

4
3 2 4 1

Sample Output 1

3 1 2 4

The solution above is obtained as follows:

p q
(3,2,4,1) ()
(3,1) (2,4)
() (3,1,2,4)

Sample Input 2

2
1 2

Sample Output 2

1 2

Sample Input 3

8
4 6 3 2 8 5 7 1

Sample Output 3

3 1 2 7 4 6 8 5

The solution above is obtained as follows:

p q
(4,6,3,2,8,5,7,1) ()
(4,6,3,2,7,1) (8,5)
(3,2,7,1) (4,6,8,5)
(3,1) (2,7,4,6,8,5)
() (3,1,2,7,4,6,8,5)

//题意:给出一个 p 序列,一个 q 序列,p 中有 1 -- n 的某种排列,q初始为空,现要求可以每次选两个相邻的数,移动到 q 中,要操作到 p 为空,并且要求 q 序列字典序最小

题解:相当难想到的一个题,假如有一个区间 l,r,肯定是,选择这区间内与 l 奇偶相同的,并且最小的作为开头,这样才能保证字典序最小,然后第二个数,应该为选的数右边的,并且与 l 奇偶性不同的,作为第二个,也要保证字典序最小,这部分是标准RMQ问题,随便用什么。然后,区间被分割成最多三块,用优先队列保存区间即可,排队顺序为区间内与左端点奇偶相同的位置上值小的先出队  n*lgn

还是难以想到啊,很好的题

 # include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define LL long long # define eps 1e-
# define MOD
# define INF 0x3f3f3f3f
inline int Scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int MX=;
//Code begin.... int n;
int dat[MX];
int ans[MX];
int mn[MX];
int st[][MX][];
struct Qu
{
int l,r;
int dex; //zui xiao ,,biao hao
bool operator < (const Qu b) const{
return dat[dex]>dat[b.dex];
}
};
void Init()
{
mn[]=-;
dat[]=INF;
for (int i=;i<=n;i++)
{
if ((i&(i-))==) mn[i]=mn[i-]+;
else mn[i]=mn[i-]; if (i&) st[][i][]=i;
else st[][i][]=i;
} for (int j=;(<<j)<=n;j++)
{
for (int i=;(i+(<<j)-)<=n;i++)
{
if (dat[st[][i][j-]]<=dat[st[][i+(<<(j-))][j-]])
st[][i][j] = st[][i][j-];
else
st[][i][j] = st[][i+(<<(j-))][j-]; if (dat[st[][i][j-]]<=dat[st[][i+(<<(j-))][j-]])
st[][i][j] = st[][i][j-];
else
st[][i][j] = st[][i+(<<(j-))][j-];
}
}
}
int st_cal(int l,int r,int same)
{
int k = mn[r-l+];
int c;
if (same) c=l%;
else c=(l+)%; if (dat[st[c][l][k]]<=dat[st[c][r-(<<k)+][k]])
return st[c][l][k];
else
return st[c][r-(<<k)+][k];
} int main()
{
scanf("%d",&n);
for (int i=;i<=n;i++)
dat[i] = Scan();
Init();
priority_queue<Qu> Q;
Q.push((Qu){,n,st_cal(,n,)});
int pp=;
for (int i=;i<=n/;i++)
{
Qu now = Q.top();Q.pop();
int ml = now.dex;
int mr = st_cal(ml+,now.r,);
ans[pp++]=dat[ml];
ans[pp++]=dat[mr];
if (ml>now.l)
Q.push( (Qu){now.l,ml-,st_cal(now.l,ml-,)} );
if (mr->ml)
Q.push( (Qu){ml+,mr-,st_cal(ml+,mr-,)} );
if (mr<now.r)
Q.push( (Qu){mr+,now.r,st_cal(mr+,now.r,)} );
}
for (int i=;i<pp-;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[pp-]);
return ;
}

Young Maids的更多相关文章

  1. AtCoder Regular Contest 080 (ARC080) E - Young Maids 线段树 堆

    原文链接http://www.cnblogs.com/zhouzhendong/p/8934377.html 题目传送门 - ARC080 E - Young Maids 题意 给定一个长度为$n$的 ...

  2. 【AtCoder Regular Contest 080E】Young Maids [堆][线段树]

    Young Maids Time Limit: 50 Sec  Memory Limit: 512 MB Description 给定一个排列,每次选出相邻的两个放在队头,要求字典序最小. Input ...

  3. AtCoder Regular Contest 080 E - Young Maids

    地址:http://arc080.contest.atcoder.jp/tasks/arc080_c 题目: E - Young Maids Time limit : 2sec / Memory li ...

  4. Atcoder arc080E Young Maids(线段树+优先队列)

    给出一个n排列,每次可以选择相邻的两个数字放在新的排列首部,问最后形成的新的排列字典序最小是? 考虑新排列的第一个数字,则应是下标为奇数的最小数,下标不妨设为i.第二个数字应该下标大于i且为偶数的最小 ...

  5. 【递归】【线段树】【堆】AtCoder Regular Contest 080 E - Young Maids

    给你一个1~n的排列p,n是偶数,每次从中任选一对相邻的数出来,插到排列q的开头,如此循环,问你所能得到的字典序最小的排列q. 我们先确定q开头的两个数q1,q2,q1一定是p的奇数位的最小的数,而q ...

  6. 【Atcoder】ARC 080 E - Young Maids

    [算法]数学+堆 [题意]给定n个数的排列,每次操作可以取两个数按序排在新序列的头部,求最小字典序. [题解] 转化为每次找字典序最小的两个数按序排在尾部,则p1和p2的每次选择都必须满足:p1在当前 ...

  7. AtCoder Regular Contest 080 E:Young Maids

    题目传送门:https://arc080.contest.atcoder.jp/tasks/arc080_c 题目翻译 给你一个\(n\)的排列\(p\),一个空序列\(q\),你每次可以从\(p\) ...

  8. AT2688 [ARC080C] Young Maids

    一道挺有意思的题目,在这里记录一下. 题目大意 给你一个长度为 \(n\) 的排列,每一次你可以取出相邻的两个数将其放在答案序列的开头,最后问你字典序最小的答案序列是什么. 题解 由于最后是求字典序最 ...

  9. Atcoder 乱做

    最近感觉自己思维僵化,啥都不会做了-- ARC103 F Distance Sums 题意 给定第 \(i\) 个点到所有点的距离和 \(D_i\) ,要求构造一棵合法的树.满足第 \(i\) 个点到 ...

随机推荐

  1. A – EIGHT

    八数码转换问题-- 经典bfs-- 关键问题: 1.状态的保存(见longwuxu该题解题报告中的全排列Hash表示) 2.bfs中标记数组的处理:     bfs中有两个标记数组,一个是标记队列中节 ...

  2. 将python对象序列化成php能读取的格式(即能反序列化到对象)

    转载自:http://my.oschina.net/zuoan001/blog/94914 代码如下: #coding:utf-8 # vim: encoding=utf-8:ft=python:et ...

  3. RabbitMQ快速入门python教程

    摘要:HelloWorld 简介 RabbitMQ:接受消息再传递消息,可以视为一个“邮局”.发送者和接受者通过队列来进行交互,队列的大小可以视为无限的,多个发送者可以发生给一个队列,多个接收者也可以 ...

  4. javascript原型的改动与重写(覆盖)区别

    每一个JavaScript函数都有prototype属性(javascript对象没有这个属性),这个属性引用了一个对象,这个对象就是原型对象.javascript同意我们改动这个原型对象. 改动有2 ...

  5. POJ训练计划3096_Surprising Strings(STL/map)

    解题报告 id=3096">题目传送门 题意: 给一个字符串,要求.对于这个字符串空隔为k取字符对(k=0,1,2,3,4...)要求在同样的空隔取对过程汇总.整个字符串中没有一个同样 ...

  6. linux系统日志报错

    使用journalctl -xe命令时报错: Error was encountered while opening journal files: Input/output error 日志文件损坏了 ...

  7. sql 转 markdown

    https://github.com/2liang/AutoBuildDocFromDB SQL脚本生成数据字典 http://www.jianshu.com/p/f491d0d3c503 这两个脚本 ...

  8. Failed to import package with error: Couldn't decompress package

    解压unitypackage的时候出错.原因是路径中包括中文字符,更改成英文路径就可以. 參考 Error while importing package: Couldn't decompress p ...

  9. SGDMA

    Scatter-gather DMA 使用一个链表描述物理上不连续的存储空间,然后把链表首地址告诉DMA master.DMA master在传输完一块物理连续的数据后,不用发起中断,而是根据链表来传 ...

  10. 如何给unity3d工程加入依赖的android工程

    最近在忙着接平台的事,需要接入各种各样的android平台sdk来发布.在接sdk的时候遇到了这样的一个情况,有点麻烦,所以纪录一下. 有些sdk的接入是提供jar包,这样的可以简单地将jar包制作成 ...