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. lucene、solr中的日期衰减方法-------function query --尚未测试在solr4.8

    经常有一种情景是这样的:我们索引了N年的文章,而查询时候无论直接用相关度.或者用时间排序,都是比较鲁莽的:我们想要一种既要相关度比较高,又要时间上比较新的文章. 这时候的解决办法就是,自定义日期衰减的 ...

  2. Flow Layout

    --------------siwuxie095                             将根面板 contentPane 的布局切换为 Flow Layout     Flow La ...

  3. 面试题: java面试经历 已看1 抢红包如何分配每个人抢到的钱 有用 难点的面试题

    2018.03.09 深圳乐唯科技 我看了下感觉这公司貌似挺不错的,面试官人也挺好的,氛围应该很不错,可惜我实力不足,唉,接续努力,下面把面试中印象较深的三个问题写一下. 面试问题1:数据库删除重复数 ...

  4. c语言学习笔记 const变量

    在c语言的编程过程中经常会遇到有常数参加运算的运算,比如这种. int a=100*b; 这个100我们叫常数或者叫常量,但是程序中我们不推荐这种直接写常数的方法,有两个缺点. 第一是程序可读性差. ...

  5. 说说excel

    今天遇到一个实际问题. 我有一组数据: 0.0.0.1 activate.adobe.com 0.0.0.1 practivate.adobe.com 0.0.0.1 ereg.adobe.com 0 ...

  6. 第三章:PCL基础3.1

    架构师为了确保在PCL中所有代码风格的一致性,使得其他开发者及用户容易理解源码,PCL开发者制定并遵循着一套严格的编写规范,PCL的开发者都默认此规范. 3.1PCL推荐的命名规范 1.文件命名 1) ...

  7. svn冲突问题详解 SVN版本冲突解决详解

    svn冲突问题详解 SVN版本冲突解决详解 (摘自西西软件园,原文链接http://www.cr173.com/html/46224_1.html) 解决版本冲突的命令.在冲突解决之后,需要使用svn ...

  8. MVC中使用代码创建数据库(code first +mysql+EF)

    1.新建一个mvc项目 2.安装mysql需要的几个文件 EntityFramework.MySql.Data(6.9.12)和MySql.Data.Entity (6.9.12) 这里有几点要注意 ...

  9. 《Linux内核设计与实现》读书笔记(九)- 内核同步介绍

    存在共享资源(共享一个文件,一块内存等等)的时候,为了防止并发访问时共享资源的数据不一致,引入了同步机制. 主要内容: 同步的概念 同步的方法-加锁 死锁 锁的粒度 1. 同步的概念 了解同步之前,先 ...

  10. SQL的发展史

    在20世纪60年代,网状数据库系统(如CODASYL)和分层数据库系统(如IMS TM)是用于自动化银行业务.记帐和订单处理系统的一流技术,这些系统是由于商业大型计算机的引入才启用的.而SQL是在70 ...