• 1089 Insert or Merge


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

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

    Sample Output 2:

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

    和乙级P1035一样:1035 插入与归并 (25 分)_Brosto_Cloud的博客-CSDN博客 

    1. #include
    2. #include
    3. using namespace std;
    4. bool cmp(int a[], int b[], int n) {
    5. for (int i = 0; i < n; i++) {
    6. if (a[i] != b[i]) {
    7. return false;
    8. }
    9. }
    10. return true;
    11. }
    12. bool flag;
    13. int main() {
    14. int a[110], b[110], c[110], n;
    15. cin >> n;
    16. for (int i = 0; i < n; i++) {
    17. cin >> a[i];
    18. c[i] = a[i];
    19. }
    20. for (int i = 0; i < n; i++) {
    21. cin >> b[i];
    22. }
    23. //插入排序
    24. for (int i = 1; i < n; i++) {
    25. //会存在输入的两组序列本就相等的情况
    26. //这是因为插入排序前几个元素正好是升序
    27. //所以在前几次排序步骤中的顺序并没有发生变化
    28. //需要特判,此时继续下一步排序
    29. //归并排序不会出现这种情况
    30. if (cmp(a, b, n) && i == 1) {
    31. i++;
    32. }
    33. sort(a, a + i);
    34. if (flag) {
    35. cout << "Insertion Sort" << endl;
    36. for (int j = 0; j < n; j++) {
    37. cout << a[j];
    38. if (j != n - 1) {
    39. cout << ' ';
    40. }
    41. }
    42. return 0;
    43. }
    44. if (cmp(a, b, n)) {
    45. flag = 1;
    46. }
    47. }
    48. //归并排序
    49. int t = 2;//每组的元素个数
    50. int x = n / t;//一共有几组
    51. while (x) {
    52. for (int i = 0; i < x; i++) {
    53. sort(c + t * i, c + t * (i + 1));
    54. }
    55. if (t * x < n) {
    56. sort(c + t * x, c + n);
    57. }
    58. t *= 2;
    59. x = n / t;
    60. if (flag) {
    61. cout << "Merge Sort" << endl;
    62. for (int j = 0; j < n; j++) {
    63. cout << c[j];
    64. if (j != n - 1) {
    65. cout << ' ';
    66. }
    67. }
    68. break;
    69. }
    70. if (cmp(c, b, n)) {
    71. flag = 1;
    72. }
    73. }
    74. return 0;
    75. }

     

  • 相关阅读:
    2022杭电多校(二)
    sort()排序函数(c++)
    【强化学习论文合集】ICML-2021 强化学习论文
    图论-SPFA算法
    Java代码基础算法练习-求数据序列的最大值及最小值---2024.3.15
    LeetCode 317 周赛
    RunnerGo UI自动化使用流程
    机电团队怎么使用软件系统快速实施 部署
    C++ 学习链接
    键盘出口欧洲地区需要做哪些检测认证?
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/127103716