Codeforces Round #642 (Div. 3) D. Constructing the Array(优先队列)
You are given an array aa of length nn consisting of zeros. You perform nn actions with this array: during the ii -th action, the following sequence of operations appears:
- Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one;
- Let this segment be [l;r][l;r] . If r−l+1r−l+1 is odd (not divisible by 22 ) then assign (set) a[l+r2]:=ia[l+r2]:=i (where ii is the number of the current action), otherwise (if r−l+1r−l+1 is even) assign (set) a[l+r−12]:=ia[l+r−12]:=i .
Consider the array aa of length 55 (initially a=[0,0,0,0,0]a=[0,0,0,0,0] ). Then it changes as follows:
- Firstly, we choose the segment [1;5][1;5] and assign a[3]:=1a[3]:=1 , so aa becomes [0,0,1,0,0][0,0,1,0,0] ;
- then we choose the segment [1;2][1;2] and assign a[1]:=2a[1]:=2 , so aa becomes [2,0,1,0,0][2,0,1,0,0] ;
- then we choose the segment [4;5][4;5] and assign a[4]:=3a[4]:=3 , so aa becomes [2,0,1,3,0][2,0,1,3,0] ;
- then we choose the segment [2;2][2;2] and assign a[2]:=4a[2]:=4 , so aa becomes [2,4,1,3,0][2,4,1,3,0] ;
- and at last we choose the segment [5;5][5;5] and assign a[5]:=5a[5]:=5 , so aa becomes [2,4,1,3,5][2,4,1,3,5] .
Your task is to find the array aa of length nn after performing all nn actions. Note that the answer exists and unique.
You have to answer tt independent test cases.
The first line of the input contains one integer tt (1≤t≤1041≤t≤104 ) — the number of test cases. Then tt test cases follow.
The only line of the test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105 ) — the length of aa .
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105 ).
For each test case, print the answer — the array aa of length nn after performing nn actions described in the problem statement. Note that the answer exists and unique.
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
自己太菜了==偶然出一道暴力STL的题就没想到,一直在想有什么巧妙的解法...看到别人题解里一句BFS就秒懂了...
优先队列的BFS。每次处理完一段区间后就把这段区间拆分,丢进优先队列里就好。注意priority_queue本身是一个大根二叉堆,所以重载运算符时符号要反一下(或者按照蓝书讲的 把len换成相反数)。
#include <bits/stdc++.h> using namespace std; struct node { int l; int r; int len; }; struct cmp { bool operator() (node a, node b) { if(a.len!=b.len)return a.len < b.len; else return a.l>b.l; } }; priority_queue<node,vector<node>,cmp>q; int a[200005]; int main() { int t; cin>>t; while(t--) { int n; cin>>n; node fst={1,n,n}; q.push(fst); int cnt=1; while(q.size()) { node pre=q.top(); q.pop(); if(pre.len==1) { a[pre.l]=cnt; cnt++; continue; } int mid; if(pre.len&1) mid=(pre.l+pre.r)/2; else mid=(pre.l+pre.r-1)/2; a[mid]=cnt; cnt++; if(mid-1>=pre.l) { q.push({pre.l,mid-1,mid-pre.l}); } if(mid+1<=pre.r) { q.push({mid+1,pre.r,pre.r-mid}); } } int i; for(i=1;i<=n;i++)cout<<a[i]<<' '; cout<<endl; } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!