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:

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;

Let ;

Call mergesort(a, l, mid);

Call mergesort(a, mid, r);

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 ≤ 100000, 1 ≤ 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 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.

Examples

inputCopy

3 3

outputCopy

2 1 3

inputCopy

4 1

outputCopy

1 2 3 4

inputCopy

5 6

outputCopy

-1

题目要你构造长度为N,调用归并排序次数为K 的序列,如果已经排好序,他将不在调用归并排序

做法,首先,这个调用除了第一次,之后肯定都是调用两次,所以如果是偶数的话肯定是不可以的,所以首先排除次数是偶数的

之后,调用的时候,分成两边,然后K在传递的时候,如果K/2是奇数,则K1=K2=K/2,否则K1=K2-2;

因为我们的下两次调用也要保证两边为奇数次,

最后,如果当前调用区间只有一个数字,就可以返回了,如果区间有两个数字,并且调用时K为2,则交换这两个数字

如果K为2,则可以直接交换mid-1和mid,每次定义一个tot+1或者+2最后看是否等于K,是则输出,不是则-1

#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<stdio.h>
#include<float.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define sf scanf
#define pf printf
#define fi first
#define se second
#define mp make_pair
#define pii pair<int,int>
#define scf(x) scanf("%d",&x)
#define prf(x) printf("%d\n",x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define mm(x,b) memset((x),(b),sizeof(x))
#define scfff(x,y,z) scanf("%d%d%d",&x,&y,&z)
typedef long long ll;
const ll mod=1e9+7;
using namespace std; const double eps=1e-8;
const int inf=0x3f3f3f3f;
const double pi=acos(-1.0);
const int N=2e5+10;
int a[N];
int tot=0;
void dfs(int l,int r,int q)
{
tot++;
int mid=(l+r)>>1;
if(!q) return ;
if(q==2&&mid>0)
{
q=0;tot+=2;
swap(a[mid-1],a[mid]);
return ;
}
if(l+2==r){
tot+=2;
swap(a[l],a[l+1]); return;
}
if(mid==l) return;
int k1,k2; k1=k2=q/2;
if((q/2)%2==0)
k1--,k2++;
if(q) dfs(l,mid,k1-1);
if(q) dfs(mid,r,k2-1);
}
int main()
{
int n,k;cin>>n>>k;
rep(i,0,n) a[i]=i+1;
if(!(k&1)) { cout<<"-1"; return 0; } dfs(0,n,k-1);
if(tot==k)
{
cout<<a[0];
rep(i,1,n) cout<<" "<<a[i];
}else
cout<<"-1";
return 0;
}

873D. Merge Sort的更多相关文章

  1. CodeForces 873D Merge Sort 构造 分治

    题意 给出一个归并排序的算法\(mergesort\),如果对于当前区间\([l, r)\)是有序的,则函数直接返回. 否则会分别调用\(mergesort(l, mid)\)和\(mergesort ...

  2. [算法]——归并排序(Merge Sort)

    归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...

  3. SQL Tuning 基础概述06 - 表的关联方式:Nested Loops Join,Merge Sort Join & Hash Join

    nested loops join(嵌套循环)   驱动表返回几条结果集,被驱动表访问多少次,有驱动顺序,无须排序,无任何限制. 驱动表限制条件有索引,被驱动表连接条件有索引. hints:use_n ...

  4. 归并排序(Merge Sort)

    归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...

  5. 归并排序(merge sort)

    M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...

  6. Summary: Merge Sort of Array && 求逆序对

    常用算法(后面有inplace版本): package ArrayMergeSort; import java.util.Arrays; public class Solution { public ...

  7. 基础排序算法之并归排序(Merge Sort)

    并归排序是学习分治法 (Merge Sort) 的好例子.而且它相对于选择,插入,冒泡排序来说,算法性能有一定提升.我首先会描述要解决的问题,并给出一个并归排序的例子.之后是算法的思路以及给出伪代码. ...

  8. Divide and Conquer.(Merge Sort) by sixleaves

    algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...

  9. STL 源代码剖析 算法 stl_algo.h -- merge sort

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...

随机推荐

  1. PyCharm的使用教程

    1.1 安装 首先去下载最新的pycharm ,进行安装.可以直接在官网下载. 1.2 首次使用 1,点击Create New Project. 2, 输入项目名.路径.选择python解释器.如果没 ...

  2. 2018-2019-20175315 实验一 《Java开发环境的熟悉》实验报告

    2018-2019-20175315实验一 <Java开发环境的熟悉>实验报告 一.实验内容及步骤 实验1 1.用mkdir建立“20175303exp1”的目录 2.在“20175303 ...

  3. 用户态与内核态 & 文件流与文件描述符 简介【转】

    转自:https://www.cnblogs.com/Jimmy1988/p/7479856.html 用户态和内核态 程序代码的依赖和调用关系如下图所示: Lib:标准ASCI C函数,几乎所有的平 ...

  4. python使用pip 18以上版本离线安装package

    在内网办公环境,常常需要使用离线安装python的软件包. 一般都会先在互联网的电脑上下载,再拷贝到内网办公机器上进行离线安装. 一般来说,我是这样做的: 1.拷贝和外网电脑上版本一致,且32位或64 ...

  5. 本地项目文件夹上传至个人Github

    安装Git 之后到Git官网,点击Download下载,打开安装包一路按Next一切默认直至安装结束. 找到任意一个文件夹,点击鼠标右键后若出现下图的 Git Gui Here 和 Git Bash ...

  6. XPath Helper使用教程

    下载地址 百度网盘下载密码:yuuv下载完成后选择保留 安装 在导航栏中输入chrome://extensions:将.crx文件拖拽到扩展程序页面中,勾选已启用按钮. 使用 重启浏览器,快捷键CTR ...

  7. html中去除ul,li标签的样式列表标签的点?

  8. 差分线Layout的两个误区

    误区一:认为差分线可以相互之间耦合,所以可以相互之间提供回流路径,不需要地作为回流路径: 其实在信号回流分析上,差分走线和普通的单端走线的机理是一致的,即高频信号总是沿着电感最小的回路进行回流.最大的 ...

  9. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165323

    1.Kali下载路径 由于之前的课程中已经安装过VMware虚拟机,这一个步骤就省去 2.安装 (1)创建新的虚拟机->典型->添加镜像路径 (2)选择操作系统linux (3)进行虚拟机 ...

  10. How Classes are Found

    转载自: https://docs.oracle.com/javase/7/docs/technotes/tools/findingclasses.html How Classes are Found ...