地址: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:

  1. N
  2. p1 p2 pN

Output

Print the lexicographically smallest permutation, with spaces in between.

思路:

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

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define MP make_pair
  6. #define PB push_back
  7. typedef long long LL;
  8. typedef pair<int,int> PII;
  9. const double eps=1e-;
  10. const double pi=acos(-1.0);
  11. const int K=3e5+;
  12. const int mod=1e9+;
  13.  
  14. int n,vb[*K],vc[*K],ff[*K],hs[K],a[K];
  15. //vb奇数,vc偶数
  16. void push_down(int o)
  17. {
  18. if(ff[o])
  19. {
  20. swap(vb[o<<],vc[o<<]);
  21. swap(vb[o<<|],vc[o<<|]);
  22. ff[o<<]^=ff[o],ff[o<<|]^=ff[o];
  23. ff[o]=;
  24. }
  25. }
  26. void update(int o,int l,int r,int pos,int x)
  27. {
  28. if(l==r)
  29. {
  30. if(pos&) vb[o]=x,vc[o]=K;
  31. else vb[o]=K,vc[o]=x;
  32. return ;
  33. }
  34. int mid=l+r>>;
  35. push_down(o);
  36. if(pos<=mid) update(o<<,l,mid,pos,x);
  37. else update(o<<|,mid+,r,pos,x);
  38. vb[o]=min(vb[o<<],vb[o<<|]);
  39. vc[o]=min(vc[o<<],vc[o<<|]);
  40. }
  41. void update2(int o,int l,int r,int nl,int nr)
  42. {
  43. if(l==nl&&r==nr)
  44. {
  45. ff[o]^=;swap(vb[o],vc[o]);
  46. return ;
  47. }
  48. int mid=l+r>>;
  49. push_down(o);
  50. if(nr<=mid) update2(o<<,l,mid,nl,nr);
  51. else if(nl>mid) update2(o<<|,mid+,r,nl,nr);
  52. else update2(o<<,l,mid,nl,mid),update2(o<<|,mid+,r,mid+,nr);
  53. vb[o]=min(vb[o<<],vb[o<<|]);
  54. vc[o]=min(vc[o<<],vc[o<<|]);
  55. }
  56. int query(int o,int l,int r,int nl,int nr)
  57. {
  58. if(l==nl&&r==nr)return vb[o];
  59. int mid=l+r>>;
  60. push_down(o);
  61. if(nr<=mid) return query(o<<,l,mid,nl,nr);
  62. else if(nl>mid) return query(o<<|,mid+,r,nl,nr);
  63. else return min(query(o<<,l,mid,nl,mid),query(o<<|,mid+,r,mid+,nr));
  64. }
  65. struct node
  66. {
  67. int l,r,pl,pr;
  68. node(){}
  69. node(int i,int j,int p,int q){l=i,r=j,pl=p,pr=q;}
  70. bool operator<(const node &ta)const
  71. {
  72. if(a[pl]==a[ta.pl]) return a[pr]>a[ta.pr];
  73. return a[pl]>a[ta.pl];
  74. }
  75. };
  76. node sc(int l,int r)
  77. {
  78. int x=query(,,n,l,r);
  79. update(,,n,hs[x],K);
  80. if(hs[x]+<=r)
  81. update2(,,n,hs[x]+,r);
  82. int y=query(,,n,hs[x]+,r);
  83. update(,,n,hs[y],K);
  84. if(hs[y]+<=r)
  85. update2(,,n,hs[y]+,r);
  86. return node(l,r,hs[x],hs[y]);
  87. }
  88. priority_queue<node>q;
  89. int main(void)
  90. {
  91. scanf("%d",&n);
  92. for(int i=,mx=n*;i<=mx;i++) vb[i]=vc[i]=K;
  93. for(int i=;i<=n;i++)
  94. {
  95. scanf("%d",a+i);
  96. update(,,n,i,a[i]);
  97. hs[a[i]]=i;
  98. }
  99. q.push(sc(,n));
  100. while(q.size())
  101. {
  102. node tmp=q.top();
  103. q.pop();
  104. printf("%d %d ",a[tmp.pl],a[tmp.pr]);
  105. if(tmp.pl->tmp.l) q.push(sc(tmp.l,tmp.pl-));
  106. if(tmp.pl+<tmp.pr-) q.push(sc(tmp.pl+,tmp.pr-));
  107. if(tmp.pr+<tmp.r) q.push(sc(tmp.pr+,tmp.r));
  108. }
  109. return ;
  110. }

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. 移动ChemDraw结构有什么方法

    ChemDraw软件是一款比较常见的化学绘图软件,化学专业的领域的人常常会用到它.本教程主要是针对新手用户,让其了解一些ChemDraw的一些基本操作,以便其能尽快上手早日用到工作中.下面我们就来给大 ...

  2. phpcms替换类列表页,内容页,主页

    phpcms替换类列表页,内容页,主页   利用phpcms制作企业站,首先要将静态的企业主页替换成后台可编辑的动态主页. 在phpcms/install_package/phpcms/templat ...

  3. 【BZOJ2560】串珠子 状压DP+容斥

    [BZOJ2560]串珠子 Description 铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子.现在铭铭想用绳子把所有的珠子连接成一个整体. 现在已知所有珠子互不相同,用整数1到n编号.对于第i个 ...

  4. Android ListView的监听事件

    Android开发时,最常用的控件之一就是ListView了,而使用ListView的同时,必然需要对它设置监听器,常用的监听器有这么几个1.OnItemClickListener 2.OnTouch ...

  5. PHP 基础知识代码总结

    一.PHP基础语法 变量到数组 <?php //phpinfo(); /* 变量 $a=1;//不分配空间 echo "\$a=".$a; echo "<br ...

  6. SpringMvc三大组件详解

    SpringMvc框架结构图 处理器映射器:用户请求路径到Controller方法的映射 处理器适配器:根据handler(controlelr类)的开发方式(注解开发/其他开发) 方式的不同区寻找不 ...

  7. js打字效果

    //文字依次出来效果 $.fn.autotype = function() { var $text = $(this); // console.log('this', this); var str = ...

  8. 170308、oracle查看被锁的表和解锁

    --以下几个为相关表SELECT * FROM v$lock;SELECT * FROM v$sqlarea;SELECT * FROM v$session;SELECT * FROM v$proce ...

  9. Pycharm取消默认的右击运行unittest方法

    Pycharm取消默认的右击运行unittest方法:File-> Settings -> Tools -> Python Integrated Tools -> Defaul ...

  10. IPTABLES简介

    iptables防火墙工作原理 简介:iptables防火墙工作在网络层,针对TCP/IP数据包实施过滤和限制,iptables防火墙基于内核编码实现,具有非常稳定的性能和高效率: iptables属 ...