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.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

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 "Heap 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
/*
* 这题思路比较简单:
1.中间序列 前面有序 后面和原始序列相同 则为插入排序。
2.堆排序 后面的数 应该是原数组中从大到小的数 如果遇到不满足这个条件的数 说明堆排序即将进行到当前这个数
在走一趟堆排序即可
*/
#include "iostream"
#include "algorithm"
using namespace std;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;
}
void adjust(int b[], int n) {
int parent = ;
int child;
int temp = b[parent];
for (; parent * + <= n;parent = child) {
child = parent * + ;
if (child + <= n && b[child + ] > b[child])
child = child + ;
if (b[child] <= temp)
break;
b[parent] = b[child];
}
b[parent] = temp;
}
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 << "Heap Sort" << endl;
int i;
sort(a, a + n);
for (i = n-; i >= ; i--) {
if (a[i] != b[i])
break;
}
int temp = b[];
b[] = b[i];
b[i] = temp;
adjust(b,i-); /* 剩下的元素调整为最大堆 */
print(b, n);
}
}

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 6 0
6 4 5 1 0 3 2 7 8 9

Sample Output 2:

Heap Sort
5 4 3 1 0 2 6 7 8 9

1098. Insertion or Heap Sort (25)的更多相关文章

  1. pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  2. PAT (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)

    http://www.patest.cn/contests/pat-a-practise/1098 According to Wikipedia: Insertion sort iterates, c ...

  3. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  4. PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)

    题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...

  5. 【PAT甲级】1098 Insertion or Heap Sort (25 分)

    题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...

  6. PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]

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

  7. 1098 Insertion or Heap Sort (25分)

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

  8. PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...

  9. pat1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

随机推荐

  1. FocusWriter

    2. FocusWriter 如果你正在从事某种写作——小说.博客.文档等——你绝对希望认识一下FocusWriter.它已经有近十年的发布历史了,但是一直是我们最喜欢的无分心写作应用之一.如果你希望 ...

  2. 173. Binary Search Tree Iterator

    题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with th ...

  3. Android 设置EditText光标位置

    Android中有很多可编辑的弹出框,其中有些是让我们来修改其中的字符,这时光标位置定位在哪里呢? 刚刚解了一个bug是关于这个光标的位置的,似乎Android原生中这种情况是把光标定位到字符串的最前 ...

  4. mysql 更新唯一主键列 被堵塞

    mysql> select @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEAT ...

  5. C#读取注册表信息

    注册表是视窗系统的一个核心的数据库,在这个数据库中存放中与系统相关的各种参数,这些参数直接控制中系统的启动.硬件的驱动程序安装信息以及在视窗系统上运行的各种应用程序的注册信息等.这就意味着,如果注册表 ...

  6. 【HDOJ】4691 Front compression

    后缀数组基础题目,dc3解. /* 4691 */ #include <iostream> #include <sstream> #include <string> ...

  7. css揭秘之按钮的实现技巧

    <html> <title>css</title> <style> button{ padding: .3em .8em; border: 1px so ...

  8. Java [leetcode 36]Valid Sudoku

    题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  9. SQL Server使用规范(转)

    常见的字段类型选择 1.字符类型建议采用varchar/nvarchar数据类型 2.金额货币建议采用money数据类型 3.科学计数建议采用numeric数据类型 4.自增长标识建议采用bigint ...

  10. jquery M97-datepicker日历控件

    My97DatePicker是一款非常灵活好用的日期控件.使用非常简单. 1.下载My97DatePicker组件包 2.在页面中引入该组件js文件:     <script type=&quo ...