codeforces D. Merge Sort 分治

来源:互联网 发布:java 1.7.0 64 bit 编辑:程序博客网 时间:2024/06/02 02:01

D. Merge Sort
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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!

Input

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

Output

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 ninteger 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.

Examples
input
3 3
output
2 1 3 
input
4 1
output
1 2 3 4 
input
5 6
output
-1

题解:

构造

初始化一个数组a[0]=1,a[1]=2,a[2]=3...a[n-1]=n;

函数dfs(l,r,x)表示的是把区间[l,r)变成需要x次call才能排好序的一个功能。

其中call的次数是一棵满二叉树的形式,所以call的次数x一定是奇数。

因此,如果x是偶数的话一点是不可能的。

这样的话dfs(l,r,x)里面应该是

x--;(表示该call)

然后x剩下的次数应由左右两部分完成。

dfs(l,mid,t1);dfs(mid,r,t2);(其中t1+t2=x)

然后把a[l,mid)放在右面a[mid,r)放在左边

注意!这里mid = (l+r+1)/2;为什么呢,因为在调用归并排序的时候左边略小一点,所以我们在构造的时候左边要略大一点。

如果x/2刚好是奇数,那么t1 =t2 =x/2;此时t1,t2都为奇数

因为dfs(,,x)中x只能接收为奇数的情况。

否则的话t1要比t2大2.(因为如果左右区间不能平分的话,左边要比右边略大一点)

代码:

#include <bits/stdc++.h>using namespace std;int n,k;const int maxn = 100011;int a[maxn];int tmp[maxn];bool dfs(int l,int r,int x){if(r - l <= 1) return x == 1;x--;if(x == 0) return true;int mid = (l+r+1)/2;if(x%2) return false;int t1,t2;if((x/2)%2 == 0){t1 = x/2+1;t2 = x/2-1;}else t1 = t2 = x/2;bool s = dfs(l,mid,t1) && dfs(mid,r,t2);if(!s) return s;int p = l;for(int i = mid;i < r;++i) tmp[p++] = a[i];for(int i = l;i < mid;++i) tmp[p++] = a[i];for(int i = l;i < r;++i) a[i] = tmp[i];return s;}int main(){cin>>n>>k;for(int i = 0;i < n;i++) a[i] = i+1;bool s = dfs(0,n,k);if(!s) cout<<-1;else{for(int i = 0;i < n;++i){printf("%d ",a[i]);}}return 0;}