According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤). Then in the next line, Nintegers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in the first line either "Insertion Sort" or "Merge Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input 2:

10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6

Sample Output 2:

Merge Sort
1 2 3 8 4 5 7 9 0 6
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int Origin[maxn],tempOri[maxn],change[maxn];
int n; void showArray(int A[]){
for(int i = ; i < n; i++){
printf("%d",A[i]);
if(i < n - ) printf(" ");
}
} bool isSame(int A[],int B[]){
for(int i = ; i < n; i++){
if(A[i] != B[i]) return false;
}
return true;
} bool Insertion(){
bool flag = false;
for(int i = ; i < n; i++){
if(i != && isSame(tempOri,change)){
flag = true;
}
int temp = tempOri[i], j = i;
while(j > && tempOri[j - ] > temp){
tempOri[j] = tempOri[j - ];
j--;
}
tempOri[j] = temp;
if(flag == true) return true;
}
return false;
} void Merge(){
bool flag = false;
for(int step = ; step/ <= n; step *= ){
if(step != && isSame(tempOri,change)){
flag = true;
}
for(int i = ; i < n; i += step){
sort(tempOri + i,tempOri + min(i+step,n));
}
if(flag == true){
showArray(tempOri);
return;
}
}
} int main(){
scanf("%d",&n);
for(int i = ; i < n; i++){
scanf("%d",&Origin[i]);
tempOri[i] = Origin[i];
}
for(int i = ; i < n; i++){
scanf("%d",&change[i]);
}
if(Insertion()){
printf("Insertion Sort\n");
showArray(tempOri);
}else{
printf("Merge Sort\n");
for(int i = ; i < n; i++){
tempOri[i] = Origin[i];
}
Merge();
}
return ;
}

09-排序2 Insert or Merge (25 分)的更多相关文章

  1. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  2. 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 ...

  3. A1089 Insert or Merge (25 分)

    一.技术总结 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...

  4. 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)

    题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...

  5. 09-排序2 Insert or Merge (25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  6. 1089 Insert or Merge (25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  7. 1089 Insert or Merge (25分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  8. 7-19(排序) 寻找大富翁 (25 分)(归并排序)(C语言实现)

    7-19(排序) 寻找大富翁 (25 分) 胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假设给出N个人的个人资产值,请快速找出资产排前M位的大富翁. 输入格式 ...

  9. pat1089. Insert or Merge (25)

    1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...

  10. PAT 1089. Insert or Merge (25)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

随机推荐

  1. html乱码原因与网页乱码解决方法

    造成html网页乱码原因主要是html源代码内中文字内容与html编码不同造成.但无论是哪种情况造成乱码在网页开始时候都需要设置网页编码. charset编码设置 html网页乱码效果截图 一.乱码造 ...

  2. Android中SQLite查询date类型字段出现有返回但是为错误值的情况

    出现该情况的原因是因为查询精度与数据库中存储精度不相同造成的,例如,查询精度为 YYYY-MM-DD 但是存储精度为 YYYY-MM-DD HH:MM:SS,就会出现该错误. 更改查询精度为YYYY- ...

  3. [luogu3385]dfs_spfa判负环模板

    解题关键:模板保存. 判负环不需要memset dis数组,因为已经更新过得d数组一定小于0,如果当前点可以更新d,说明d更小,有可能继续扩大负环,所以继续更新:如果比d[v]大,则不可能继续更新负环 ...

  4. mock SpringMVC 测试控制器方法

    从Spring3.2开始 Spring包含了一种mockSpringMVC并针对controller执行http请求的机制 如(该代码选自spring实战4): public void shouldS ...

  5. C++——override

    override关键字作用: 如果派生类在虚函数声明时使用了override描述符,那么该函数必须重载其基类中的同名函数,否则代码将无法通过编译.举例子说明 struct Base { virtual ...

  6. v8垃圾回收和js垃圾回收机制

    垃圾回收器是一把十足的双刃剑.好处是简化程序的内存管理,内存管理无需程序员来操作,由此也减少了长时间运转的程序的内存泄漏.然而无法预期的停顿,影响了交互体验.本文从 V8 (node.js runti ...

  7. .NET回归 HTML----表单元素(1)和一些常用的标记

    表单就是-----用于搜集不同类型的用户输入. 表单元素指的是不同类型的 input 元素.复选框.单选按钮.提交按钮等等. 首先将表单元素分为三个类型.文本类,按钮类,选择类. 表单可以嵌套在表中, ...

  8. php学习笔记-for循环

    for(init;condition;statement) { func(); } for循环的执行逻辑是先执行一次init语句,然后判断condition是否为true,是则执行func(),再执行 ...

  9. 初识 Redis

    浏览目录 什么是redis redis的特点 redis的安装和基本使用 操作模式 连接池 操作 string操作 hash操作 list操作 什么是Redis? redis是一个key-value存 ...

  10. 第四章输入/输出(I/O)4.2PCL中I/O模块及类介绍

    PCL中I/O库提供了点云文件输入输出相关的操作类,并封装了OpenNI兼容的设备源数据获取接口,可直接从众多感知设备获取点云图像等数据.I/O模块利用21个类和28个函数实现了对点云的获取.读入.存 ...