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
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
int A[];
int B[];
int L;
void Swap(int *C,int i, int j)
{
int temp = C[i];
C[i] = C[j];
C[j] = temp;
}
void Print(int N)
{
int i;
for (i = ; i < N - ; i++)
printf("%d ", B[i]);
printf("%d", B[i]);
} int Charge(int N)
{
int i;
if (B[] > B[] && B[] > B[])
return ;
else
return ;
} void Insert_Once(int i, int N)
{
int j;
int Temp = B[i];
for (j = i; j > && B[j - ] > Temp; j--)
B[j] = B[j - ];
B[j] = Temp;
} void PrecDown(int i,int N)
{
int Parent, Child;
int Tmp = A[i];
for (Parent = i; Parent * + <= N; Parent = Child)
{
Child = Parent * + ;
if (Child != N && A[Child] < A[Child + ])
Child++;
if (Tmp >= A[Child])break;
else
A[Parent] = A[Child];
}
A[Parent] = Tmp;
}
void BuildMaxHeap(int N)
{
for (int i = (N - ) / ; i >= ; i--)
PrecDown(i, N - );
}
void Heap_Sort(int N)
{
for (int i = N - ; i >= ; i--)
{
Swap(A, , i);
PrecDown(, i-);
}
}
int FindI(int N)
{
int i;
for (i = N - ; i >= && A[i] == B[i]; i--);
return i;
}
void PrecDown_Once(int i)
{
Swap(B,, i);
int Parent, Child;
int tmp;
tmp = B[];
for (Parent = ; (Parent * ) + <= i-; Parent = Child)
{
Child = (Parent * ) + ;
if (Child != i-&& B[Child]<B[Child + ])
Child++;
if (tmp >= B[Child])break;
else
B[Parent] = B[Child];
}
B[Parent] = tmp;
}
void Heap_Once(int N)
{
BuildMaxHeap(N);
Heap_Sort(N);
int i = FindI(N);
PrecDown_Once(i);
}
int main()
{
int N;
int Flag = ;
int OrderPosition = ;
scanf("%d", &N);
for (int i = ; i < N; i++)
scanf("%d", &A[i]);
for (int i = ; i < N; i++)
scanf("%d", &B[i]);
if (Flag = Charge(N))
printf("Heap Sort\n");
else
printf("Insertion Sort\n");
for (int i = ; i < N - ; i++)
if (B[i] <= B[i + ]) //这里必须是大于等于
OrderPosition = i + ;
else
break;
if (Flag)
Heap_Once(N);
else
Insert_Once(OrderPosition + , N);
Print(N);
return ;
}
1098 Insertion or Heap Sort (25分)的更多相关文章
- 【PAT甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
- pat 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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分) Accor ...
- 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 ...
- 1098. Insertion or Heap Sort (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- 09-排序3 Insertion or Heap Sort (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
随机推荐
- Flink系列之状态及检查点
Flink不同于其他实时计算的框架之处是它可以提供针对不同的状态进行编程和计算.本篇文章的主要思路如下,大家可以选择性阅读. 1. Flink的状态分类及不同点. 2. Flink针对不同的状态进行编 ...
- Spark RDD Tutorial
Spark RDD教程 这个教程将会帮助你理解和使用Apache Spark RDD.所有的在这个教程中使用的RDD例子将会提供在github上,供大家快速的浏览. 什么是RDD(Rssilient ...
- R中的Regex
Description grep.grepl.regexpr.gregexpr和regexec在字符向量的每个元素中搜索与参数模式匹配的参数:它们在结果的格式和详细程度上有所不同. sub和gsub分 ...
- codecs打开不同步给编码的文件
实例: with codecs.open(file=源文件,mode='命令',encoding='编(解)码方式') as 命名:
- 原来rollup这么简单之 rollup.generate + rollup.write篇
大家好,我是小雨小雨,致力于分享有趣的.实用的技术文章. 内容分为翻译和原创,如果有问题,欢迎随时评论或私信,希望和大家一起进步. 分享不易,希望能够得到大家的支持和关注. 计划 rollup系列打算 ...
- 4L-线性表之数组
关注公众号 MageByte,设置星标点「在看」是我们创造好文的动力.后台回复 "加群" 进入技术交流群获更多技术成长. 数组对于每一门编程语言来说都是重要的数据结构之一,当然不同 ...
- Mol Cell Proteomics. | Integration and analysis of CPTAC proteomics data in the context of cancer genomics in the cBioPortal (解读人:徐洪凯)
文献名:Integration and analysis of CPTAC proteomics data in the context of cancer genomics in the cBioP ...
- 《Explaining and harnessing adversarial examples》 论文学习报告
<Explaining and harnessing adversarial examples> 论文学习报告 组员:裴建新 赖妍菱 周子玉 2020-03-27 1 背景 Sz ...
- 前端CSS学习笔记
一 CSS介绍 层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(超文本标记语言)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言.CSS不仅可以 ...
- 洛谷 P5639 【CSGRound2】守序者的尊严 题解
原题链接 简要题意: 从 \(1\) 号位开始走,可以连续走过一段连续的 \(0\) ,每走一次,所有位置取反. (即 \(0 \gets 1\),\(1 \gets 0\)). 算法一 模拟暴力即可 ...