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

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

#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. map端join

    package my.hadoop.hdfs.mapreduceJoin; import java.io.BufferedReader; import java.io.FileInputStream; ...

  2. 小白学习HTTPS

    如果你和我一样是HTTPS的小白的话,那就一起来学习这个吧.争取把这篇博客写好,写全面,从原理到实践再到部署. 让我们先来模拟一个场景:当你嗨皮地敲着代码,你的老板偷偷摸摸跑到你的身边,"小 ...

  3. 【ubantu】在Ubuntu上安装tar.gz,tar.bz以及deb文件(例:libreoffice安装)

    参考文章: https://blog.csdn.net/zhuquan945/article/details/52986712 ==================================== ...

  4. 设计模式之前之UML

    UML,让系统可视化,让规格和设计文档化的表现方法.下面来简单介绍一下这个UML.

  5. shell 遍历所有文件包括子目录

    1.代码简单,但是难在校验,不像python那么好理解 建议在Notepad++下编辑. 2.注意引用linux命令的`是[tab]键上面那个 3.if[] 这里 Error :  syntax er ...

  6. Lumen开发:如何向 IoC 容器中添加自己定义的类

    版权声明:本文为博主原创文章,未经博主允许不得转载. 先在起始文件bootstrap/app.php加上$app->register(App\Providers\User\UserService ...

  7. html5+css3实现手机下拉和下拉刷新

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. j2EE的web.xml详解

    https://blog.csdn.net/changqing5818/article/details/49928231 https://www.cnblogs.com/ClassNotFoundEx ...

  9. Problem_A

    Problem_A Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Descripti ...

  10. Redis通过PUBLISH / SUBSCRIBE 等命令实现了订阅与发布模式

    # 切换目录 [root@localhost /]# cd /opt/redis-4.0.10 # 启动客户端 -p 指定端口 [root@localhost ~]# redis-cli -p 638 ...