1098 Insertion or Heap Sort

题意:给出两组数,第一组数为原始序列,第二组数为经过若干次插入排序或堆排序后的序列,判定到底是属于哪一种排序的结果,然后输出以这种排序的下一次迭代后的序列。

思路:就是练习插入排序和堆排序,没什么好说的。不过有一个小细节,我被扣了2分。见代码。

代码:

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> ori,data,temp;
int n;
int flag=-1;//0:Insertion Sort; 1:Heap Sort

void downAdjust(int low,int high)
{
    int fa=low,left=2*fa;
    while(left<=high){
        if(left+1<=high && data[left+1]>data[left])
            left=left+1;
        if(data[fa]<data[left]){
            swap(data[fa],data[left]);
            fa=left;
            left=2*fa;
        }else{
            break;
        }
    }
}

void heapSort()
{
    //make heap
    for(int i=n/2;i>=1;i--)
        downAdjust(i,n);
    //sort and adjust
    bool breakHere=false;
    for(int i=n;i>1;i--){
        if(breakHere) break;
        if(data==temp){
            flag=1;
            breakHere=true;
        }
        swap(data[1],data[i]);
        downAdjust(1,i-1);
    }
}

void insertSort()
{
    bool breakHere=false;
    for(int i=2;i<=n;i++){
        //如果判断语句放在这里就错啦。放在这里比较的话,会和原始序列进行一次比较。     //而题目中说了,适合某次迭代后的序列进行比较。所以审题千万要仔细。
        //if(breakHere) break;
        //if(data==temp){
        //    flag=0;
        //    breakHere=true;
        //}

        int k=i,tmp=data[i];
        while(k>1 && data[k-1]>tmp){
            data[k]=data[k-1];
            k--;
        }
        data[k]=tmp;

        if(breakHere) break;
        if(data==temp){
            flag=0;
            breakHere=true;
        }
    }
}

int main()
{
    //freopen("pat.txt","r",stdin);
    scanf("%d",&n);
    ori.resize(n+1);
    temp.resize(n+1);
    for(int i=1;i<=n;i++) scanf("%d",&ori[i]);
    for(int i=1;i<=n;i++) scanf("%d",&temp[i]);

    data=ori;
    heapSort();
    if(flag==1){
        printf("Heap Sort\n");
        for(int i=1;i<data.size();i++){
            printf("%d",data[i]);
            if(i<data.size()-1) printf(" ");
            else printf("\n");
        }
    }else {
        data=ori;
        insertSort();
        printf("Insertion Sort\n");
        for(int i=1;i<data.size();i++){
            printf("%d",data[i]);
            if(i<data.size()-1) printf(" ");
            else printf("\n");
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/kkmjy/p/9597173.html

时间: 2024-08-28 20:48:10

1098 Insertion or Heap Sort的相关文章

1098. Insertion or Heap Sort (25)【排序】——PAT (Advanced Level) Practise

题目信息 1098. Insertion or Heap Sort (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one el

PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iterati

PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)

题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是,后面是从小到大排列的最大的几个数p~n-1,前面第一位则是p-1. 所以只要先按照插入排序的特点来判断是否为插入排序,接下来再进行操作就可以了,这里要手动写下最大堆的更新操作. 代码: #include <iostream> #include <cstdio> #include <

PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=200; int a[maxn],b[m

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

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

1098 Insertion or Heap Sort (25 分)

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 li

【PAT甲级】1098 Insertion or Heap Sort (25 分)

题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int a[107],b[107]; 5 int c[107][5]; 6 int main(){ 7 ios:

pat1098. 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. Each iteration, inse