Young Maids
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的更多相关文章
- AtCoder Regular Contest 080 (ARC080) E - Young Maids 线段树 堆
原文链接http://www.cnblogs.com/zhouzhendong/p/8934377.html 题目传送门 - ARC080 E - Young Maids 题意 给定一个长度为$n$的 ...
- 【AtCoder Regular Contest 080E】Young Maids [堆][线段树]
Young Maids Time Limit: 50 Sec Memory Limit: 512 MB Description 给定一个排列,每次选出相邻的两个放在队头,要求字典序最小. Input ...
- AtCoder Regular Contest 080 E - Young Maids
地址:http://arc080.contest.atcoder.jp/tasks/arc080_c 题目: E - Young Maids Time limit : 2sec / Memory li ...
- Atcoder arc080E Young Maids(线段树+优先队列)
给出一个n排列,每次可以选择相邻的两个数字放在新的排列首部,问最后形成的新的排列字典序最小是? 考虑新排列的第一个数字,则应是下标为奇数的最小数,下标不妨设为i.第二个数字应该下标大于i且为偶数的最小 ...
- 【递归】【线段树】【堆】AtCoder Regular Contest 080 E - Young Maids
给你一个1~n的排列p,n是偶数,每次从中任选一对相邻的数出来,插到排列q的开头,如此循环,问你所能得到的字典序最小的排列q. 我们先确定q开头的两个数q1,q2,q1一定是p的奇数位的最小的数,而q ...
- 【Atcoder】ARC 080 E - Young Maids
[算法]数学+堆 [题意]给定n个数的排列,每次操作可以取两个数按序排在新序列的头部,求最小字典序. [题解] 转化为每次找字典序最小的两个数按序排在尾部,则p1和p2的每次选择都必须满足:p1在当前 ...
- AtCoder Regular Contest 080 E:Young Maids
题目传送门:https://arc080.contest.atcoder.jp/tasks/arc080_c 题目翻译 给你一个\(n\)的排列\(p\),一个空序列\(q\),你每次可以从\(p\) ...
- AT2688 [ARC080C] Young Maids
一道挺有意思的题目,在这里记录一下. 题目大意 给你一个长度为 \(n\) 的排列,每一次你可以取出相邻的两个数将其放在答案序列的开头,最后问你字典序最小的答案序列是什么. 题解 由于最后是求字典序最 ...
- Atcoder 乱做
最近感觉自己思维僵化,啥都不会做了-- ARC103 F Distance Sums 题意 给定第 \(i\) 个点到所有点的距离和 \(D_i\) ,要求构造一棵合法的树.满足第 \(i\) 个点到 ...
随机推荐
- hdu1698 Just a Hook (线段树区间更新 懒惰标记)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- eval(data)和eval("("+data+")")的区别
如果data是字符串,使用eval("("+data+")")可以将其转换为json对象,和JSON.parse的功能一样.如果data是json对象,使用ev ...
- Vue webpack配置文件
一.代码地址 github:https://github.com/MengFangui/VueWebpackConfig 二.配置说明 1.命令 (1)npm i 安装依赖包 (2)num run d ...
- 【Python3 爬虫】16_抓取腾讯视频评论内容
上一节我们已经知道如何使用Fiddler进行抓包分析,那么接下来我们开始完成一个简单的小例子 抓取腾讯视频的评论内容 首先我们打开腾讯视频的官网https://v.qq.com/ 我们打开[电视剧]这 ...
- spring学习笔记(六)
1.配置环绕通知 需要实现的接口为 MethodInterceptor 代码举例 package com.huawei.aop; import org.aopalliance.intercept ...
- 转:Window10下RabbitMQ安装图文教程
一.erlang下载安装 1.下载 下载erlang,原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的,下载地址:http://www.erlang.org/downloads,双 ...
- Atitit.js的键盘按键事件捆绑and事件调度
Atitit.js的键盘按键事件捆绑and事件调度 1. Best的方法还是 objEvtMap[ id+evt ]=function(evt,element) 2. Event bind funct ...
- JQuery常用操作实现方式
http://www.cnblogs.com/guomingfeng/articles/2038707.html 一个优秀的 JavaScript 框架,一篇 jQuery 常用方法及函数的文章留存备 ...
- 转:关于安卓多线程while(true)方法占用CPU高的原因及其解决方法
由于项目需要用到安卓多线程操作,结果开了四条线程,下载到平板一直很卡,CPU占用率暴涨.于是开始查找原因,发现是线程run()方法里的while(true)导致的, 下图是为解决时开启一条while( ...
- libevent源码学习_event_test
对应的sample文件中提供了event_test.c,里面就是关于事件的简单示例,具体如下: /* * Compile with: * cc -I/usr/local/include -o even ...