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 ...
随机推荐
- OpenCV使用说明
我在这边大概说一下OpenCV的使用,具体环境配置参考下面我给出的两个链接. 1. 对于目前OpenCV来说,安装变的简单了很多,现在官方已经给出了预编译文件,不要重新编译.具体使用可以参考http: ...
- 启动spring boot项目
启动spring boot项目 pom.xml如下: <?xml version="1.0" encoding="UTF-8"?> <proj ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
感觉自己做有关区间的题目方面的思维异常的差...有时简单题都搞半天还完全没思路,,然后别人提示下立马就明白了...=_= 题意:给一个含有n个元素的数组和k,问存在多少个区间的和值为k的次方数. 题解 ...
- java的死锁学习
学习java的死锁写的代码 也是看书上的然后自己敲了一个 <span style="font-size:18px;">package synchronization.j ...
- 使用Django框架实现游戏站点搭建
完整project链接点击打开链接 上一篇中我们使用了Javascript和Html5实现了弹球游戏.而在本文中我们希望以其为基础实现游戏站点,可以实现用户的注冊登录.游戏成绩记录,排名显示.微博分享 ...
- 《Java编程思想》笔记
第十章 (1)当生成一个内部类的对象时.此对象 与制造他的外围对象之间就有了一种联系,所以它能訪问其外围对象的全部成员,而不须要不论什么特殊条件. 此外,内部类还拥有其它外围类的全部元素的訪问权. ( ...
- opencv源代码分析之二:cvhaartraining.cpp
我使用的是opencv2.4.9.安装后.我的cvboost..cpp文件的路径是........\opencv\sources\apps\haartraining\cvhaartraining.cp ...
- WCF学习笔记——契约不能少了set
我定义的WCF契约里,有一个类,里面的属性,有一个因为只读,所以只写了个get.结果客户端就报错. [DataContract] public class UserItem { public User ...
- bzoj 1503郁闷的出纳员(splay)
1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 11759 Solved: 4163[Submit][Stat ...
- Speex回声消除原理深度解析
这里假设读者具有自适应滤波器的基础知识.Speex的AEC是以NLMS为基础,用MDF频域实现,最终推导出最优步长估计:残余回声与误差之比.最优步长等于残余回声方差与误差信号方差之比,这个结论可以记下 ...