【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 ...
随机推荐
- jQuery尺寸
jQuery 尺寸 jQuery width() 和 height() 方法 width() 方法设置或返回元素的宽度(不包括内边距.边框或外边距). height() 方法设置或返回元素的高度(不包 ...
- WPF学习笔记三之绑定
1.绑定模式 <TextBlock Margin="10" Text="LearningHard" Name="lbtext" Fon ...
- 在github网站上更新fork的repo
打开fork的repo. 点击Pull request, 这里会跳转到一个页面提示There isn’t anything to compare. 点击switching the base,将orig ...
- 随缘记录 LeetCode第168场周赛 2019-12-22
5292. 划分数组为连续数字的集合 给你一个整数数组 nums 和一个正整数 k,请你判断是否可以把这个数组划分成一些由 k 个连续数字组成的集合. 如果可以,请返回 True:否则,返回 Fals ...
- Linux基础命令小结(超全!!)
Linux目录结构 1.bin 存放经常使用的指令比如ll,cp 2.sbin 系统管理员使用的系统管理指令 3.home 存放普通用户的住目录 4.root 系统管理员的用户主目录 5.boot 存 ...
- 后端——框架——日志框架——logback——《官网》阅读笔记——第一章节
第一章节搭建了logback日志框架的环境,演示了Hello World的示例,并详细分析了示例. 搭建日志框架的过程非常简单,只需要在项目的classpath上添加以下三个jar包,logback- ...
- nodejs后台运行的方法
nohup node ***.js & 这种方法可以,但存在你无法查询日志等问题 在SSH里另一个有效的方法是screen命令. [转]http://www.9usb.net/201002/l ...
- 445. 两数相加 II
Q: A: 这种题的用例是一定会搞一些很大的数的.long都会溢出,所以我们就不用尝试转数字做加法转链表的方法了.另外直接倒置两个链表再做加法的做法会改变原链表,题干也说了禁止改动原链表. 1.求两个 ...
- 老生常谈--Java值传递和引用传递
起因 前两天面试被问到了这个问题,虽然之前老早就了解过这个问题,但是并没有深入了解,所以面试的时候一下子慌了,菜是原罪,今天菜鸡来补补基础知识. 其实这个问题一直是被讨论的,常见的三种说法就是,1,J ...
- jQuery 源码解析(三十) 动画模块 $.animate()详解
jQuery的动画模块提供了包括隐藏显示动画.渐显渐隐动画.滑入划出动画,同时还支持构造复杂自定义动画,动画模块用到了之前讲解过的很多其它很多模块,例如队列.事件等等, $.animate()的用法如 ...