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 ...
随机推荐
- 洛谷——P2925 [USACO08DEC]干草出售Hay For Sale
https://www.luogu.org/problem/show?pid=2925 题目描述 Farmer John suffered a terrible loss when giant Aus ...
- Android Notification状态栏通知
没有添加额外的震动及声音效果,这里直接实现了通知的功能,看效果吧: MainActivity.java package com.example.notification; import android ...
- 技术总结--android篇(四)--工具类总结
StringUtil(视个人须要进行加入) public class StringUtil { public static boolean isMail(String string) { if (nu ...
- TTS-零基础入门之停止列表中单条语音播报
做了一个语音 循环播报列表信息.当我新删除了一天列表之后,发现它仅仅有在下一轮播报中才会取消.这明显是不合理的. 一開始的代码是这样写的. <span style="font-fami ...
- luogu1220 关路灯
题目大意 路面上有一些开着的灯,每个灯有功率和它的位置,人在第c个灯处,行走速度1m/s.问怎样关灯能使耗能最小,输出这个耗能. 思路 #include <cstdio> #include ...
- dynamic关键字的使用
https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConvert.htm 在使用DeserializeObject函数进行 ...
- kafka备份机制——zk选举leader,leader在broker里负责备份
Kafka架构 如上图所示,一个典型的kafka集群中包含若干producer(可以是web前端产生的page view,或者是服务器日志,系统CPU.memory等),若干broker(Kafka支 ...
- yii widget使用的3个用法
yii视图中使用的widget方式总结:常用的有3种方式:一.显示详细信息: $this->widget('zii.widgets.CDetailView', array( 'data' =&g ...
- Winform 异步调用
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- RabbitMQ 官方NET教程(二)【工作队列】
这篇中我们将会创建一个工作队列用来在工作者(consumer)间分发耗时任务. 工作队列的主要任务是:避免立刻执行资源密集型任务和避免必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送 ...