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 (<=100). 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 resulting 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
/*
* 这题给了200ms的时间 n<=100 所以这题即使每排序一趟与中间序列进行比较 时间复杂度也仅为O(n^2) 完全可以
* 还有个O(n)的算法:
1. 插入排序特点为 前面均为有序序列 没排序的与原序列相同
2. 归并排序进行了几趟归并 可通过len=2 到 n/2 进行枚举 直到不满足每一组归并序列都有序的条件时 即可得到当前
进行了几次归并 另外 这题题目好像有点问题 比如 1 2 4 3 中间序列为 1 2 4 3 你根本不知道是进行了2趟还是3趟插入排序@—-@。
*/
#include "iostream"
#include "algorithm"
using namespace std;
void merge(int a[], int temp[], int left, int right, int rightEnd) { /* 合并2个有序的子序列 */
int l = left;
int leftEnd = right - ;
int len = rightEnd - left + ;
while (left <= leftEnd && right <= rightEnd) {
if (a[left] <= a[right]) {
temp[l] = a[left];
left++;
}
else {
temp[l] = a[right];
right++;
}
l++;
}
while (left <= leftEnd)
temp[l++] = a[left++];
while (right <= rightEnd)
temp[l++] = a[right++];
}
/* 一趟2路归并 */
void MergePass(int a[], int temp[], int n, int length) { /* length:当前有序子列的长度 */
int i;
for (i = ; i <= n - * length; i += * length) {
merge(a, temp, i, i + length, i + * length - );
}
if (i + length < n)
merge(a, temp, i, i + length, n - );
else
for (int j = i; j < n; j++)
temp[j] = a[j];
}
int getLen(int b[], int n) { /* 进行一趟归并排序 */
for (int len = ; len <= n / ; len *= ) {
int i;
for (i = ; i <= n - * len; i += * len)
if (!is_sorted(b + i, b + i + * len)) return len;
if (!is_sorted(b + i, b + n)) return len;
}
}
bool judge(int a[],int b[],int n) { /* 判断是不是插入排序 */
int len = ;
int i;
for ( i = ; i < n - ; i++)
if (b[i] > b[i + ]) {
len = i + ;
break;
}
for (i=len; i < n; i++) {
if (a[i] != b[i])
return false;
}
sort(b, b + len + );
return true;
}
void print(int a[], int n) {
for (int i = ; i < n; i++) {
if (i == )
cout << a[i];
else
cout << " " << a[i];
}
cout << endl;
}
int main() {
int a[], b[];
int n;
cin >> n;
for (int i = ; i < n; i++) {
cin >> a[i];
}
for (int i = ; i < n; i++) {
cin >> b[i];
}
if (judge(a, b, n)) {
cout << "Insertion Sort" << endl;
print(b, n);
}
else {
cout << "Merge Sort" << endl;
int* temp = (int *)malloc(n * sizeof(int));
if (temp != NULL) {
MergePass(b, temp, n, getLen(b, n));
print(temp,n);
free(temp);
}
}
}

PAT 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. PAT 1089 Insert or Merge[难]

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

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

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

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

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

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

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

  6. 1089. Insert or Merge (25)

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

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

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

  8. PAT 1089. Insert or Merge

    Insertion sort iterates, consuming one input element each repetition, and growing a sorted output li ...

  9. 1089 Insert or Merge (25 分)

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

随机推荐

  1. sqlite优化记录:建立索引加快查询速度

    凡是数据库中,索引的存在就是为了提高查询速度的,数据库的索引有点类似于书本上面的目录的概念,因为在英文中都是index,事实上也就是目录. 其算法应该叫做“倒排索引”,这个其实也类似于搜索引擎里面的基 ...

  2. 中国海洋大学第四届朗讯杯高级组 I Cuckoo for Hashing

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2719&cid=1203 题意 :意思就是哈希来的,具体大意就是说有两个哈希表,然后有这样 ...

  3. Memcached总结四:用ava程序连接memcached进行操作

    1. Memcached的Java环境设置 需要下载spymemcached-2.10.3.jar,并把这个jar放到java程序的classpath中才能使用memcached. 在下面的程序,假设 ...

  4. easyui源码翻译1.32--Combo(自定义下拉框)

    前言 扩展自$.fn.validatebox.defaults.使用$.fn.combo.defaults重写默认值对象.下载该插件翻译源码 自定义下拉框显示一个可编辑的文本框和下拉面板在html页面 ...

  5. javaweb学习总结(四十)——编写自己的JDBC框架

    一.元数据介绍 元数据指的是"数据库"."表"."列"的定义信息. 1.1.DataBaseMetaData元数据 Connection.g ...

  6. 解决“重新安装vmware-tools”灰色而无法安装的问题

    前几天重装系统,之后虚拟机需要重新装,装好后要使用vmware-tools实现文件共享,却发现虚拟机那里显示为灰色的,无法安装vmware-tools,在共享文件夹那里设置好共享的文件夹猴也没有用,/ ...

  7. 转:Apache和Nginx运行原理解析

    Web服务器 Web服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务. 应用层使用HTTP协议. HTML文档格式. 浏览器统一资源定位器(URL). Web服 ...

  8. Grid表格属性

    <Grid> <Grid.ColumnDefinitions> <!--添加列--> <ColumnDefinition Width="/> ...

  9. BZOJ_1007_ [HNOI2008]_水平可见直线_(单调栈+凸包)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1007 给出一些直线,沿着y轴从上往下看,能看到多少条直线. 分析 由于直线相交,会遮挡住一些直 ...

  10. Aspose.Words组件介绍及使用—基本介绍与DOM概述

    1.基本介绍 Aspose.Words是一个商业.NET类库,可以使得应用程序处理大量的文件任务.Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XP ...