• 浙大pat甲级题目---1098. Insertion or Heap Sort (25)


    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

    题目大意:先介绍了一下插入排序和堆排序,然后给出两个序列,第一个序列是无序的初始序列,第二个序列指用堆排序或插入排序的某一步迭代的序列,我们的目标是找出用的是哪中迭代方法,并写出下一个迭代序列
    题目思路:很简单,就是写一个插排和堆排,然后在每一次迭代之后看和题目序列是否一样(可以用vector的==轻松实现),之后用一个flag变量来标记即可
    这个题没什么坑(注意最后一个数字后不能有空格),只要你了解两个排序即可
    请大家注意堆排序建堆的时候一定要有堆的大小那个参数,最后堆排序的时候是按照迭代次数逐渐减小堆的大小的
    #include <iostream>
    #include<vector>
    using namespace std;
    bool insertion_sort(vector<int> &a,vector<int> &b)
    {
        bool flag=false;
        for(int i=1;i<a.size();i++)
        {
            int j=i-1;//j为有序区下表
            int temp=a[i];//temp为无序区待排序的数
            while(temp<a[j]&&j>=0)
            {
                a[j+1]=a[j];
                j--;
            }
            a[j+1]=temp;
            if(flag)
            {
                return true;
            }
            if(a==b)
            {
                flag=true;
            }
        }
        return false;
    }
    void swap(vector<int> &arry,int a,int b)
    {
        int t=arry[a];
        arry[a]=arry[b];
        arry[b]=t;
    }
    void adjust_heap(vector<int> &a,int j,int m)
    {
        int left=j*2+1;
        int right=left+1;
        int max;
        if(left<m&&a[left]>a[j])
            max=left;
        else
            max=j;
        if(right<m&&a[right]>a[max])
            max=right;
        if(max!=j)
        {
            swap(a,max,j);
            adjust_heap(a,max,m);
        }
    
    }
    void build_heap(vector<int> &a)
    {
        int begin=(int)a.size()/2-1;//注意是减一
        for(int i=begin;i>=0;i--)
        {
            adjust_heap(a,i,a.size());
        }
    
    }
    bool heap_sort(vector<int> &a,vector<int> &b)
    {
        build_heap(a);//构建大顶堆
        bool flag=false;
        for(int i=a.size()-1;i>=1;i--)
        {
            swap(a,0,i);
            adjust_heap(a,0,i);//一定注意这里是堆的大小是变小的,第三项一定要填i
            if(flag)
            {
                return true;
            }
            if(a==b)
            {
                flag=true;
            }
        }
        return false;
    }
    int main()
    {
        int n;
        int t;
        vector<int> initial;
        vector<int> initial2;
        vector<int> sorting;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&t);
            initial.push_back(t);
            initial2.push_back(t);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d",&t);
            sorting.push_back(t);
        }
        if(insertion_sort(initial,sorting))
        {
            printf("Insertion Sort
    ");
            for(int i=0;i<n;i++)
                if(i==0)
                {
                    printf("%d",initial[i]);
                }
                else
                {
                    printf(" %d",initial[i]);
                }
        }
        else if(heap_sort(initial2,sorting))
        {
            printf("Heap Sort
    ");
            for(int i=0;i<n;i++)
            {
                if(i==0)
                {
                    printf("%d",initial2[i]);
                }
                else
                {
                    printf(" %d",initial2[i]);
                }
            }
        }
    
        return 0;
    }
    

      



  • 相关阅读:
    fmt:formatNumber use locale display negative currency in -$xxx.xx format in JSTL
    Order By 问题集合
    Order By 问题集合
    mybatis异常invalid comparison: java.util.Date and java.lang.String
    mybatis异常invalid comparison: java.util.Date and java.lang.String
    requestLayout, invalidate和postInvalidate的异同
    如何关闭打开了多个activity的activity
    关于handler的再次讨论
    Android中pull解析XML文件的简单使用
    使用广播退出打开了多个activity的程序
  • 原文地址:https://www.cnblogs.com/SK1997/p/8540574.html
Copyright © 2020-2023  润新知