Educational Codeforces Round 30 D. Merge Sort
题意:给你n和k,n代表有多少个数,k代表几次操作,求一个1到n的序列,要k次mergesort操作才能还原
3 3
2 1 3
4 1
1 2 3 4
5 6
-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的更多相关文章
- Educational Codeforces Round 30
Educational Codeforces Round 30 A. Chores 把最大的换掉 view code #pragma GCC optimize("O3") #pr ...
- Educational Codeforces Round 30 B【前缀和+思维/经典原题】
B. Balanced Substring time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Educational Codeforces Round 30 A[水题/数组排序]
A. Chores time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Educational Codeforces Round 42 D. Merge Equals (set + pll)
CF962D 题意: 给定一个数列,对于靠近左端的两个相同大小的值x可以合并成一个点.把x 乘以2 放在第二个点的位置,问最后的数列大小和每个位子的值. 思路: 利用set 配上 pair 就行了,感 ...
- Educational Codeforces Round 69 D E
Educational Codeforces Round 69 题解 题目编号 A B C D E F 完成情况 √ √ √ ★ ★ - D. Yet Another Subarray Problem ...
- Educational Codeforces Round 85 (Rated for Div. 2)
\(Educational\ Codeforces\ Round\ 85\ (Rated\ for\ Div.2)\) \(A. Level Statistics\) 每天都可能会有人玩游戏,同时一部 ...
- Educational Codeforces Round 117 (Rated for Div. 2)
Educational Codeforces Round 117 (Rated for Div. 2) A. Distance https://codeforces.com/contest/1612/ ...
- [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 ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
随机推荐
- Python3.4 枚举类型的使用
From: https://majing.io/posts/10000005131196 枚举类型是在Python3.4新增到Python的标准库. 创建枚举 Python提供了两种方法来创建枚举: ...
- Extjs相关知识
1.链接 1.1.零散知识链接 https://blog.csdn.net/zhaojianrun/article/details/70141071 https://www.cnblogs.com/p ...
- 将NULL值转化为“”
// 将NULL转化为“”,1是需要修改的实体类参数,3是转化后的实体对象 String 2= JSON.toJSONString(1, SerializerFeature.WriteNullStri ...
- 搭建vsftpd服务
配置文件 anonymous_enable=NOlocal_enable=YESwrite_enable=YESlocal_umask=022dirmessage_enable=YESxferlog_ ...
- nmcli配置ipv6
nmcli配置ipv6 作者:Eric 微信:loveoracle11g [root@racooler ~]# nmcli connection show eno16777736 | grep ipv ...
- sql查询统计
SELECT TOP 50 (select text from sys.dm_exec_sql_text(sql_handle)) as [SQL], CAST( ((qs.total_elapsed ...
- 自定义项目启动初始化信息的listener报错
自定义初始化组件代码如下: @Component public class InitComponent implements ServletContextListener, ApplicationCo ...
- JAVA之Mybatis基础入门--框架搭建与简单查询
JAVA中,操作数据库有JDBC.hibernate.Mybatis等技术,今天整理了下,来讲一讲下Mybatis.也为自己整理下文档: hibernate是一个完全的ORM框架,是完全面向对象的.但 ...
- How to setup Tensorflow inception-v3 model on Windows
There is Inception-v3 model python implementation on GitHub at: https://github.com/tensorflow/models ...
- c#字符串to/from文本文档IO示例
写入文本文档 class Program { static void Main(String[] args) { //写入string数组,每个string一行 string[] lines = { ...