codeforces Round #642 div.3 D. Constructing the Array

D. Constructing the Arraytime

limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou 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:

  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.InputThe 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).OutputFor 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
    6
    1
    2
    3
    4
    5
    6
    output
    1
    1 2
    2 1 3
    3 1 2 4
    2 4 1 3 5
    3 4 1 5 2 6
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
#define Fbo friend bool operator < (node a, node b)
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(a, b, c) for (int a = b; a <= c; a++)
#define RFOR(a, b, c) for (int a = b; a >= c; a--)
#define off ios::sync_with_stdio(0)
#define sc(a) scanf("%d",&a)
#define pr(a) printf("%lld\n",a);
#define SC(n,m) scanf("%lld%lld",&n,&m)
using namespace std;
typedef long long ll;
const int Maxn=1e6+9;
int a[Maxn];
int main() {
    int t;
    cin >> t;
    while (t--) {
        priority_queue<node>q;
        int n;
        cin >> n;
        if (n == 1)cout << 1 << endl;
        else if (n == 2)cout << 1 << " " << 2 << endl;
        else {
            int mid;
            node x;
            x.l = 1;
            x.r = n;
            q.push(x);
            FOR(i, 1, n) {
                node w;
                w = q.top();
                q.pop();
                if ((w.r - w.l + 1) % 2 != 0) {
                    mid = (w.l + w.r) / 2;
                    a[mid] = i;
                }
                else {
                    mid = (w.l + w.r - 1) / 2;
                    a[mid] = i;
                }
                node aa, bb;
                if (mid - 1 >= w.l) {
                    aa.l = w.l;
                    aa.r = mid - 1;
                    q.push(aa);
                }
                if (mid + 1 <= w.r) {
                    bb.l = mid + 1;
                    bb.r = w.r;
                    q.push(bb);
                }
            }
            FOR(i, 1, n) {
                if (i == 1)cout << a[i];
                else cout << " " << a[i];
            }
            cout << endl;
        }
    }
}

你可能感兴趣的:(笔记)