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:

  1. 10
  2. 3 1 2 8 7 5 9 4 6 0
  3. 1 2 3 7 8 5 9 4 6 0
  1. /*
  2. * 这题思路比较简单:
  3. 1.中间序列 前面有序 后面和原始序列相同 则为插入排序。
  4. 2.堆排序 后面的数 应该是原数组中从大到小的数 如果遇到不满足这个条件的数 说明堆排序即将进行到当前这个数
  5. 在走一趟堆排序即可
  6. */
  7. #include "iostream"
  8. #include "algorithm"
  9. using namespace std;bool judge(int a[],int b[],int n) { /* 判断是不是插入排序 */
  10. int len = ;
  11. int i;
  12. for ( i = ; i < n - ; i++)
  13. if (b[i] > b[i + ]) {
  14. len = i + ;
  15. break;
  16. }
  17. for (i=len; i < n; i++) {
  18. if (a[i] != b[i])
  19. return false;
  20. }
  21. sort(b, b + len + );
  22. return true;
  23. }
  24. void print(int a[], int n) {
  25. for (int i = ; i < n; i++) {
  26. if (i == )
  27. cout << a[i];
  28. else
  29. cout << " " << a[i];
  30. }
  31. cout << endl;
  32. }
  33. void adjust(int b[], int n) {
  34. int parent = ;
  35. int child;
  36. int temp = b[parent];
  37. for (; parent * + <= n;parent = child) {
  38. child = parent * + ;
  39. if (child + <= n && b[child + ] > b[child])
  40. child = child + ;
  41. if (b[child] <= temp)
  42. break;
  43. b[parent] = b[child];
  44. }
  45. b[parent] = temp;
  46. }
  47. int main() {
  48. int a[], b[];
  49. int n;
  50. cin >> n;
  51. for (int i = ; i < n; i++) {
  52. cin >> a[i];
  53. }
  54. for (int i = ; i < n; i++) {
  55. cin >> b[i];
  56. }
  57. if (judge(a, b, n)) {
  58. cout << "Insertion Sort" << endl;
  59. print(b, n);
  60. }
  61. else {
  62. cout << "Heap Sort" << endl;
  63. int i;
  64. sort(a, a + n);
  65. for (i = n-; i >= ; i--) {
  66. if (a[i] != b[i])
  67. break;
  68. }
  69. int temp = b[];
  70. b[] = b[i];
  71. b[i] = temp;
  72. adjust(b,i-); /* 剩下的元素调整为最大堆 */
  73. print(b, n);
  74. }
  75. }

Sample Output 1:

  1. Insertion Sort
  2. 1 2 3 5 7 8 9 4 6 0

Sample Input 2:

  1. 10
  2. 3 1 2 8 7 5 9 4 6 0
  3. 6 4 5 1 0 3 2 7 8 9

Sample Output 2:

  1. Heap Sort
  2. 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. Android:控件GridView的使用

    如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView. <?xml version="1.0" encoding="u ...

  2. 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!

    启动 Eclipse 弹出"Failed to load the JNI shared library jvm.dll"错误的解决方法 http://blog.csdn.net/z ...

  3. windows 下 文件属性及目录列表操作

    转:http://blog.sina.com.cn/s/blog_686d0fb001012tsg.html 我们需要一个结构体和几个函数.这些函数和结构体在<io.h>的头文件中,结构体 ...

  4. Linux命令面试常考的简单汇总

    1.显示日期与时间的命令:date 2.显示日历的命令:cal 3.简单好用的计算器:bc 4.热键“命令补全或文件补齐”:Tab 5.热键“中断目前程序”:Ctrl+C 6.热键“键盘输入结束(En ...

  5. (从终端看linux-1)linux tty pty pts 概念 区别

    基本概念: 1> tty(终端设备的统称):tty一词源于Teletypes,或者teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘通过阅读和发送信息的东西,后来这东 ...

  6. 远程唤醒 N54L

    远程唤醒 N54L 我的N54L装了一块买时带的WD500G黑盘,又装了一个WD2T绿盘,存些电影歌曲什么的,当NAS用,装的WIN7系统,长时间不使用就自动关机了,每次都得按一下电源开关键,于是就研 ...

  7. sudo: ./sd_fusing.sh:找不到命令

    1. -----

  8. sublime text2卸载和重新安装

    很多同学使用 sublime text2 的时候,出现一些奇怪的bug,且重启无法修复. 于是,就会想到卸载 sublime text2 再重新安装. 然而,你会发现,重新安装后,这个bug任然存在, ...

  9. Oracle数据库生成UUID

    从Data Ghost的blog得知,原来可以用Oracle来生成UUID,做法很简单,如下: select sys_guid() from dual;  数据类型是 raw(16) 有32个字符.

  10. 中南大学oj 1317 Find the max Link 边权可以为负的树上最长路 树形dp 不能两遍dfs

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1317经典问题:树上最长路,边权可以为负值的,树形dp,不能用两边dfs.反例:5 41 2 22 ...