题意:给你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. Ubuntu16.04下安装elasticsearch+kibana实现php客户端的中文分词

    1.下载安装java, elasticsearch和kibana apt-get install default-jre default-jdk wget https://artifacts.elas ...

  2. 踩坑rosbag --clock

    将rosbag的数据feed给lego-loam,输出地图.另外写了一个滤波节点,订阅地图,进行滤波操作,再发布出来. 由于输入给lego-loam的数据来自于rosbag,所以需要rosbag提供时 ...

  3. 使用velodyne16线激光雷达跑loam-velodyne

    一.velodyne-VLP16使用教程 推荐网址: http://blog.csdn.net/littlethunder/article/details/51920681 https://www.c ...

  4. python3-基础8

    模块与包 什么是模块 模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. #在python中,模块的使用方式都是一样的,但其实细说的话,模块可以分为四个通用类别: 1 ...

  5. thymeleaf的fragment例子

    fragment介绍 fragment类似于JSP的tag,在html中文件中,可以将多个地方出现的元素块用fragment包起来使用. 定义fragment 新建foot.html文件 <!D ...

  6. catkin-make: command not found 错误解决

    参考网址:https://answers.ros.org/question/212492/catkin_make-command-not-found/ zc@ubuntu:~ $ source /op ...

  7. java核心-多线程-Java多线程编程涉及到包、类

    Java有关多线程编程设计的类主要涉及两个包java.lang和java.util.concurrent两个包 java.lang包,主要是线程基础类 <1>Thread <2> ...

  8. [STM32F103]串口UART配置

    l 串口时钟使能,GPIO时钟使能: RCC_APB2PeriphClockCmd(); l 串口复位: USART_DeInit(); 这一步不是必须的 l GPIO端口模式设置: GPIO_Ini ...

  9. python对mysql进行简单操作

    python 连接MySQL数据库,进行简单操作 一.连接MySQL数据库,关闭连接 import pymysql db = pymysql.connect(host="xxx.xxx.x. ...

  10. html 常用button事件

    <input onclick="document.all.WebBrowser.ExecWB(1,1)" type="button" value=&quo ...