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\) 个点到 ...
随机推荐
- Windows如何自定义U盘盘符、文件夹图标、文件夹背景
自定义U盘盘符.文件夹图标.文件夹背景 注意对于Vista和Win7的用户不支持文件夹图标和文件夹背景的更换 1.自定义盘符:在U盘根目录下新建文件 autorun.inf(可先建.txt文本文档,再 ...
- 【Python3 爬虫】12_代理IP的使用
我们在爬取页面的时候,如果长时间使用一个网址去爬取某个网站,就会受爬去限制,此时,我们引用了代理IP,IP随时在变化,也就不会被限制了 一下是国内提供免费代理IP的地址:http://www.xici ...
- InputStream写文件出现大量NUL
写文件大家出现最多的是汉字知码之类的问题,今天不是乱码问题,是出现在大量空字符,用记事本打开是不可见的.如果用NodePad++打开则会显示NUL 问题分题: 刚开始以为是编码问题,试了几个编码发现问 ...
- 用户自定义类型《lua程序设计》 28章 笔记
本实例实现一种很简单的类型------布尔数组.C语言可以实现将每个布尔值存储在一个bit中,从而减少内存用量. 必须的一些宏 Code Snippet #defineBITS_PER_WORD (C ...
- TCP/IP --概述
分层 网络协议通常分不同层次进行开发,每一层分别负责不同的通信功能.一个协议族,比如T C P / I P,是一组不同层次上的多个协议的组合.T C P / I P通常被认为是一个四层协议系统.,如图 ...
- 【CODEFORCES】 C. Table Decorations
C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...
- centos 7 查看修改时区
查看时区 date -R 修改时区 # timedatectl list-timezones # 列出所有时区 # timedatectl set-local-rtc 1 # 将硬件时钟调整为与本地时 ...
- 【收藏】15个常用的javaScript正则表达式(转)
0 前言 收集整理了15个常用的javaScript正则表达式,其中包括用户名.密码强度.整数.数字.电子邮件地址(Email).手机号码.身份证号.URL地址. IPv4地址. 十六进制颜色. 日期 ...
- [Oracle] - Install Oracle12cR1 on Oracle Linux 6.5 in VirtualBox
My Oralce Linux 6.5 is running on VirtualBox. Basic settings is 4G memory, 50G hard-disk, auto parti ...
- MySQL(8)--Cluster 7.4 rpm centos7
还没来得及写.我就知道一个月完毕不了,暂时添加的工作总是拖后腿. .. MySQL集群 docker下安装MySQL Cluster报错了.是我image的包少了perl.老实装吧.image又小不了 ...