Codeforces 873 D Merge Sort 【分治】
来源:互联网 发布:js舞蹈培训中心靠谱么 编辑:程序博客网 时间:2024/06/06 06:46
题目:
http://codeforces.com/contest/873/problem/D
题意:
子序列长度为n,归并排序,如果子序列不是有序的,那么就折半,再递归,直到子序列有序,总共递归k次。
给出n和k,求原来的子序列。
分析:
归并排序的变形,每次折半直到k为0。
代码:
#include <iostream>#include <algorithm>#include <string>#include <vector>#include <map>#include <set>#include <queue>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int N=2e5+9;typedef long long LL;typedef pair<int,int> pii;int n,k,a[N+5];void Solve(int l,int r){ if(!k||l==r-1) return; k-=2; int mid=(l+r)>>1; swap(a[mid-1],a[mid]); Solve(l,mid); Solve(mid,r);}int main(){ scanf("%d%d",&n,&k); k--; if(k&1) return 0*puts("-1"); for(int i=0;i<n;++i) a[i]=i+1; Solve(0,n); if(k) return 0*puts("-1"); for(int i=0;i<n;i++) printf("%d ",a[i]); return 0;}
阅读全文
0 0
- Codeforces 873D. Merge Sort 分治 + 构造
- Codeforces 873 D Merge Sort 【分治】
- codeforces D. Merge Sort 分治
- Codeforces 873D Merge Sort
- codeforces 873D merge sort 构造
- Codeforces 873 D Merge Sort(归并排序性质)
- Educational Codeforces Round 30 D. Merge Sort
- Educational Codeforces Round 30 D.Merge Sort
- 分治排序(MERGE-SORT)
- Codeforces 847B Preparing for Merge Sort
- 【二分优化】Preparing for Merge Sort CodeForces
- Java练习:分治法之合并排序(merge Sort)
- 算法导论2.3MERGE-SORT(分治或者合并排序算法)
- merge-sort (归并排序)——分治算法
- codeforces 161D 树的点分治
- codeforces 868D 分治哈希
- Codeforces 875D High Cry rmq+分治
- CodeForces 340D - Bubble Sort Graph
- Ubuntu16.04LTS64位安装64JDK9.0.1
- maven groupId,groupId,version
- MACbook系统从10.7升级到10.10
- mac上安装xgboost报错
- mysql优化
- Codeforces 873 D Merge Sort 【分治】
- 手把手教你用webpack来搭建一个项目
- Linux CentOS 7 安装字体库 & 中文字体
- C++线段树初步(下)
- Redis 在Centos7下配置开机自启动
- 补齐函数求数根
- SVN问题-Failed to load JavaHL Library
- PAT (Basic Level) Practise (中文)1003. 我要通过!(20)
- 上传下载