题目链接
题意:
一个长度为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;
}