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
 //注意,这里的排序为排序迭代过程中完成的某一步而已,并不是最终结果
//我们可以直接用sort来代替排序的部分,无论是合并排序还是插入排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, a;
vector<int>nums, ans, temp;
int main()
{
cin >> N;
for (int i = ; i < N; ++i)
{
cin >> a;
nums.push_back(a);
}
for (int i = ; i < N; ++i)
{
cin >> a;
ans.push_back(a);
}
temp = nums;
bool f = false;
for (int i = ; i < N; ++i)//进行插入排序
{
sort(temp.begin(), temp.begin() + i + );//第一趟排序应该是排序前两个数,第i趟排序分别排序前i+1个数
if (temp == ans)
{
f = true;
cout << "Insertion Sort" << endl;
sort(temp.begin(), temp.begin() + i + );//再一次迭代
break;
}
}
if (f == false)//那就是合并排序
{
cout << "Merge Sort" << endl;
temp = nums;
for (int i = ; i < N; i *= )//将每i个元素归并为一个非递减序列
{
for (int j = ; j < N; j += i)
sort(temp.begin() + j, temp.begin() + (j + i < N ? j + i : N));
if (temp == ans)
{
for (int j = ; j < N; j += i * )//进行下一次迭代
sort(temp.begin() + j, temp.begin() + (j + i * < N ? j + i * : N));
break;
}
}
}
for (int i = ; i < N; ++i)
cout << temp[i] << (i < N - ? " " : "");
return ;
}

PAT甲级——A1089 Insert or Merge的更多相关文章

  1. PAT甲级1089. Insert or Merge

    PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...

  2. A1089. Insert or Merge

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

  3. A1089 Insert or Merge (25 分)

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

  4. PAT Advanced 1089 Insert or Merge (25) [two pointers]

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

  5. PAT_A1089#Insert or Merge

    Source: PAT A1089 Insert or Merge (25 分) Description: According to Wikipedia: Insertion sort iterate ...

  6. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

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

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

  8. PAT 1089 Insert or Merge[难]

    1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  9. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. nio读取文件,输出文件

    io流的一种: package com.cxy.ssp.Automic; import java.io.FileOutputStream; import java.io.IOException; im ...

  2. STM32---初学者用库函数好还是直接对寄存器操作比较好

    引用:http://blog.csdn.net/u010349006/article/details/416 首先,两个都是C语言.从51过渡过来的话,就先说寄存器操作.每个MCU都有自己的寄存器,5 ...

  3. mysql 12章_MySQL数据库的高级管理

    一. 用户管理 Root用户是MySQL数据库管理系统中的系统管理员,但在实际开发过程中通常需要根据不同的开发者分配不同的用户,这样有利于用户的管理和维护. . 用户的创建: ) 方式1:使用MySQ ...

  4. iOS组件化开发-CocoaPods简介

    CocoaPods简介 任何一门开发语言到达一定阶段就会出现第三方的类库管理工具,比如Java的Maven.WEB的Webpack等.在iOS中类库的管理工具-CocoaPods. 利用CocoaPo ...

  5. Python基础知识之5——函数基础

    函数 函数是一个独立且封闭完成特定功能的代码块,可以在任何地方被调用.Python内置了很多函数,我们可以直接调用,使用的时候,先查询下Python的官方文档即可: http://docs.pytho ...

  6. js 阻止事件

    event.stopPropagation();//阻止事件冒泡 ,可阻止父类事件的发生 event.preventDefault();//阻止默认行为 如A标签

  7. 校园商铺-4店铺注册功能模块-8店铺注册之Controller层的改造

    不合理的地方: 1. 并不需要将InputStream转换成File类型,直接将InputStream传进入交给CommonsMultipartfile去处理就可以了 如果做这样的转换,每次都需要生成 ...

  8. 线性dp——cf1012C好题

    比较套路的dp题 /* dp[i][j][0|1]:前i座山盖了j座房子,第i座不盖|盖 dp[i][j][0]=min( dp[i-1][j][0] , dp[i-1][j][1]+max(0,a[ ...

  9. js面试总结3

    异步和单线程 题目: 1.同步和异步的区别? 2.一个关于setTimeout的笔试题. 3.前段使用异步的场景有哪些? 什么是异步? console.log(100) setTimeout(func ...

  10. js 常见功能总会

    一.随着页面滚动,元素到达可视区域,显示特殊样式 <!DOCTYPE html> <html lang="en"> <head> <met ...