Loading

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:

  1. Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one;
  2. Let this segment be [l;r][l;r] . If rl+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 rl+1r−l+1 is even) assign (set) a[l+r12]:=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:

  1. 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] ;
  2. 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] ;
  3. 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] ;
  4. 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] ;
  5. 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.

Input

The first line of the input contains one integer tt (1t1041≤t≤104 ) — the number of test cases. Then tt test cases follow.

The only line of the test case contains one integer nn (1n21051≤n≤2⋅105 ) — the length of aa .

It is guaranteed that the sum of nn over all test cases does not exceed 21052⋅105 (n2105∑n≤2⋅105 ).

Output

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.

Example
Input
Copy
6
1
2
3
4
5
6
Output
Copy
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;
}

 



posted @ 2020-05-15 14:35  脂环  阅读(228)  评论(0编辑  收藏  举报