归并排序的逆操作,每次二分时把第二段第一位与第一段最后一位开始往前第一个比它大的数交换位置

可以用归并排序验证答案对不对

#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; int a[N],b[N],k,res;
void rmergesort(int l,int r)
{
if(l==r||k<=)return ;
k-=;
int m=(l+r)>>;
if((r-l)%==)m--;
// cout<<l<<" "<<m<<" "<<r<<endl;
int ans=a[m+],id=m+;
for(int i=m;i>=l;i--)
{
if(ans>a[i])
{
ans=a[i];
id=i;
break;
}
}
swap(a[id],a[m+]);
rmergesort(l,m);
rmergesort(m+,r);
}
void mergesort(int l,int r)
{
res++;
bool f=;
for(int i=l+;i<=r;i++)
{
if(a[i]<a[i-])
{
f=;
break;
}
}
if(!f)return ;
int m=(l+r)>>;
if((r-l)%==)m--;
mergesort(l,m);
mergesort(m+,r);
int p1=l,p2=m+,i=l;
while(i<=r)
{
if(p1>m)b[i]=a[p2++];
else if(p2>r)b[i]=a[p1++];
else
{
if(a[p1]>a[p2])b[i]=a[p2++];
else b[i]=a[p1++];
}
i++;
}
for(int i=l;i<=r;i++)
a[i]=b[i];
}
int main()
{
int n;
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)a[i]=i;
k--;
rmergesort(,n);
if(k!=)puts("-1");
else
{
for(int i=; i<=n; i++)
printf("%d ",a[i]);
puts("");
}
/* res=0;
mergesort(1,n);
for(int i=1;i<=n;i++)
printf("%d ",a[i]);
puts("");
cout<<k<<" "<<res<<endl;*/
return ;
}
/******************** ********************/

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

  1. Educational Codeforces Round 42D. Merge Equals(STL)

    D. Merge Equals time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  2. Educational Codeforces Round 69 D E

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

  3. Educational Codeforces Round 30

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

  4. [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 ...

  5. Educational Codeforces Round 37

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

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

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  7. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  8. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. IIS 实现一个主机部署多个网站 共享80端口

    如果一个主机只是建立一个80端口的网站就有点浪费了,通过本文你就可以实现,在一个主机上建立多个80端口的站点,并通过不同的域名进行访问. 打开iis软件:控制面板-->管理工具-->Int ...

  2. centos7.2 开发 部署 .net core

    1.centos7.2 安装 nginx官方文档:http://nginx.org/en/linux_packages.html#mainline 创建nginx.repo 文件 Pre-Built ...

  3. tcp 状态转移图详解

    首先看一张图片: 虚线表示服务端的状态转移,实现表示客户端的状态转移. 初始的close状态并不是真是的状态,只是为了方便描述开始和终止状态而构造出来的. 从服务端的状态转移开始说: 服务端打开后处于 ...

  4. .Net中多线程类的使用和总结

    lock, Monitor, Thread, Join, BackGroundWorker.   消费者和生产者.Async 委托Invoke TypeHandle中BlockIndex. http: ...

  5. Python爬上不得姐 并将段子写入数据库

    #Python2.7 可以优化一下 前10页 每页点赞最多的段子 百思不得姐 # -*- coding: utf-8 -*-import MySQLdbimport urllib,urllib2imp ...

  6. Linq系列(7)——表达式树之ExpressionVisitor

    大家好,由于今天项目升级,大家都在获最新代码,所以我又有时间在这里写点东西,跟大家分享. 在上一篇的文章中我介绍了一个dll,使大家在debug的时候可以可视化的看到ExpressionTree的Bo ...

  7. local variable 'xxx' referenced before assignment(犯过同样的错)

    这个问题很囧,在外面定义了一个变量 xxx ,然后在Python的一个函数里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before assi ...

  8. 使用3DES+Base64来加密传输iOS应用数据

    本文转载至 http://www.erblah.com/post/objective-c/shi-yong-3des-base64lai-jia-mi-chuan-shu-iosying-yong-s ...

  9. maven;spring;pom

    [说明]因为对环境配置文件理解的不充分,遇到问题经常是无法独自解决,特别是maven和javaweb的转换,也是糊里糊涂的,今天就又出问题了. [说明] 一:今日完成 1)任务二的效果展示看的我一脸懵 ...

  10. 【BZOJ4128】Matrix BSGS+hash

    [BZOJ4128]Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...