PAT 1089. Insert or Merge (25)
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)的更多相关文章
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- PAT 1089 Insert or Merge[难]
1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...
- PAT (Advanced Level) 1089. Insert or Merge (25)
简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...
- PAT Advanced 1089 Insert or Merge (25) [two pointers]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- 1089. Insert or Merge (25)
题目如下: According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, ...
- 1089. Insert or Merge (25)-判断插入排序还是归并排序
判断插入排序很好判断,不是的话那就是归并排序了. 由于归并排序区间是2.4.8开始递增的,所以要判断给出的归并排序执行到哪一步,就要k从2开始枚举. 然后再对每个子区间进行一下sort即可. #inc ...
- PAT 1089. Insert or Merge
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output li ...
- 1089 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
随机推荐
- webkit私有css3属性 -webkit-overflow-scrolling:touch;
-webkit-overflow-scrolling:touch;/*允许独立的滚动区域和触摸回弹*/ 这个属性可以提高滚动的平滑度
- oct(x) 将一个数字转化为8进制
>>> a = 12 >>> b 21 >>> c = oct(a) >>> d = oct(b) >>> c ...
- conky 配置变量表
转自conky 配置变量表 项目主页:http://conky.sourceforge.net/ 文档说明:http://conky.sourceforge.net/docs.html Variabl ...
- Spring MVC使用commons fileupload实现文件上传功能
通过Maven建立Spring MVC项目,引入了Spring相关jar依赖. 1.为了使用commons fileupload组件,需要在pom.xml中添加依赖: <properties&g ...
- Bootstrap 貌似不错,先做一下记录
Bootstrap 简洁.直观.强悍的前端开发框架,让web开发更迅速.简单. http://www.bootcss.com/
- table 表头固定
<html> <head> <title>Test</title> <style type="text/css"> .d ...
- android 业务需求: 先干掉自己, 在重启自己
// 重启应用 public void restartApp() { Intent intent = new Intent(); // 参数1:包名,参数2:程序入口的activity intent. ...
- *[topcoder]LittleElephantAndIntervalsDiv1
http://community.topcoder.com/stat?c=problem_statement&pm=12822&rd=15707 第一次用C++提交,艰辛.首先想到可以 ...
- 重温XML
关于什么是XML,以及XML有什么作用,网络上很多,我就在这里不班门弄斧,写博客,是我的一个习惯,究其内容无非个人情感,心得体会,转载,技术相关的,或者一时心血来潮的个人之谈,但是我是一个小心翼翼的人 ...
- 【HDOJ】1009 FatMouse' Trade
这道题目是一道非常简单的贪心,但是我却修改了1h+.原因就是qsort的comp有bug.其实还是题目中的数据可以为0.除数为0真的要慎重啊.后来改为结构体,加一层循环选取最大值,果然ac啊.wa了几 ...