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, N integers 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 tempOri[maxn],origin[maxn],change[maxn];
int n; bool same(int A[],int B[]){
for(int i = ; i < n; i++){
if(A[i] != B[i]){
return false;
}
}
return true;
} void showArray(int A[]){
for(int i = ; i < n ; i++){
printf("%d",A[i]);
if(i < n - ) printf(" ");
}
printf("\n");
} bool insert(){
bool flag = false;
for(int i = ; i < n; i++){
if(i != && same(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 != && same(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(insert()){
printf("Insertion Sort\n");
showArray(tempOri);
}else{
printf("Merge Sort\n");
for(int i = ; i < n; i++){
tempOri[i] = origin[i];
}
Merge();
}
return ;
}

1089 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. 1089 Insert or Merge (25 分)

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

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

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

  4. 1089 Insert or Merge (25分)

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

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

  6. PAT 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, ...

  8. 1089. Insert or Merge (25)-判断插入排序还是归并排序

    判断插入排序很好判断,不是的话那就是归并排序了. 由于归并排序区间是2.4.8开始递增的,所以要判断给出的归并排序执行到哪一步,就要k从2开始枚举. 然后再对每个子区间进行一下sort即可. #inc ...

  9. PAT (Advanced Level) 1089. Insert or Merge (25)

    简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

  10. A1089 Insert or Merge (25 分)

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

随机推荐

  1. IntelliJ手记

    1. 配置JDK:File - Project Structure - SDKs,点击“+”即可: 2. 配置远程调试,对于azkaban的远程调试,在azkaban-solo-start.sh里面的 ...

  2. Erlang generic standard behaviours -- summary

    gen_server 相关的片段分析得也差不多了, 这篇作为一个简要的总结.这一系列相关的分析暂且告一段落(之后如有必要,还会回来的 ^^ ),下一个系列主要是以pool 相关, 包括但不仅限于开源项 ...

  3. C#某月的第一天和最后一天

    1.本月的第一天===>DateTime.Now.AddDays(1 - DateTime.Now.Day);//当前日期减去当前日期和本月一号相差天数 2.本月的最后一天===>Date ...

  4. 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历

    二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根 ...

  5. bean validator - Hibernate validator

    在后台开发过程中,对参数的校验成为开发环境不可缺少的一个环节.比如参数不能为null,email那么必须符合email的格式,如果手动进行if判断或者写正则表达式判断无意开发效率太慢,在时间.成本.质 ...

  6. kvm 基础 虚拟机改名

    转自:http://www.cnblogs.com/5201351/p/4464350.htm 1.查看所有的kvm虚拟机 [root@5201351_kvm ~]# virsh list --all ...

  7. ueditor1.4.3jsp版在上传图片报"未找到上传文件"解决方案

    这是因为struts2的过滤器,解决方法是自定义一个过滤器 新建一个过滤器的类,代码: package com.filter; import java.io.IOException; import j ...

  8. foregroundservice的用处和用法

    由于android的系统资源回收机制,当内存不足的时候,会自动关闭一些后台服务,如果这时候我们的服务正在播放歌曲,由于被关闭,歌曲会被中断,这样会造成很差的用户体验. 这时候我们可以通过在servic ...

  9. JDBC编程之数据查询

    ----------------siwuxie095                             JDBC 编程之数据查询             首先下载 MySQL 的 JDBC 驱动 ...

  10. Pig Latin JOIN (inner) 与JOIN (outer)的区别

    1.内连接(自然连接): 只有两个表相匹配的行才能在结果集中出现 2.外连接: 包括 (1)左外连接(左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右两表都不加限制) 3. ...