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. 浅谈MVC、MVP、MVVM模式

    mvc的模式已经深入人心,想必大家都很熟悉,但是未必都能遵守mvc模式.我们的一个mvc项目比较简单,主要是数据库的查询.一个DBHelp类,封装了数据库的操作,然后Controller中进行中各种查 ...

  2. 1038 Recover the Smallest Number (30)(30 分)

    Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...

  3. Apache+OpenSSL实现证书服务器提供HTTPS

    通过 Linux+Apache+OpenSSL 实现 SSL ( Secure Socket Layer )证书服务器,提供安全的 HTTPS ( Hypertext Transfer Protoco ...

  4. python mysql 查询返回字典结构

    cur = self.conn.cursor(MySQLdb.cursors.DictCursor)加上MySQLdb.cursors.DictCursor可以返回字典结构 {列名:值} class ...

  5. IDEA常用快捷键整理

    快速定位文件:   Ctrl+E,最近的文件 Ctrl+Shift+E,最近更改的文件 Alt+Shift+C,最近打开的文件 Ctrl+N,快速打开类 Ctrl+Shift+N,快速打开文件   当 ...

  6. pycharm安装 package报错:module 'pip' has no attribute 'main'

    转自: <pycharm安装 package报错:module 'pip' has no attribute 'main'> https://www.cnblogs.com/Fordest ...

  7. 安装zendstudio和破解方法及配置svn

    下载zendstudio12文件http://www.zendstudio.net/zend-studio-all-in-one-download/ 下载破解补丁http://pan.baidu.co ...

  8. HTTP之缓存首部

    缓存分好多种:服务器缓存,第三方缓存,浏览器缓存等.其中浏览器缓存是代价最小的,因为浏览器缓存依赖的是客户端,而几乎不耗费服务器端的资源.浏览器做缓存需要给浏览器发送指定的Http头,告诉浏览器缓存多 ...

  9. How to Write a Spelling Corrector用java 写拼写检查器 Java实现 以备查验

    import java.io.*;import java.util.*;import java.util.regex.*; class Spelling { private final HashMap ...

  10. centos7安装与配置ansible

    此次测试总共有三台机,分别如下: ansible服务器:10.0.0.20 client01:10.0.0.21 client02:10.0.0.22 一.安装ansible 1. python版本需 ...