1098 Insertion or Heap Sort

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

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

题目大意:给你一组初始序列和中间序列,让你根据序列来判断是插入排序还是堆排序。然后输出中间序列的下一趟排序

解法1:按照题目所给要求对插入排序的每一步进行模拟,每一趟的排序结果和中间序列进行比较,如果相同则为插入排序,否则为堆排序。注意:因为序列是按照从小到大排列,所以根据堆的性质我们要构建的堆是大根堆。

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e5 + 10;
  4. int init_array[N], final_array[N], tmp_array[N];
  5. int n;
  6. bool same_array() {
  7. for (int i = 1; i <= n; i++) {
  8. if (init_array[i] != final_array[i]) return false;
  9. }
  10. return true;
  11. }
  12. void print_array() {
  13. for (int i = 1; i <= n; i++) {
  14. cout << init_array[i];
  15. if (i != n)
  16. cout << " ";
  17. else
  18. cout << endl;
  19. }
  20. }
  21. bool insert_sort() {
  22. int i, j;
  23. bool flag = false;
  24. for (i = 2; i <= n; i++) {
  25. if (i != 2 && same_array()) flag = true;
  26. if (init_array[i] < init_array[i - 1]) {
  27. init_array[0] = init_array[i];
  28. for (j = i - 1; init_array[0] < init_array[j]; j--)
  29. init_array[j + 1] = init_array[j];
  30. init_array[j + 1] = init_array[0];
  31. if (flag) {
  32. return true;
  33. }
  34. }
  35. }
  36. return false;
  37. }
  38. void down(int u, int sizes) {
  39. int t = u;
  40. if (u * 2 <= sizes && init_array[u * 2] > init_array[t]) t = u * 2;
  41. if (u * 2 + 1 <= sizes && init_array[u * 2 + 1] > init_array[t])
  42. t = u * 2 + 1;
  43. if (t != u) {
  44. swap(init_array[t], init_array[u]);
  45. down(t, sizes);
  46. }
  47. }
  48. void up(int u) {
  49. while (u / 2 && init_array[u / 2] > init_array[u]) {
  50. swap(init_array[u / 2], init_array[u]);
  51. u /= 2;
  52. }
  53. }
  54. void heap_sort() {
  55. //初建堆,因为是从小打到输出所以构建的是大顶堆
  56. for (int i = n / 2; i >= 1; i--) down(i, n);
  57. bool flag = false;
  58. for (int i = n; i > 1; i--) {
  59. if (i != n && same_array()) flag = true;
  60. swap(init_array[1], init_array[i]);
  61. down(1, i - 1);
  62. if (flag) {
  63. print_array();
  64. return;
  65. }
  66. }
  67. }
  68. int main() {
  69. scanf("%d", &n);
  70. // sizes = n;
  71. for (int i = 1; i <= n; i++) {
  72. scanf("%d", &init_array[i]);
  73. tmp_array[i] = init_array[i];
  74. }
  75. for (int i = 1; i <= n; i++) scanf("%d", &final_array[i]);
  76. if (insert_sort()) {
  77. puts("Insertion Sort");
  78. print_array();
  79. } else {
  80. puts("Heap Sort");
  81. for (int i = 1; i <= n; i++) init_array[i] = tmp_array[i];
  82. heap_sort();
  83. }
  84. return 0;
  85. }

解法2:参考柳神的解法 ,堆排序大致思路相同,关键的是对插入排序的优化。根据插入排序的性质可以知道结点L(i)前L(1 ~i - 1)个元素都是有序序列,结点L(i + 1, n)个元素都是无序序列。所以我们只需要在中间序列中找到结点L(i)然后比较中间序列的L(i + 1, n)和原序列的L(i + 1, n),如果相同则为插入排序,如果不同则为堆排序。如果是插入排序直接对L(1 ~ i + 1)个元素进行sort即可。

