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

  题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列。

  插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同。

  堆排序的特点就是,后面是从小到大排列的最大的几个数p~n-1,前面第一位则是p-1。

  所以只要先按照插入排序的特点来判断是否为插入排序,接下来再进行操作就可以了,这里要手动写下最大堆的更新操作。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
/*

之前一直WA的原因是,用for循环写寻找idx一开始就写错了。。。
找了整个序列的<,应该是找反例>从而跳出for循环,或者直接加到条件里。
比如:
一开始这么写,
for(int i=0;i<n;i++){
    if(num2[i]<=num2[i+1])
        idx++;
}
正确应该是:
for(idx=0;idx<n-1 && num2[idx]<=num2[idx+1];idx++);
晕死。。。脑子糊涂了
*/
using namespace std;
const int maxn=105;
int heap[maxn];
int heap_size=0;

void swaps(int &a,int &b){
    int tmp;
    tmp=a;
    a=b;
    b=tmp;
}
void heap_update(int i){
    int bigger=i;
    int l=(i<<1)+1;
    int r=(i<<1)+2;
    if(l<heap_size && heap[l]>heap[i])
        bigger=l;
    if(r<heap_size && heap[r]>heap[bigger])
        bigger=r;
    if(bigger!=i){
        swaps(heap[i],heap[bigger]);
        heap_update(bigger);
    }
}
void heap_pop(){
    if(heap_size>=1){
        swaps(heap[0],heap[heap_size-1]);  //take the max to the rear.
        heap_size--;
        heap_update(0);
    }
}

int main()
{
    int n;
    int num[maxn],num2[maxn];

    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&num[i]);
    }
    for(int i=0;i<n;i++){
        scanf("%d",&num2[i]);
    }
    int idx;
    for(idx=0;idx<n-1 && num2[idx]<=num2[idx+1];idx++);
    int p=idx+1;
    for(;p<n && num[p]==num2[p];p++);
    if(p==n){
        sort(num2,num2+idx+2); //相当于将num[idx+1]插入到前面排序
        printf("Insertion Sort\n");
        for(int i=0;i<n-1;i++)
            printf("%d ",num2[i]);
        printf("%d",num2[n-1]);
    }
    else{
        int sortedsize=0;
        int tmpnum[maxn];
        for(int i=0;i<n;i++)
            tmpnum[i]=num[i];
        sort(tmpnum,tmpnum+n);
        p=n-1;
        /*
        看了别人网上有写第三行AC的,
        但按道理来说,如果序列是6450123789,那明显第三行就不对额。
        */
        for(;p>=0 && num2[p]==tmpnum[p];p--);
        //for(;p>=1 && num2[p]>=num2[0];p--);
        //for(;p>=1 && num2[p]>=num2[p-1];p--); 个人认为应该是过不了的。。。但却AC了

        heap_size=p+1;
        for(int i=0;i<n;i++)
            heap[i]=num2[i];
        heap_pop();
        printf("Heap Sort\n");
        for(int i=0;i<n-1;i++){
            printf("%d ",heap[i]);
        }
        printf("%d",heap[n-1]);
    }
    return 0;
}

时间: 2024-08-05 06:50:41

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

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 (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

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

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甲题题解-1039. Course List for Student (25)-建立映射+vector

博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789157.html特别不喜欢那些随便转载别人的原创文章又不给出链接的所以不准偷偷复制博主的博客噢~~ 题意:给出要查询的n个学生,k个课程接下来对于k门课,给出其id和学生数量,以及对应的学生名字租后给出n个查询的学生名字,让你输出其选的课程数量和对应的课程id,id从小到大排序. 题目简单,然而建立映射的想法不错~~推荐一开始发生段错误,才发现n的范围只是

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

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