【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::sync_with_stdio(false);
 8     cin.tie(NULL);
 9     cout.tie(NULL);
10     int n;
11     cin>>n;
12     for(int i=1;i<=n;++i)
13         cin>>a[i];
14     for(int i=1;i<=n;++i)
15         cin>>b[i];
16     int pos=0;
17     for(int i=1;i<n;++i)
18         if(b[i]>b[i+1]){
19             pos=i;
20             break;
21         }
22     int pos2=0;
23     for(int i=pos+1;i<=n;++i)
24         if(a[i]!=b[i]){
25             pos2=i;
26             break;
27         }
28     if(!pos2){
29         cout<<"Insertion Sort\n";
30         int flag=0,mn=b[pos+1];
31         int ppos=0;
32         for(int i=1;i<=n;++i)
33             if(b[i]>mn){
34                 ppos=i;
35                 break;
36             }
37         for(int i=pos+1;i>ppos;--i)
38             b[i]=b[i-1];
39         b[ppos]=mn;
40         for(int i=1;i<=n;++i){
41             cout<<b[i];
42             if(i<n)
43                 cout<<" ";
44         }
45     }
46     else{
47         cout<<"Heap Sort\n";
48         int ppos=0;
49         sort(a+1,a+1+n);
50         for(int i=n;i;--i)
51             if(b[i]!=a[i]){
52                 ppos=i;
53                 break;
54             }
55         int mx=0,flag=0;
56         for(int i=1;i<=ppos;++i)
57             if(b[i]>mx){
58                 mx=b[i];
59                 flag=i;
60             }
61         swap(b[flag],b[ppos]);
62         int s=1;
63         while(s<ppos){
64             if(b[s*2]>b[s]&&b[s*2+1]>b[s]&&ppos>s*2+1){
65                 if(b[s*2]>b[s*2+1]){
66                     swap(b[s],b[s*2]);
67                     s=s*2;
68                 }
69                 else{
70                     swap(b[s],b[s*2+1]);
71                     s=s*2+1;
72                 }
73             }
74             else if(b[s*2+1]>b[s]&&ppos>s*2+1){
75                 swap(b[s],b[s*2+1]);
76                 s=s*2+1;
77             }
78             else if(b[s*2]>b[s]&&ppos>s*2){
79                 swap(b[s],b[s*2]);
80                 s=s*2;
81             }
82             if(s*2>=ppos)
83                 break;
84         }
85         for(int i=1;i<=n;++i){
86             cout<<b[i];
87             if(i<n)
88                 cout<<" ";
89         }
90     }
91     return 0;
92 }

原文地址:https://www.cnblogs.com/ldudxy/p/11964469.html

时间: 2024-10-04 04:34:54

【PAT甲级】1098 Insertion or Heap Sort (25 分)的相关文章

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

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

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

PTA 09-排序3 Insertion or Heap Sort (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/676 5-14 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 s

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)-(插入排序和堆排序)

题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是,后面是从小到大排列的最大的几个数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

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 堆——A1098.Insertion or Heap Sort(25)(内涵堆的详细创建过程)

堆的详细创建过程:参考:https://www.jianshu.com/p/21bef3fc3030 明白堆的详细创建过程的前提是要理解Shift Down. 但是这明显不符合最大堆的定义,所以我们需要让该完全二叉树转换成最大堆!怎么转换成一个最大堆呢?   最大堆有一个特点就是其各个子树都是一个最大堆,那么我们就可以从把最小子树转换成一个最大堆,然后依次转换它的父节点对应的子树,直到最后的根节点所在的整个完全二叉树变成最大堆.那么从哪一个子树开始调整? 我们从该完全二叉树中的最后一个非叶子节点