【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意:
输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列。输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操作所得到的序列。数据保证了不会有两种排序方式都可以得到的序列。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[];
int b[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=;i<=n;++i)
cin>>a[i];
for(int i=;i<=n;++i)
cin>>b[i];
int pos=;
for(int i=n;i;--i)
if(a[i]!=b[i]){
pos=i;
break;
}
int flag=;
int loc=;
for(int i=;i<n;++i)
if(b[i]>b[i+]){
loc=i;
break;
}
for(int i=loc+;i<=n;++i)
if(a[i]!=b[i]){
flag=;
}
if(flag){
cout<<"Insertion Sort\n";
sort(a+,a++loc+);
for(int i=;i<=n;++i){
cout<<a[i];
if(i<n)
cout<<" ";
}
}
else{
cout<<"Merge Sort\n";
int t=;
while(){
int flag=;
for(int i=;i<=n;++i)
if(a[i]!=b[i]){
flag=;
break;
}
t*=;
for(int i=;i<=n/t;++i)
sort(a+(i-)*t+,a+i*t+);
sort(a+n/t*t+,a+n+);
if(!flag)
break;
}
for(int i=;i<=n;++i){
cout<<a[i];
if(i<n)
cout<<" ";
}
}
return ;
}
【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)的更多相关文章
- 1089. Insert or Merge (25)-判断插入排序还是归并排序
判断插入排序很好判断,不是的话那就是归并排序了. 由于归并排序区间是2.4.8开始递增的,所以要判断给出的归并排序执行到哪一步,就要k从2开始枚举. 然后再对每个子区间进行一下sort即可. #inc ...
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- PAT甲级1089. Insert or Merge
PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...
- PAT Advanced 1089 Insert or Merge (25) [two pointers]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- 1089 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1089 Insert or Merge (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PTA 09-排序2 Insert or Merge (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 Insert or Merge (25分) According to ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
随机推荐
- Centos7在防火墙中添加访问端口
1. 查看jenkins启动状态命令:systemctl status Jenkins 保证jenkins启动,此处的状态为正在运行 2. 查看防火墙状态命令:systemct ...
- 给html元素添加自定义属性,并且用jquery进行筛选
例如有多个div,想要实现类似radio的效果. <div name="teacher" selected="selected">tch1</ ...
- js解释器
又名js引擎 JavaScript是解释型语言,这就是说它无需编译,直接由JavaScript引擎直接执行. 既然说到了解释型语言,那么我们就来分别以下解释型语言和编译型语言的差别: 编译型语言:程序 ...
- javaFx中Image的路径问题
网络图像文件前面加“http://”,而本地文件则要加“file:”.将源代码改为: Image image = new Image("file:image/qq.jpg"); I ...
- webpack4.41.0配置三(插件minCssExtract/ DefinePlugin/Html)
(注:如无特殊说明这里的配置文件都指的是webpack.config.js) minCssExtract 我们通常期望js和js文件一起,css和css文件一起.该插件将CSS提取到单独的文件中.它为 ...
- IDEA格式化代码快捷键失灵原因
网易云音乐快捷键与IDEA快捷键冲突了!!!!!!!坑爹
- 【转载】Java多线程
转自:http://www.jianshu.com/p/40d4c7aebd66 引 如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目 ...
- STL顺序容器用法自我总结
顺序容器类型:vector.deque.list.forward_list.string.array. queue(单向队列)底层也是用deque(双端队列)实现的 a.swap(b); swap(a ...
- go语言 二叉树
package main import ( "fmt" "reflect" ) type BinaryNode struct { Data interface{ ...
- ColorPix——到目前为止最好用的屏幕取色器
分享一个颜色取色器网页.PPT.EXCEL配色不再烦恼 简单易用 大家做商业.企业报告的时候是不是经常遇到要调色的困扰呢?PPT.EXCEL等颜色选取会对报告有质的影响!!要更专业要更有美感!给大家分 ...