未过样例,萌新求调
查看原帖
未过样例,萌新求调
752706
hyfzelda楼主2023/1/16 19:38

rt

#include<bits/stdc++.h>
using namespace std;
int n,p[200005],f[2000005][20],llog[200005];
int query(int l,int r)
{
	int len=llog[r-l+1];
    return min(f[l][len],f[r-(1<<len)+1][len]);
}
struct node{
    int l,r,x,y;
    node(const int a=0,const int b=0):l(a),r(b),x(query(a,b)),y(query(x+1,b+1)){}
    bool operator<(const node &now)const{
        return p[x]>p[now.x];
    }
}tmp;

priority_queue<node>q;
int main(){
    scanf("%d",&n); 
    p[0]=n+1;
    llog[0]=-1;
    for(int i=1;i<=n;i++)
	{
        scanf("%d",p+i);
        f[i][0]=f[i][1]=i;
        llog[i]=llog[i/2]+1;
    }
    for(int i=n;i;i--)
    {
    	for(int j=2;i+(1<<j)-1<=n+1;j++)
    	{
    		f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
		}  
	}
    q.push(node(1,n));
    while(!q.empty())
	{
        tmp=q.top(),q.pop();
        printf("%d %d ",p[tmp.x],p[tmp.y]);
        if(tmp.l<tmp.x)
		{
			q.push(node(tmp.l,tmp.x-1));
		}
        if(tmp.x+1<tmp.y)
		{
			q.push(node(tmp.x+1,tmp.y-1));
		}
        if(tmp.y<tmp.r)
		{
			q.push(node(tmp.y+1,tmp.r));
		}
    }
}
2023/1/16 19:38
加载中...