CodeForces 1353 D. Constructing the Array 堆排序

1. 题目描述

1.1. Limit

Time Limit: 1 second

Memory Limit: 256 megabytes

1.2. Problem Description

You are given an array a a a of length n n n consisting of zeros. You perform n n n actions with this array: during the i i i-th action, the following sequence of operations appears:

Consider the array a a a of length 5 5 5 (initially a = [ 0 , 0 , 0 , 0 , 0 ] 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] [1;5] and assign a [ 3 ] : = 1 a[3] := 1 a[3]:=1, so a a a becomes [ 0 , 0 , 1 , 0 , 0 ] [0, 0, 1, 0, 0] [0,0,1,0,0];
  2. then we choose the segment [ 1 ; 2 ] [1; 2] [1;2] and assign a [ 1 ] : = 2 a[1] :=2 a[1]:=2, so a a a becomes [ 2 , 0 , 1 , 0 , 0 ] [2, 0, 1, 0, 0] [2,0,1,0,0];
  3. then we choose the segment [ 4 ; 5 ] [4; 5] [4;5] and assign a [ 4 ] : = a[4] := a[4]:=, so a a a becomes [ 2 , 0 , 1 , 3 , 0 ] [2, 0, 1, 3, 0] [2,0,1,3,0];
  4. then we choose the segment [ 2 ; 2 ] [2; 2] [2;2] and assign a [ 2 ] : = 4 a[2] :=4 a[2]:=4, so a a a becomes [ 2 , 4 , 1 , 3 , 0 ] [2, 4, 1, 3, 0] [2,4,1,3,0];
  5. and at last we choose the segment [ 5 ; 5 ] [5; 5] [5;5] and assign a [ 5 ] : = 5 a[5] := 5 a[5]:=5, so a a a becomes [ 2 , 4 , 1 , 3 , 5 ] [2, 4, 1, 3, 5] [2,4,1,3,5].

Your task is to find the array a a a of length n n n after performing all n n n actions. Note that the answer exists and unique.

You have to answer t t t independent test cases.

1.3. Input

The first line of the input contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. Then t t t test cases follow.

The only line of the test case contains one integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105) — the length of a a a.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105 ( ∑ n ≤ 2 ⋅ 1 0 5 \sum n \le 2 \cdot 10^5 n2105).

1.4. Onput

For each test case, print the answer — the array a a a of length n n n after performing n n n actions described in the problem statement. Note that the answer exists and unique.

1.5. Sample Input

6
1
2
3
4
5
6

1.6. Sample Onput

1
1 2
2 1 3
3 1 2 4
2 4 1 3 5
3 4 1 5 2 6

1.7. Source

CodeForces 1353 D. Constructing the Array


2. 解读

用优先队列 priority_queue 根据区间长度构造大顶堆。

每次取出堆顶的区间元素进行赋值,每次赋值完以后,将左子区间和右子区间放入堆中。和分治法的思想比较类似。

重复以上步骤直到堆为空。

3. 代码

#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>

const long long num = 2 * 1e5 + 1;
using namespace std;
// 定义数据类型
#define llpair pair<long long, long long>
// 存储
long long list[num];
// 标记当前数字个数
long long markNum;
// 队列长度
long long n;
// 定义排序方法
struct cmp {
    bool operator()(llpair a, llpair b)
    {
        if ((a.second - a.first) != (b.second - b.first)) {
            // 返回小于判断时,是大顶堆,与queue相反
            return (a.second - a.first) < (b.second - b.first);
        } else {
            // 返回大于号时,较小的元素在前
            return a.first > b.first;
        }
    }
};
// 使用优先队列构造大顶堆
priority_queue<llpair, vector<llpair>, cmp> qu;
// 定义清零函数
void clear(priority_queue<llpair, vector<llpair>, cmp>& q)
{
    priority_queue<llpair, vector<llpair>, cmp> empty;
    swap(empty, q);
}

// 分治法
void divideAndConquer(long long array[])
{
    while (!qu.empty()) {
        // 获取最大区间
        llpair pairBuffer = qu.top();
        // 出队
        qu.pop();
        // 获取队首元素
        long long low = pairBuffer.first;
        long long high = pairBuffer.second;
        // 计算中值
        long long mid = low + (high - low) / 2;
        if (low <= high && mid > 0 && array[mid] == 0) {
            array[mid] = markNum;
            markNum++;
        }
        // 入队
        if (low <= mid - 1)
            qu.push(make_pair(low, mid - 1));
        if (mid + 1 <= high)
            qu.push(make_pair(mid + 1, high));
    }
}

int main()
{
    // test case
    long long t;
    // long long n;
    // test case
    scanf("%lld", &t);
    // for each test case
    while (t--) {
        // 初始化
        markNum = 1;
        memset(list, 0, sizeof(list));
        clear(qu);
        // 输入
        scanf("%lld", &n);
        // 计算
        qu.push(make_pair(1, n));
        divideAndConquer(list);
        // 输出
        for (int i = 1; i <= n; i++) {
            printf("%lld ", list[i]);
        }
        printf("\n");
    }
}



联系邮箱:curren_wong@163.com

Github:https://github.com/CurrenWong

欢迎转载/Star/Fork,有问题欢迎通过邮箱交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值