Codeforces Round #642 (Div. 3) d

题目思路

用优先队列储存段落信息 记录位置和长度 以长度排序长的在前面
如果长度相同 要让左端点小的在左边(应为这个wa了几发)
然后就是模拟一个二分的过程
但是还是写了挺久 这种模拟题还是写少了
有些细节注意不到

ac代码

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#define pi 3.1415926535898
#define ll long long
#define lson rt<<1
#define rson rt<<1|1
#define eps 1e-6
#define ms(a,b) memset(a,b,sizeof(a))
#define legal(a,b) a&b
#define print1 printf("111\n")
using namespace std;
const int maxn = 2e6+10;
const int inf = 0x3f3f3f3f;
const ll llinf =0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;

int a[maxn];
struct node
{
    int l,r,len;
    node(int l,int r,int len)
    {
        this->l=l;
        this->r=r;
        this->len=len;
    }
    bool operator<(const node&x)const
    {
        if(len==x.len)return l>x.l;
        return len<x.len;
    }
};

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        priority_queue<node>q;
        q.push(node(1,n,n));
        int tem=0;
        while(!q.empty())
        {
            tem++;
            node b=q.top();
            q.pop();
            int mid=(b.l+b.r)>>1;
            a[mid]=tem;
            if(b.l<mid)q.push(node(b.l,mid-1,mid-b.l));
            if(b.r>mid)q.push(node(mid+1,b.r,b.r-mid));
        }
        for(int i=1;i<=n;i++)
            printf("%d ",a[i]);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/daydreamer23333/article/details/107628653