利用插入排序优化的代码如下:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e5 + 10;
  4. int init_array[N], final_array[N];
  5. int n;
  6. void down(int h[], int u, int sizes) {
  7. int t = u;
  8. if (u * 2 <= sizes && h[u * 2] > h[t]) t = u * 2;
  9. if (u * 2 + 1 <= sizes && h[u * 2 + 1] > h[t])
  10. t = u * 2 + 1;
  11. if (t != u) {
  12. swap(h[t], h[u]);
  13. down(h, t, sizes);
  14. }
  15. }
  16. int main() {
  17. scanf("%d", &n);
  18. // sizes = n;
  19. for (int i = 1; i <= n; i++) {
  20. scanf("%d", &init_array[i]);
  21. }
  22. for (int i = 1; i <= n; i++) scanf("%d", &final_array[i]);
  23. int p = 1;
  24. while(p < n && final_array[p] <= final_array[p + 1]) p++;
  25. bool flag = true;
  26. for (int i = p + 1; i <= n; i++) {
  27. if (init_array[i] != final_array[i]) {
  28. flag = false;
  29. break;
  30. }
  31. }
  32. if (flag) {
  33. puts("Insertion Sort");
  34. sort(final_array + 1, final_array + p + 2);
  35. }
  36. else {
  37. puts("Heap Sort");
  38. int index = n;
  39. while(index > 2 && final_array[index] > final_array[1]) index--;
  40. swap(final_array[1], final_array[index]);
  41. down(final_array,1, index - 1);
  42. }
  43. for (int i = 1; i <= n; i++) {
  44. cout << final_array[i];
  45. if (i != n)
  46. cout << " ";
  47. else
  48. cout << endl;
  49. }
  50. return 0;
  51. }

1098 Insertion or Heap Sort——PAT甲级真题的更多相关文章

  1. PAT甲级1098. Insertion or Heap Sort

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

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

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

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

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

  4. 1098 Insertion or Heap Sort

    1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...

  5. PAT 甲级真题题解(63-120)

    2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...

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

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

  7. 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 ...

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

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

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

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

随机推荐

  1. C++基本之 运算符重载

    =====>友元运算符#include <iostream> using namespace std; class Test { public: Test(int a = 0) { ...

  2. 【从零开始撸一个App】RecyclerView的使用

    目标 前段时间打造了一款简单易用功能全面的图片上传组件,现在就来将上传的图片以图片集的形式展现到App上.出于用户体验考虑,加载新图片采用[无限]滚动模式,Android平台上我们优选Recycler ...

  3. XV6学习(12)Lab lock: Parallelism/locking

    代码在github上 这一次实验是要对XV6内部的锁进行优化,减少锁争用,提高系统的性能. Memory allocator (moderate) 第一个实验是对XV6内核的内存页面分配器进行改进,改 ...

  4. AtCoder - agc043_a 和 POJ - 2336 dp

    题意: 给你一个n行m列由'#'和'.'构成的矩阵,你需要从(1,1)点走到(n,m)点,你每次只能向右或者向下走,且只能走'.'的位置. 你可以执行操作改变矩阵: 你可以选取两个点,r0,c0;r1 ...

  5. P4718 [模板]Pollard-Rho算法

    对一个大质数进行质因数分解 需要引用miller-robin来判素数 一直写的gcd居然挂掉了... 以后用__gcd了 #include <bits/stdc++.h> using na ...

  6. 【hdu 3579】Hello Kiki(数论--拓展欧几里德 求解同余方程组)

    题意:Kiki 有 X 个硬币,已知 N 组这样的信息:X%x=Ai , X/x=Mi (x未知).问满足这些条件的最小的硬币数,也就是最小的正整数 X. 解法:转化一下题意就是 拓展欧几里德求解同余 ...

  7. 7.Topic交换机之模拟文件分类

    标题 : 7.Topic交换机之模拟文件分类 目录 : RabbitMQ 序号 : 7 Password = "123456", AutomaticRecoveryEnabled ...

  8. String的20个方法

    String的20个方法 面试题 1.new和不new的区别 String A="OK"; String B="OK";//会去常量池查找有没有"Ok ...

  9. 牛客国庆2 F-平衡二叉树【非原创】

    题目:戳这里 学习博客:戳这里

  10. Linux 网络协议栈开发基础篇—— 网桥br0

    一.桥接的概念 简单来说,桥接就是把一台机器上的若干个网络接口"连接"起来.其结果是,其中一个网口收到的报文会被复制给其他网口并发送出去.以使得网口之间的报文能够互相转发. 交换机 ...