AtCoder Regular Contest 080E Young Maids 堆+RMQ

36 篇文章 0 订阅
33 篇文章 0 订阅

Description


给一个n排列p[],每次可以从中选取两个连续的元素拿出来,按照原本顺序放进一个队列q[]的前端
问字典序最小的q
n ≤ 2 ∗ 1 0 5 n\le2*10^5 n2105

Solution


很容易想到找最小的数作为开头元素,并且可以发现假如我们选择了某个位置x,那么另一个位置y一定和x奇偶性不同,并且x-1和n-y必须是偶数
那么就是十分好做了。我们按照奇偶性把序列拆成两份,每次从堆里面找到最小的区间,然后把区间拆成[l,x-1],[x+1,y-1],[y+1,r]三份再塞回堆里面。根据上面的结论不难发现我们两次选取的四个位置一定不会跨越,那么这样做就是对的了

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

const int INF=0x3f3f3f3f;
const int N=400005;

int w[N],n;

struct data {
	int l,r,w1,w2;
	bool operator <(data b) const {
		return (w[w1]==w[b.w1])?(w[w2]>w[b.w2]):(w[w1]>w[b.w1]);
	}
} ;

struct RMQ {
	int rec[18][N],lg[N];

	void pre() {
		rep(i,2,N-1) lg[i]=lg[i>>1]+1;
		rep(j,1,lg[n]) {
			rep(i,1,n-(1<<j)+1) {
				if (w[rec[j-1][i]]>w[rec[j-1][i+(1<<j-1)]]) {
					rec[j][i]=rec[j-1][i+(1<<j-1)];
				} else rec[j][i]=rec[j-1][i];
			}
		}
	}

	int ask(int l,int r) {
		int t=lg[r-l+1];
		if (w[rec[t][l]]<w[rec[t][r-(1<<t)+1]]) return rec[t][l];
		else return rec[t][r-(1<<t)+1];
	}
} T[2];

std:: priority_queue <data> heap;

int read() {
	int x=0,v=1; char ch=getchar();
	for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):v,ch=getchar());
	for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
	return x*v;
}

int main(void) {
	freopen("data.in","r",stdin);
	n=read(),w[0]=INF;
	rep(i,1,n) {
		w[i]=read();
		T[i&1].rec[0][i]=i;
		T[!(i&1)].rec[0][i]=0;
	}
	T[0].pre(),T[1].pre();
	int p1=T[1].ask(1,n-1);
	int p2=T[0].ask(p1+1,n);
	heap.push((data) {1,n,p1,p2});
	for (int i=1;i*2<=n;++i) {
		data top=heap.top(); heap.pop();
		int l=top.l,r=top.r,w1=top.w1,w2=top.w2,wjp;
		printf("%d %d ", w[top.w1],w[top.w2]);
		if (w1>l+1) {
			wjp=l&1;
			p1=T[wjp].ask(l,w1-2);
			p2=T[!wjp].ask(p1+1,w1-1);
			heap.push((data) {l,w1-1,p1,p2});
		}
		if (w2+1<r) {
			wjp=(w2+1)&1;
			p1=T[wjp].ask(w2+1,r-1);
			p2=T[!wjp].ask(p1+1,r);
			heap.push((data) {w2+1,r,p1,p2});
		}
		if (w1+1<w2) {
			wjp=(w1+1)&1;
			p1=T[wjp].ask(w1+1,w2-2);
			p2=T[!wjp].ask(p1+1,w2-1);
			heap.push((data) {w1+1,w2-1,p1,p2});
		}
	} puts("");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值