地址:http://arc080.contest.atcoder.jp/tasks/arc080_c

题目:

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.

思路:

  不想自己写了,结合我代码看官方题解吧。

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=3e5+;
const int mod=1e9+; int n,vb[*K],vc[*K],ff[*K],hs[K],a[K];
//vb奇数,vc偶数
void push_down(int o)
{
if(ff[o])
{
swap(vb[o<<],vc[o<<]);
swap(vb[o<<|],vc[o<<|]);
ff[o<<]^=ff[o],ff[o<<|]^=ff[o];
ff[o]=;
}
}
void update(int o,int l,int r,int pos,int x)
{
if(l==r)
{
if(pos&) vb[o]=x,vc[o]=K;
else vb[o]=K,vc[o]=x;
return ;
}
int mid=l+r>>;
push_down(o);
if(pos<=mid) update(o<<,l,mid,pos,x);
else update(o<<|,mid+,r,pos,x);
vb[o]=min(vb[o<<],vb[o<<|]);
vc[o]=min(vc[o<<],vc[o<<|]);
}
void update2(int o,int l,int r,int nl,int nr)
{
if(l==nl&&r==nr)
{
ff[o]^=;swap(vb[o],vc[o]);
return ;
}
int mid=l+r>>;
push_down(o);
if(nr<=mid) update2(o<<,l,mid,nl,nr);
else if(nl>mid) update2(o<<|,mid+,r,nl,nr);
else update2(o<<,l,mid,nl,mid),update2(o<<|,mid+,r,mid+,nr);
vb[o]=min(vb[o<<],vb[o<<|]);
vc[o]=min(vc[o<<],vc[o<<|]);
}
int query(int o,int l,int r,int nl,int nr)
{
if(l==nl&&r==nr)return vb[o];
int mid=l+r>>;
push_down(o);
if(nr<=mid) return query(o<<,l,mid,nl,nr);
else if(nl>mid) return query(o<<|,mid+,r,nl,nr);
else return min(query(o<<,l,mid,nl,mid),query(o<<|,mid+,r,mid+,nr));
}
struct node
{
int l,r,pl,pr;
node(){}
node(int i,int j,int p,int q){l=i,r=j,pl=p,pr=q;}
bool operator<(const node &ta)const
{
if(a[pl]==a[ta.pl]) return a[pr]>a[ta.pr];
return a[pl]>a[ta.pl];
}
};
node sc(int l,int r)
{
int x=query(,,n,l,r);
update(,,n,hs[x],K);
if(hs[x]+<=r)
update2(,,n,hs[x]+,r);
int y=query(,,n,hs[x]+,r);
update(,,n,hs[y],K);
if(hs[y]+<=r)
update2(,,n,hs[y]+,r);
return node(l,r,hs[x],hs[y]);
}
priority_queue<node>q;
int main(void)
{
scanf("%d",&n);
for(int i=,mx=n*;i<=mx;i++) vb[i]=vc[i]=K;
for(int i=;i<=n;i++)
{
scanf("%d",a+i);
update(,,n,i,a[i]);
hs[a[i]]=i;
}
q.push(sc(,n));
while(q.size())
{
node tmp=q.top();
q.pop();
printf("%d %d ",a[tmp.pl],a[tmp.pr]);
if(tmp.pl->tmp.l) q.push(sc(tmp.l,tmp.pl-));
if(tmp.pl+<tmp.pr-) q.push(sc(tmp.pl+,tmp.pr-));
if(tmp.pr+<tmp.r) q.push(sc(tmp.pr+,tmp.r));
}
return ;
}

AtCoder Regular Contest 080 E - 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 080 E:Young Maids

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

  3. AtCoder Regular Contest 080 [CDEF]

    C - 4-adjacent Time limit : 2sec / Memory limit : 256MB Problem Statement We have a sequence of leng ...

  4. AtCoder Regular Contest 080

    手贱去开了abc,这么无聊.直接arc啊 C - 4-adjacent Time limit : 2sec / Memory limit : 256MB Score : 400 points Prob ...

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

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

  6. AtCoder Regular Contest 080 D - Grid Coloring

    地址:http://arc080.contest.atcoder.jp/tasks/arc080_b 题目: D - Grid Coloring Time limit : 2sec / Memory ...

  7. AtCoder Regular Contest 080 C - 4-adjacent

    地址:http://arc080.contest.atcoder.jp/tasks/arc080_a 题目: C - 4-adjacent Time limit : 2sec / Memory lim ...

  8. AtCoder Regular Contest 094 (ARC094) CDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...

  9. AtCoder Regular Contest 098

    AtCoder Regular Contest 098 C - Attention 题意 给定一个只包含"E","W"字符串,可以花一的花费使他们互相转换.选定 ...

随机推荐

  1. 目标检测YOLOv1-v3——学习笔记

    Fast RCNN更准一些.其损失函数比YOLO简单. YOLO更快 YOLO(You Only Look Once) 简介: 测试过程: 训练过程: 坐标.含有.不含.类别预测 目标检测的效果准确率 ...

  2. VC++中CEdit控件实现回车换行

    1.通过回车Enter换行: 这里要有两个设置 <1>.将控件的属性设置为Mutilines->true; <2>.将控件的另一个属性设置为Want return-> ...

  3. 【转】Native Thread for Win32 C- Creating Processes(通俗易懂,非常好)

    http://www.bogotobogo.com/cplusplus/multithreading_win32C.php To create a new process, we need to ca ...

  4. ArcGIS Android SDK 中文标注乱码

    Android使用如下代码添加标注: TextSymbol ts = new TextSymbol(12, "名称", Color.RED);Graphic gp = new Gr ...

  5. Android无线测试之—UiAutomator UiWatcher API介绍一

    UiWatcher类介绍与中断监听检查条件 一.UiWatcher类说明 1.Uiwatcher用于处理脚本执行过程中遇到非预想的步骤 2.UiWatcher使用场景 1)测试过程中来了一个电话 2) ...

  6. IOS开发学习笔记039-autolayout 代码实现

    本文转载至 http://www.cnblogs.com/songliquan/p/4548206.html 1.代码实现比较复杂 代码实现Autolayout的步骤 利用NSLayoutConstr ...

  7. C# 图片识别技术(支持21种语言,提取图片中的文字)

    图片识别的技术到几天已经很成熟了,只是相关的资料很少,为了方便在此汇总一下(C#实现),方便需要的朋友查阅,也给自己做个记号. 图片识别的用途:很多人用它去破解网站的验证码,用于达到自动刷票或者是批量 ...

  8. js访问CSS最终计算样式

    所谓计算样式,就是嵌入式样式.外部样式表.内联样式综合的样式表现,那么如何来获取呢? "DOM2 级样式"增强了document.defaultView,提供了getCompute ...

  9. Struts2中的类型转换失败

    类型转换失败: 若 Action 类没有实现 ValidationAware 接口: Struts 在遇到类型转换错误时仍会继续调用其 Action 方法, 就好像什么都没发生一样. 若 Action ...

  10. AOP通知无法切入指定方法

    AOP通知,切入指定方法时拦截不到,可能是拦截的方法本身是被本类的其他方法调用的,根据AOP反射原理是无法拦截本类中方法调用的方法的.如: class AImpl implements AIf { s ...