zoukankan      html  css  js  c++  java
  • 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 
    
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 998244353;
    const int N = 2e5+5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m%n);
    }
    ll lcm(ll m, ll n)
    {
        return m*n / gcd(m, n);
    }
    
    struct cmp
    {
        bool operator() (const PII &a, const PII &b)
        {
            int lena = a.second - a.first + 1;
            int lenb = b.second - b.first + 1;
            if (lena == lenb)
                return a.first < b.first;
            return lena > lenb;
        }
    };
    
    int main()
    {
        int T;
        cin >> T;
        while (T--)
        {
            set<PII,cmp> s;
            int n;
            cin >> n;
            s.insert({ 1, n });
            vector<int> a(n + 1);
            for (int i = 1; i <= n; i++)
            {
                PII cur = *s.begin();
                s.erase(s.begin());
                int mid = (cur.first + cur.second) / 2;
                a[mid] = i;
                if (mid - 1 >= cur.first)
                    s.insert({ cur.first,mid-1 });
                if (mid + 1 <= cur.second)
                    s.insert({ mid+1,cur.second });
            }
            rep(i, 1, n)
            {
                cout << a[i] << " ";
            }
            cout << endl;
        }
        return 0;
    }
  • 相关阅读:
    Redis之数据持久化RDB与AOF
    linux命令
    路由选择协议
    三次握手+滑动窗口
    JSP的文件上传
    JSP的会话(Session)跟踪
    JSP的Cookie处理
    JSP的过滤器
    JSP的表单处理
    JSP中HTTP状态码
  • 原文地址:https://www.cnblogs.com/dealer/p/12892895.html
Copyright © 2011-2022 走看看