1098. Insertion or Heap Sort (25) heapsort

Time limit memory limit 1000 ms 65536 KB limit a code length determination program 100 KB Standard (from  small )

Title Description

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?

Enter a description:

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

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.

Input example:

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

Output example:

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

analysis:

Title effect: n and n gives the number of sequences a and b, a is the original sequence, wherein step b is a sort of a Q b is through the heap sort or insertion sort, and outputs it to the next -

Reference: 1098. Insertion or the Sort Heap (25) - PAT Grade Exams (heap sort)

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll long long
const int inf=0x3f3f3f3f;
using namespace std;
int n;
int Sp[106],Sort1[106],Sort2[106];
int f=0;
void BFS(int x,int N)///一趟排序
{
    if(2*x>N) return ;
    else if(2*x==N){///不向下递归
        if(Sort1[x]<Sort1[2*x]) {
            swap(Sort1[x],Sort1[2*x]);
            f=1;
        }
    }
    else{
        f=1;
        if(Sort1[2*x]>Sort1[x] && Sort1[2*x]>Sort1[2*x+1]){
            swap(Sort1[2*x],Sort1[x]);

            BFS(2*x,N);
        }else if(Sort1[2*x+1]>Sort1[x] && Sort1[2*x+1]>Sort1[2*x]){
            swap(Sort1[2*x+1],Sort1[x]);
            BFS(2*x+1,N);
        }
    }
}

void Sort_Heap()///模拟堆排序
{
    for(int i=1;i<=n;i++) Sort2[i]=Sort1[i];
    for(int i=1;i<=n;i++) Sort1[i]=Sp[i];
    for(int i=n;i>=1;i--){///n次
        for(int j=i;j>=1;j--){
            BFS(j,i);
        }

        int ff=0;
        for(int j=i+1;j<=n;j++){
            if(Sort1[j]!=Sort2[j]){
                ff=1;
                break;
            }
        }
        if(ff) break;

        swap(Sort1[1],Sort1[i]);
    }
    cout<<Sort1[1];
    for(int ii=2;ii<=n;ii++) cout<<" "<<Sort1[ii];
    cout<<endl;
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>Sp[i];
    for(int i=1;i<=n;i++) cin>>Sort1[i];
    int i;
    for(i=2;i<=n;i++){
        if(Sort1[i]>Sort1[i-1]) continue;
        break;
    }
    int f=0,t=i;
    for(;i<=n;i++){
        if(Sort1[i]!=Sp[i]){
            f=1;    break;
        }
    }
    if(f==0){
        cout<<"Insertion Sort"<<endl;
        sort(Sort1+1,Sort1+1+t);///µÚt¸ö²åÈë
        cout<<Sort1[1];
        for(int i=2;i<=n;i++) cout<<" "<<Sort1[i];
    }else{
        cout<<"Heap Sort"<<endl;
        Sort_Heap();
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/liuyongliu/p/11237715.html