题意:给你n和k,n代表有多少个数,k代表几次操作,求一个1到n的序列,要k次mergesort操作才能还原

Examples
Input
3 3
Output
2 1 3 
Input
4 1
Output
1 2 3 4 
Input
5 6
Output
-1

思路:看了很久才看懂题目的意思,根据题意对于一个[l,r)的区间,如果是乱的,你会先mergesort一下,如果已经是递增的,
    那么就不用继续往下操作了;如果不是递增的,那么就要分一下,对左边[l,mid)操作,右边[mid,r)操作,等到左边和
    右边的都排完,然后最后再排一下,这样的操作次数是2。所以对于每一个样例,k肯定是个奇数,因为你最开始要操作一
    次全区间,然后才开始递归,而且k不会大于等于2*n,因为全部分开,操作最多也就是2*n-1(自己画一下图就知道了)。
    我们就从末状态开始,往前走,每次dfs把a[mid]和a[mid-1]换一下,因为a[mid-1]是在左边区间的,a[mid]是在右
    边区间的,所以你需要左边排一下,右边排一下,这样操作次数是2,最后排一下,就是这个最后排一下,可以把我们交换
    的还原(我猜应该是这样233333)。细节就看代码吧。 代码:
#include<iostream>
#include<string.h>
using namespace std; const int maxn=1e5+5; int a[maxn],n,k; void dfs(int l,int r){
    if(k==1||r<=l+1)return ;
    k-=2;
    int mid=(l+r)/2;
    int x=a[mid];
    a[mid]=a[mid-1]; a[mid-1]=x;
    //因为左边的区间是[l,mid),右边的区间是[mid,r),所以应该要把a[mid-1]和a[mid]换一下
    //这样就需要两次mergesort才能还原,(一次左边,一次右边,最后总的一次还原)
    dfs(l,mid);
    dfs(mid,r);
}
int main(){
    cin>>n>>k;
    if(k>=2*n||k%2==0){
        cout<<-1<<endl;
        return 0;
    }
    for(int i=1;i<=n;i++)a[i]=i;
    dfs(1,n+1);
    for(int i=1;i<=n;i++){
        if(i!=1)cout<<' ';
        cout<<a[i];
    }
    cout<<endl;
    return 0;
}

Educational Codeforces Round 30 D. Merge Sort的更多相关文章

  1. Educational Codeforces Round 30

    Educational Codeforces Round 30  A. Chores 把最大的换掉 view code #pragma GCC optimize("O3") #pr ...

  2. Educational Codeforces Round 30 B【前缀和+思维/经典原题】

    B. Balanced Substring time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. Educational Codeforces Round 30 A[水题/数组排序]

    A. Chores time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  4. Educational Codeforces Round 42 D. Merge Equals (set + pll)

    CF962D 题意: 给定一个数列,对于靠近左端的两个相同大小的值x可以合并成一个点.把x 乘以2 放在第二个点的位置,问最后的数列大小和每个位子的值. 思路: 利用set 配上 pair 就行了,感 ...

  5. Educational Codeforces Round 69 D E

    Educational Codeforces Round 69 题解 题目编号 A B C D E F 完成情况 √ √ √ ★ ★ - D. Yet Another Subarray Problem ...

  6. Educational Codeforces Round 85 (Rated for Div. 2)

    \(Educational\ Codeforces\ Round\ 85\ (Rated\ for\ Div.2)\) \(A. Level Statistics\) 每天都可能会有人玩游戏,同时一部 ...

  7. Educational Codeforces Round 117 (Rated for Div. 2)

    Educational Codeforces Round 117 (Rated for Div. 2) A. Distance https://codeforces.com/contest/1612/ ...

  8. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  9. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

随机推荐

  1. Azkaban实战,Command类型单一job示例,任务中执行外部shell脚本,Command类型多job工作flow,HDFS操作任务,MapReduce任务,HIVE任务

    本文转载自:https://blog.csdn.net/tototuzuoquan/article/details/73251616 1.Azkaban实战 Azkaba内置的任务类型支持comman ...

  2. (一)CentOS6.3安装Hadoop2.6.5

    1.准备环境 下载CentOS: https://www.centos.org/download/ 下载JDK: https://www.oracle.com/technetwork/java/jav ...

  3. windows 下借助7zip实现命令行解压缩

    windows 下借助7zip实现命令行解压缩 64位电脑下载 https://www.7-zip.org/a/7z1805-x64.exe 安装 安装目录下所有文件如下: 在命令行下只需要用到 7z ...

  4. promise规范之部分总结

    1. promise构造函数中的reject和resolve是微任务, 即先执行resolve后的代码,再执行之前通过then注册的代码 2. 对于状态已变更的promise来说,比如promiseA ...

  5. sqlserver 中NOLOCK、HOLDLOCK、UPDLOCK、TABLOCK、TABLOCKX

    https://www.cnblogs.com/sthinker/p/5922967.html

  6. Listen and Write 18th Feb 2019

    Weighted blanket has becomes very popular in many homes. they claim it can provide better sleep and ...

  7. 基于vue和svg的树形UI

      vue-svg-tree 基于vue和svg的动态树形UI 截图 应用 npm install vue-svg-tree 示例 <template> <div> <v ...

  8. 使用JavaScript制作页面特效2

    1.Date对象的常用方法 setFullYear() setMonth() setDate() setHours() setMinutes() setSeconds() 定时函数 setTimeou ...

  9. python2操作MySQL

    #coding=utf-8   import MySQLdb   conn = MySQLdb.connect(host='localhost',user='root',passwd='123456' ...

  10. 用360清理了一下电脑后发现Eclipse软件无法打开

    今天用360安全卫士清理了一下电脑,然后双击Eclipse软件发现不能打开,弹出以下界面: 解决方法如下: 打开计算机-属性-高级系统设置,修改系统变量里变量名为JAVA_HOME.CLASSPATH ...