zoukankan      html  css  js  c++  java
  • pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25)

    时间限制
    100 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    According to Wikipedia:

    Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At 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

    思路:是什么排序方式可以依据插入排序的特点来判断:插入排序排了一部分的数列的前半部分是从小到大排列的,后面一部分和原数列一样。
    至于堆排序的操作,每次取出未排序部分数列的最大值后,再对未排序部分进行过滤调整。
    AC代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<string>
    #include<iomanip>
    #include<map>
    #include<stack>
    #include<set>
    #include<queue>
    #include<cstdio>
    using namespace std;
    #define N_MAX 200+5
    int n;
    int a[N_MAX],b[N_MAX];
    
    void adjust(int *a,int i,int N) {//将a[i]处的值调整为以它为根的子树中的最大值
      int child;
      int tmp=a[i];
      for (; (2 * i + 1) < N;i=child) {
        child = 2 * i + 1;
        if (child != N - 1 && a[child + 1] > a[child]) {
          child++;
        }
        if (tmp < a[child]) { a[i] = a[child];}
        else break;
      }
      a[i] = tmp;
    }
    
    int main() {
      while (cin>>n) {
        for (int i = 0; i < n; i++)cin >> a[i];
        for (int i = 0; i < n; i++)cin >> b[i];
        int k ;
        for (k = 0; k < n - 1 && b[k] <= b[k + 1]; k++);
        int p = k + 1;
        for (; p < n&&b[p] == a[p]; p++);
        if (p!=n) {
          puts("Heap Sort");
          int num = n - 1,j;
          for (j = num; j >=0 ;j--) {
            if (b[j] < b[0])break;
          }
          swap(b[j], b[0]);
          adjust(b, 0, j);
          for (int i = 0; i < n;i++) printf("%d%c", b[i], i + 1 == n ? '
    ' : ' ');
        }
        else {
          puts("Insertion Sort");
          sort(b,b+k+2);
          for (int i = 0; i < n; i++)
            printf("%d%c",b[i],i+1==n?'
    ':' ');
        }
      }
      return 0;
    }

     STL里也有关于堆的操作make_heap,pop_heap().这里取堆排序操作的下一步,可以直接用这些函数解决。

    代码:

    #define _CRT_SECURE_NO_DEPRECATE
    #pragma warning(disable:4996)
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<map>
    #include<cctype>
    #include<cmath>
    #include<cstring>
    #include<vector>
    #include<set>
    #include<queue>
    #include<limits.h>
    #include<sstream>
    using namespace std;
    typedef long long ll;
    #define N_MAX 1000+5
    #define INF 0x3f3f3f3f
    int n;
    vector<int>oring;
    vector<int>cur;
    int main() {
        while (cin>>n) {
            oring.resize(n);
            cur.resize(n);
            for (int i = 0; i < n; i++)cin >> oring[i];
            for (int i = 0; i < n; i++)cin >> cur[i];
            int k=0;
            while (k < n-1&&cur[k] <= cur[k + 1])k++;
            int p = k + 1;
            while (p < n&&cur[p] == oring[p])p++;
            if (p != n) {
                puts("Heap Sort");
                int num ;
                for (num = n - 1; num >= 0; num--)
                    if (cur[num] < cur[0])break;
                pop_heap(cur.begin(), cur.begin() + num+1);
                for(int i=0;i<cur.size();i++)
                    printf("%d%c", cur[i], i + 1 == cur.size() ? '
    ' : ' ');
            }
            else {
                puts("Insertion Sort");
                sort(cur.begin(),cur.begin()+k+2);
                for (int i = 0; i < cur.size(); i++)
                    printf("%d%c",cur[i],i+1==cur.size()?'
    ':' ');
            }
    
        }
        return 0;
    }
  • 相关阅读:
    [Luogu] P1886 滑动窗口
    [Luogu] P1195 口袋的天空
    [Luogu] P1331 海战
    [Luogu] P3952 时间复杂度
    运营活动如何防刷
    考研政治刷题小程序
    考研刷题小程序
    在线答题活动小程序
    知识竞答小程序v2.0
    知识竞答小程序
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/8526162.html
Copyright © 2011-2022 走看看