题意:给你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. homework 张一刚

    #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h& ...

  2. template or render function not defined vue 突然报错了,怎么解决

    报错图例如下:template or render function not defined vue 突然报错了,怎么解决什么错误呢,就是加载不出来,网上看了一通,是vue版本不对,是vue-comp ...

  3. 读取excel 文件到datatable

    上一篇文章介绍了将datatable 内容导出到excel 文件,这里介绍如何将一个excel 文件读取出来,并保持到datatable 中,实际这样的应用场景也是经常遇到的. 这里继续使用了Micr ...

  4. 解决Flask局域网内访问不了的问题

    在服务器上使用http://127.0.0.1:5000可以访问,但是在局域网内通过服务器IP地址访问不了,解决办法为:设置Flask为任何地址均可以访问,post设置为‘0.0.0.0’, if _ ...

  5. 20175236 JAVA MyCP(课下作业)

    具体描述: 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bin 用来把文本文件(内容 ...

  6. Linux内存解读

    1.free -m命令 [root@crawler ~]# free -m total used free shared buffers cached Mem: -/+ buffers/cache: ...

  7. python版本的简单贪吃蛇

    先看看效果,白色的条是蛇(简单勿怪,有研究的同学请告知做的美观点),做了一个笑脸是糖果,背景弄了一个图, 代码也是从其他人那边弄来的,改了一部分直接可以在window上直接运行 代码如下: #codi ...

  8. solr参数之facet

    facet就像sql语句中的group一样,是对某一个字段进行group并count,即能够按照Facet的字段进行分组并统计. 一.基本使用: 必须属性:facet=true&facet.f ...

  9. 对python的一些拙见

    对于python,总的来说有点机缘巧合的识得了它.当我录取专业是计算机的时候,身边的一些人向我介绍了这个解释型脚本语言吧.大一自学了一部分,刚好听的网课是嵩天老师的课,这学期迫不及待地拉着舍友选了这个 ...

  10. 【Java基础】While循环详解

    循环结构分两大类,一类是当型,一类是直到型. 当型: 当布尔值表达式条件为True时,反复执行某语句,当布尔表达式的值为False时才停止循环,例如:while 与 for循环. 直到型: 先执行某语 ...