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

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

#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. 【puppeteer+Node.js安装环境】之步骤

    步骤一:首先,安装node.js环境,从官网下载最新的安装包. 步骤二:安装完成之后,再安装npm,通过命令行输入:npm install -g cnpm --registry=https://reg ...

  2. 64位win7环境eclipse集成svn后出现Failed to load JavaHL Library的解决办法

    http://lushuifa.iteye.com/blog/2038000

  3. CF:Problem 426B - Sereja and Mirroring 二分或者分治

    这题解法怎么说呢,由于我是把行数逐步除以2暴力得到的答案,所以有点二分的意思,可是昨天琦神说是有点像分治的意思.反正总的来说:就是从大逐步细化找到最优答案. 可是昨晚傻B了.靠! 多写了点东西,然后就 ...

  4. 【BZOJ】2186 沙拉公主的困惑

    一道很有价值的题. [解析1]欧几里德算法求乘法逆元,前缀和 [Analysis]O(T n log n). [Sum] ①int运算.假设会超出界,第一个数前要加上(LL)即类型转换. ②gcd不变 ...

  5. cocos2d-x 3.0rc1 使用iconv库 解决UTF8乱码问题

    多国语言要用到开源字符转换 iconv 先贴出自己的使用代码 你能够做成头文件 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include &qu ...

  6. iOS表格制作

    由于项目上的需求,需要做一个表格出来,来显示流程状态.刚开始脑子一头雾水,没有一点思路,但是靠着自己的座右铭--“世上无难事,只怕有心人”,克服了所有困难.好,不说了,讲正事. 制作表格,还是需要ta ...

  7. Intersection(计算几何)

    Intersection Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)To ...

  8. Dire Wolf(区间DP)

    Dire Wolf Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)Total ...

  9. python高级语法进阶

    python中几个比较难懂概念进阶. 迭代器 实现了迭代器协议的容器对象,基于如下两个方法: __next__:返回容器的下一个元素 __iter__:返回迭代器本身 由此可见,如果要自定义一个迭代器 ...

  10. SQLite支持的并发访问数

    SQLite objects created in a thread can only be used in that same thread.The object was created in th ...