CF873D.Merge Sort

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows:

  1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≤ i < r - 1 a[i] ≤ a[i + 1]), then end the function call;
  2. Let ;
  3. Call mergesort(a, l, mid);
  4. Call mergesort(a, mid, r);
  5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions.

The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n).

The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort — mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted.

Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k.

Help Ivan to find an array he wants!

归并排序(Merge sort)是一种著名的排序算法。用于对数组 aa 中下标范围为 [l, r)[l, r) 的元素进行排序的主函数可实现如下:

  1. 若区间 [l, r)[l, r) 已按非降序排好序(即对任意满足 l ≤ i < r − 1l ≤ i < r - 1 的 ii,均有 a[i] ≤ a[i + 1]a[i] ≤ a[i + 1]),则直接结束该函数调用;
  2. 令 ;
  3. 调用 mergesort(a, l, mid)\text{mergesort}(a, l, \text{mid});
  4. 调用 mergesort(a, mid, r)\text{mergesort}(a, \text{mid}, r);
  5. 将两个子区间 [l, mid)[l, \text{mid}) 和 [mid, r)[\text{mid}, r) 进行归并,使得整个区间 [l, r)[l, r) 按非降序排好序。归并过程不调用任何其他函数。

本题中数组采用 0 索引,因此要对整个数组排序,需调用 mergesort(a, 0, n)\text{mergesort}(a, 0, n)。

函数 mergesort\text{mergesort} 的调用次数极为关键,因此 Ivan 决定在排序过程中统计该调用次数。例如,若 a = {1, 2, 3, 4}a = \{1, 2, 3, 4\},则仅发生 1 次 mergesort\text{mergesort} 调用:即 mergesort(0, 4)\text{mergesort}(0, 4),该调用会检查数组已有序,随即返回;若 a = {2, 1, 3}a = \{2, 1, 3\},则调用次数为 3:首先调用 mergesort(0, 3)\text{mergesort}(0, 3),其计算得 mid = 1\text{mid} = 1,继而调用 mergesort(0, 1)\text{mergesort}(0, 1) 和 mergesort(1, 3)\text{mergesort}(1, 3);而这两个调用均不再递归(因为区间 (0, 1)(0, 1) 和 (1, 3)(1, 3) 均已有序)。

Ivan 已编写程序用于统计 mergesort\text{mergesort} 的调用次数,但他现在需要测试该程序。为此,他需要构造一个数组 aa,满足:aa 是一个长度为 nn 的排列(即 aa 包含 nn 个元素,且恰好包含从 11 到 nn 的每个整数各一次),并且对该数组执行归并排序时,mergesort\text{mergesort} 函数的总调用次数恰好为 kk。

请帮助 Ivan 找到他所需的数组!

输入格式

The first line contains two numbers n and k (1 ≤ n ≤ 100000, 1 ≤ k ≤ 200000) — the size of a desired permutation and the number of mergesort calls required to sort it.

第一行包含两个整数 nn 和 kk(1 ≤ n ≤ 1000001 \leq n \leq 100000,1 ≤ k ≤ 2000001 \leq k \leq 200000)——分别为所求排列的长度以及对其执行归并排序(mergesort)所需的递归调用次数。

输出格式

If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output  - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] — the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them.

如果不存在一个大小为 nn 的排列,使得在对其执行归并排序(mergesort)时恰好发生 kk 次 mergesort 调用,则输出 −1-1;否则,输出 nn 个整数 a[0], a[1], …, a[n−1]a[0],\ a[1],\ \dots,\ a[n-1] —— 即满足要求的一个排列。若存在多个解,输出任意一个即可。

输入输出样例

  • 输入#1

    3 3

    输出#1

    2 1 3
  • 输入#2

    4 1

    输出#2

    1 2 3 4
  • 输入#3

    5 6

    输出#3

    -1

输入解题思路,AI测评打分。不知道怎么写?

首页