1098. Insertion or Heap Sort (25)堆排序

时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自  小小)

题目描述

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?

输入描述:

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.


输出描述:

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.

输入例子:

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

输出例子:

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

分析:

题目大意:给出n和n个数的序列a和b,a为原始序列,b为排序其中的一个步骤,问b是a经过了堆排序还是插入排序的,并且输出它的下一步~

参考:1098. Insertion or Heap Sort (25)-PAT甲级真题(堆排序)

#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

猜你喜欢

转载自www.cnblogs.com/liuyongliu/p/11237715.html