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.

地址:https://codeforces.com/contest/1353/problem/D

思路:开一个优先队列关于pair<int,int>,第一个存数组长度,第二个存右端点的负数(方便排序,当长度相同时取最左边的)

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int a[maxn] = { 0 };
        int n;
        cin >> n;
        priority_queue<P, vector<P>, less<P>>qu;
        qu.push({n,-n});
        int step = 1;
        while (!qu.empty())
        {
            P te = qu.top();
            int lon = te.first, r = -te.second;
            int l = r - lon + 1;
            int mid = (l + r) / 2;
            int chang = lon / 2;
            qu.pop();
            if (lon <= 0) continue;
            if (lon % 2 == 0)
            {
                a[(l+r) / 2] = step++;
                qu.push({ lon / 2-1,-(l+chang-1-1) });
                qu.push({ lon / 2,-(r)});
            }
            else
            {
                a[(l + r) / 2] = step++;
                qu.push({ lon / 2, -(l+chang-1) });
                qu.push({ lon / 2,-(r) });
            }
        }
        for (int i = 1; i <= n; i++) cout << a[i] << " ";
        cout << endl;
    }
}

猜你喜欢

转载自www.cnblogs.com/yin101/p/12897266.html
今日推荐