PAT (Advanced Level) Practice - 1098 Insertion or Heap Sort(25 分)

简介: PAT (Advanced Level) Practice - 1098 Insertion or Heap Sort(25 分)

题目链接:点击打开链接

题目大意:略。

解题思路:插入排序的特点是:b数组前面的顺序是从小到大的,后面的顺序不一定,但是一定和原序列的后面的顺序相同!所以只要遍历一下前面几位,遇到不是从小到大的时候,开始看b和a是不是对应位置的值相等,相等就说明是插入排序,否则就是堆排序啦!

  • 插入:sort() 函数
  • 堆:pop_heap() 函数

AC 代码

1.#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
int a[110], b[110];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++) scanf("%d",&a[i]);
    for(int i=0;i<n;i++) scanf("%d",&b[i]);
    int i;
    for(i=1;i<n;)
        if(b[i]>=b[i-1]) i++;
        else break;
    int p=i, isIns=1;
    for(;i<n;i++)
        if(a[i]!=b[i]){isIns=0; break;}
    if(isIns)
    {
        puts("Insertion Sort");
        sort(b,b+p+1);
    }
    else
    {
        for(i=n-1;i>0;)
            if(b[i]>=b[i-1]) i--;
            else break;
        puts("Heap Sort");
        pop_heap(b,b+i+1);
    }
    for(i=0;i<n;i++)
        printf("%d%c",b[i],i==n-1?'\n':' ');
    return 0;
}
目录
相关文章
PAT (Advanced Level) Practice - 1010 Radix(25 分)
PAT (Advanced Level) Practice - 1010 Radix(25 分)
93 0
|
存储
PAT (Advanced Level) Practice - 1051 Pop Sequence(25 分)
PAT (Advanced Level) Practice - 1051 Pop Sequence(25 分)
81 0
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
94 0
PAT (Advanced Level) Practice - 1057 Stack(30 分)
PAT (Advanced Level) Practice - 1057 Stack(30 分)
91 0
PAT (Advanced Level) Practice - 1057 Stack(30 分)
Data Structures and Algorithms (English) - 6-6 Level-order Traversal(25 分)
Data Structures and Algorithms (English) - 6-6 Level-order Traversal(25 分)
83 0
PAT (Advanced Level) Practice - 1068 Find More Coins(30 分)
PAT (Advanced Level) Practice - 1068 Find More Coins(30 分)
92 0
|
C++
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
99 0
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
87 0
|
存储 测试技术
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
88 0
PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)
PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)
100 0