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

本文解析了一个构建特定数组的算法题目,通过寻找最长的0串并在其中间插入当前操作数,使用优先队列进行高效处理。文章详细介绍了算法的实现过程,包括数据结构的设计和关键步骤的说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值