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 list, and inserts it there. It repeats until no input elements remain.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in the first line either "Insertion Sort" or "Heap Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resulting sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

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
//直接插入排序和堆排序
#include <bits/stdc++.h>
using namespace std;
#define N 120
int n,a[N],b[N],heap[N];
int flag = ;
bool same(int *a,int *b){
for(int i=;i<=n;i++) {
if(a[i]!=b[i]) return false;
}
return true;
}
void insert_sort(int *a){
for(int i =;i<=n;i++){//2不是1,因为堆排序输入的可能也是第一次排好的
sort(a+,a+i+);
if(flag!=) break;
if(same(a,b)) flag =;
}
}
void downjust(int l,int h){
int i =l;
int j =*i;
while(j<=h){
if(j+<=h&&heap[j]<heap[j+]){
j=j+;
}
if(heap[j]>heap[i]){
swap(heap[i],heap[j]);
i=j;
j=*i;
}
else
break;
}
}
void create(int *a){
for(int i =n/;i>=;i--){
downjust(i,n);
}
}
void heap_sort(int *a){
for(int i=n;i>;i--){
swap(heap[i],heap[]);
downjust(,i-);
if(flag!=) break;
if(same(heap,b)) flag =;
}
}
int main(){
scanf("%d",&n);
for(int i =;i<=n;i++)
{
scanf("%d",&a[i]);
heap[i] = a[i] ;
}
for(int i=;i<=n;i++){
scanf("%d",&b[i]);
}
insert_sort(a);
create(heap);
heap_sort(heap);//heap
if(flag==){
printf("Insertion Sort\n");
for(int i =;i<=n;i++) {
printf("%d%c",a[i],i==n?'\n':' ');
}
}
else if(flag ==){
printf("Heap Sort\n");
for(int i =;i<=n;i++) {
printf("%d%c",heap[i],i==n?'\n':' ');
}
}
return ;
}

PAT 1098的更多相关文章

  1. PAT 1098. Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  2. PAT甲级1098. Insertion or Heap Sort

    PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...

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

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...

  4. pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

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

    http://www.patest.cn/contests/pat-a-practise/1098 According to Wikipedia: Insertion sort iterates, c ...

  6. 1098 Insertion or Heap Sort——PAT甲级真题

    1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...

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

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

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

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

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

    题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...

随机推荐

  1. matlab基础向1-6:基础语法

    1.软件中如何运行代码? 命令行直接写代码,回车执行,也可以在文件里编写代码,比如有文件hello.m,点击“Run”直接运行或者在命令行窗口里输入“hello+回车”运行. 2.清空命令行 clc+ ...

  2. 分布式任务平台XXL_JOB

    目录 1.源码下载地址 2.文档地址 3.源码结构 4.初始化数据库 5.配置调度中心 ①.修改调度中心配置文件 ②.部署调度中心 ③.访问调度中心管理界面 6.创建执行器项目 ①.添加maven依赖 ...

  3. File upload - MIME type

    Your goal is to hack this photo galery by uploading PHP code.Retrieve the validation password in the ...

  4. kuma 学习四 策略

    通过策略我们可以构建灵活的service mesh 应用策略 我们可以通过kumactl 以及kubectl 应用策略 kumactl 格式 echo " type: .. spec: .. ...

  5. ERA-Interim数据学习

    1.气象再分析数据有很多种,看文献里用到的主要有这几种 ECWRF——ERA-Interim,分辨率0.125°,欧洲的 MERRA-2,分辨率0.625°*0.5°,NASA的 GEOS-5FP,分 ...

  6. YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe

    test.py import os import sys sys.path.append(])+'/lib/lib3.7') import yaml with open("default.y ...

  7. R语言中的管道操作符 %>% %T>% %$% %<>%

    magrittr 包的官网 https://magrittr.tidyverse.org/ magrittr 包的 github 主页 https://github.com/tidyverse/mag ...

  8. <每日 1 OJ> -Table

    上图是一个Mysql查询结果图,我们看到这个表格非常漂亮,只需要使用”+”和”-”两个符号就可以打印,现在你的任务是打印一个n×m的表格我们定义单位长度(水平方向有三个”-”,竖直方向有一个”| ”, ...

  9. [Beta]Scrum Meeting#4

    github 本次会议项目由PM召开,时间为5月9日晚上10点30分 时长15分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写博客整理文档 撰写博客整理文档 swoip 改进界面 改进界面 bh ...

  10. Unity2D游戏开发之保卫萝卜

    保卫萝卜是2D塔防游戏里边的一个经典案例,这次去开发这个游戏,我们会尽力去实现和原版一样的功能,做好我们可以处理好的每一个游戏细节(比如塔攻击的集火目标优先攻击,与自动搜索,格子的三种处理逻辑,UI的 ...