1. 程式人生 > 實用技巧 >1098. Insertion or Heap Sort (25)堆排序模擬

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

題目:1098 Insertion or Heap Sort (25分)

題意

給出n和n個數的序列a和b,a為原始序列,b為排序其中的一個步驟,問b是a經過了堆排序還是插入排序的,並且輸出它的下一步

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

解法

若是插入排序,序列前面有序,後面相同
否則是堆排序

是否考慮特殊情況:經過一趟插入排序後,序列沒有變化
比如:
8 7 1 2 3 9 5 6
1 2 3 7 8 9 5 6
這個9算不算排過了呢??題目貌似算排過了

code

/***************************
        Hello World!
***************************/
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int n;
int a[105];
int b[103];

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>b[i];

    //為什麼要先判斷左(正序)後右(相等),才能決定是否是insert
    /**
    8 7 1 2 3 9 5 6
    1 2 3 7 8 9 5 6
    這個9算不算排過了呢
    **/

/** 按下面的寫法(也就是從右向左判斷),9不算排過 **/
//    int p=n;
//    while(p>=1 && a[p]==b[p]) p--;
//    //判斷1-p是否有序
//    int index=p+1;
//    while(p>=2 && b[p-1]<=b[p]) p--;

/** 按下面的寫法(也就是從左向右判斷),9算排過 **/
    int p=2;
    while(p<=n && b[p-1]<=b[p]) p++;
    int index=p;
    while(p<=n && b[p]==a[p]) p++;

    //第一趟插入序列不變的
    if(p>n)//是插入
    {
        cout<<"Insertion Sort"<<endl;
        sort(b+1,b+index+1);
        for(int i=1;i<n;i++) cout<<b[i]<<" ";
        cout<<b[n]<<endl;
    }
    else//堆排序
    {
        cout<<"Heap Sort"<<endl;
        int fin;
        for(int i=n;i>=1;--i)
        {
            if(b[i]<=b[1]) {
                fin=i; break;
            }
        }
        
        swap(b[1],b[fin]);
        fin--;
        int p=1;
        while(p<=fin/2)
        {
            int j;
            if(2*p+1<=fin) {
                j=b[2*p]>b[2*p+1]?2*p:2*p+1;
            }
            else {
                j=2*p;
            }
            if(b[j]>b[p]) {
                swap(b[j],b[p]);
                p=j;
            }
            else break;
        }
        
        for(int i=1;i<n;i++) cout<<b[i]<<" ";
        cout<<b[n]<<endl;
    }
    return 0;
}
/***************************
        The end!
***************************/