codeforces #642(div3) D. Constructing the Array(优先队列)

题目链接:D. Constructing the Array

在这里插入图片描述
input

6
1
2
3
4
5
6

1
1 2
2 1 3
3 1 2 4
2 4 1 3 5
3 4 1 5 2 6

题意很简单,就是让你找最长的0串,然后在这串中间加上当前操作数。比赛的时候想错了,总想是到规律题,其实是模拟。(STL NB)

#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<math.h>
#include<map>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3f
const double PI=acos(-1.0);
const double eps=1e-8;
const LL N=1e6+10;
int ans[N];
struct zxc
{
    int l,r;
     friend bool operator < (zxc q,zxc w)
     {
         if((q.r-q.l)==(w.r-w.l))
         {
             return q.l>w.l;
         }
         else
         {
             return (q.r-q.l)<(w.r-w.l);
         }
     }

}st;
priority_queue<zxc>q;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        zxc x;zxc y;
        scanf("%d",&n);
        st.l=1,st.r=n;
        q.push(st);
        for(int i=1;i<=n;i++)
        {
            st=q.top();
            q.pop();
            ans[(st.l+st.r)/2]=i;
            if((st.l+st.r)%2==0&&st.l!=st.r)
            {
                x.l=st.l;
                x.r=(st.l+st.r)/2-1;
                y.l=(st.l+st.r)/2+1;
                y.r=st.r;
                q.push(x);
                q.push(y);
            }
            else if((st.l+st.r)%2)
            {
               x.l=(st.l+st.r)/2+1;
               x.r=st.r;
               q.push(x);
               y.l=st.l;
                   y.r=(st.l+st.r)/2-1;
               if(y.r>=y.l)
               {

                   q.push(y);
               }
            }
        }
        for(int i=1;i<=n;i++)
        {
            printf("%d ",ans[i]);
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43402296/article/details/106138260