Date:2022.01.02
题意:有n个数初始全为0,要求构造满足下列条件的n个数。
思路:每次考虑最长的,如果等长先考虑左边的,不妨设一个struct存一下每个区间的左右端点排个序,顺序问题则解决了。之后用priority_queue每次找顺序最靠前的,如果最中间没填过就填最中间的,注意一个区间分叉再入队列至少其分叉前长度>=2,否则区间长度为1分叉会出错。【思路很简单,主要练一下pq怎么用以及重载】
代码如下:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
typedef long long LL;
LL t,n,m,k;
LL a[N];
struct node
{
LL l,r;
bool operator< (const node& s)const
{
if((r-l+1)!=(s.r-s.l+1)) return (r-l+1)<(s.r-s.l+1);
return l>s.l;
}
};
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++) a[i]=0;
priority_queue<node> q;
q.push({1,n});
LL cnt=0;
while(!q.empty())
{
node tt=q.top();q.pop();
LL l=tt.l,r=tt.r;
LL mid=l+r>>1;
if(a[mid]) continue;
a[mid]=++cnt;
if(r-l+1>1)
{
q.push({l,mid-1});
q.push({mid+1,r});
}
}
for(int i=1;i<=n;i++) cout<<a[i]<<' ';
cout<<endl;
}
}
本文介绍了一种解决动态区间填充问题的方法,利用优先队列(priority_queue)对区间进行排序,并确保每次选择最长未填充的中间位置。通过实例展示了如何在代码中实现这一思路,适用于需要维护区间顺序的场景。


被折叠的 条评论
为什么被折叠?



