PAT_A1098#Insertion or Heap Sort
Source:
Description:
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 (≤100). 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
keys:
Attention:
- 初始状态与中间状态相同时为插入排序;
- 初始状态与中间状态相同时,下一个前插的元素应该是A【k】< A【k-1】的元素,而非直接把第二个元素前插(初始状态 != 中间状态);
Code:
/*
Data 2019-06-21 17:11:02
Problem: PAT_A1098#Insertion or Heap Sort
AC: 41:05 题目大意:
给定初始序列和经过若干阶段排序的中间序列,判断是插入排序还是堆排序
设最终序列递增有序
输出:
插入排序或堆排序,并给出下一阶段的排序结果 基本思路:
根据插入排序的特点来判断,
前半部分递增,后半部分与初始序列相同,则为插入排序
注意:初始序列与中间序列相同,为插入排序
*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int M=;
int n, init[M], sorted[M]; void DownAdjust(int low, int high)
{
int i=low, j=i*;
while(j<=high)
{
if(j+<=high && sorted[j+]>sorted[j])
j++;
if(sorted[i] < sorted[j])
{
swap(sorted[i], sorted[j]);
i=j;
j=i*;
}
else
break;
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d", &n);
for(int i=; i<=n; i++)
scanf("%d", &init[i]);
for(int i=; i<=n; i++)
scanf("%d", &sorted[i]);
int pos=n;
while(pos>= && init[pos]==sorted[pos])
pos--;
int pt=pos;
while(pos>= && sorted[pos-]<=sorted[pos])
pos--;
if(pos==)
{
printf("Insertion Sort\n");
if(pt==)
while(pt<=n && sorted[pt]<=sorted[pt+])
pt++;
sort(sorted+,sorted+pt+);
}
else
{
printf("Heap Sort\n");
pt=n;
while(pt>= && sorted[]<=sorted[pt])
pt--;
swap(sorted[], sorted[pt]);
DownAdjust(,pt-);
}
for(int i=; i<=n; i++)
printf("%d%c", sorted[i], i==n?'\n':' '); return ;
}
PAT_A1098#Insertion or Heap Sort的更多相关文章
- PTA Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1098 Insertion or Heap Sort
1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...
- PAT甲级1098. Insertion or Heap Sort
PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...
- PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...
- pat1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 ...
- Insertion or Heap Sort
7-14 Insertion or Heap Sort(25 分) According to Wikipedia: Insertion sort iterates, consuming one inp ...
- 1098 Insertion or Heap Sort——PAT甲级真题
1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...
随机推荐
- 机器学习1k近邻
自己一直学习计算机视觉方面的东西,现在想学习一下数据挖掘跟搜索引擎,自己基础也有点薄弱,看朱明的那本数据挖掘,只能片面的了解这个数据挖掘.不过最近有一本书 机器学习实战,于是乎通过实战的形式了解一下基 ...
- firedac数据集控件的公共祖先类——TFDAdaptedDataSet
firedac数据集控件的公共祖先类——TFDAdaptedDataSet TFDQuery = class(TFDCustomQuery)TFDCustomQuery = class(TFDRdbm ...
- MVC.Net:对MVC5部署时出现403.14错误的解决方法
当我们部署MVC5到IIS 7的时候,有时会出现403.14的错误,如下图: 对于这个错误的解决方法就是在应用程序的web.config的system.webServer节点中加入这一句: <m ...
- Maven 的dependency 的 classifier的作用
直接看一个例子,maven中要引入json包,于是使用了 <dependency> <groupId>net.sf.json-lib</groupId> <a ...
- 《python源代码剖析》笔记 Python虚拟机框架
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 1. Python虚拟机会从编译得到的PyCodeObject对象中依次读入每一条字节码指令 ...
- Codeforces Round #332 (Div. 2) B. Spongebob and Joke 模拟
B. Spongebob and Joke While Patrick was gone shopping, Spongebob decided to play a little trick ...
- Parallel in C#
https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel ...
- C# Interactive Walkthrough
C# Interactive Walkthrough
- hdu1533 费用流模板
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Java数据结构2——深入JCF
Java集合框架(JCF)参考C++的STL实现的在日常Java开发工作很常用的数据结构容器,有技术追求的人除了要会简单使用JCF之外,也要知道其底层的实现机制,知道它是如何实现的,为什么这样实现.就 ...