cf构造题(10/50)D. Constructing the Array(优先队列)

题目链接
在这里插入图片描述
题意:
一个长度为n的数组,初始全为为0,设置一个时间戳,每次进行一次操作,选择一段区间,将区间的中点更新成时间戳的值,
直到区间中所有数都进行完操作,区间的选择应该优先长度最大,然后左端点最小的顺序来枚举
题目分析:
优先队列维护区间左右端点模拟一遍即可,但语法没写对
需要复习优先队列的自定义排序语法
代码:

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define int long long
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
typedef set<int>::iterator SIT;
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
int gcd(int a,int b) {return b?gcd(b,a%b):a;}
int lcm(int a,int b) {return a/gcd(a,b)*b;}
const double eps=1e-6;
const int INF=0x3f3f3f3f,mod=998244353;
const int N=2e5+10;
int n,id[N];
struct node{
    int len,l,r;
    bool operator > (const node& a)const{
        if(len!=a.len) return len<a.len;
        else return l>a.l;
    }
};
signed main(){
    fast;
    int T;
    cin>>T;
    while(T--){
        cin>>n;
        priority_queue<node,vector<node>,greater<node>>q;
        q.push(node{n,1,n});
        int k=1;
        while(q.size()){
            auto t=q.top();
            q.pop();
            int l=t.l,r=t.r;
            int mid=(l+r)/2;
            id[mid]=k++;
            //debug(mid);
            if(l<=mid-1) q.push(node{mid-l,l,mid-1});
            if(r>=mid+1) q.push(node{r-mid,mid+1,r});
        }
        for(int i=1;i<=n;i++)
            cout<<id[i]<<' ';
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值