PAT 1098
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的更多相关文章
- PAT 1098. Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 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 ...
- pat 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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——PAT甲级真题
1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...
- PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- 【PAT甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
随机推荐
- PHP - Filters
Retrieve the administrator password of this application. 对文件包含的介绍------------第一个链接需要谷歌 https://mediu ...
- [Algorithm] 171. Excel Sheet Column Number
Given a column title as appear in an Excel sheet, return its corresponding column number. For exampl ...
- 【转载】windbg 常用命令详解
windbg 常用命令详解 https://blog.csdn.net/chenyujing1234/article/details/7743460 vertarget 显示当前进程的大致信息 lmv ...
- cogs 998. [東方S2] 帕秋莉·诺蕾姬
二次联通门 : cogs 998. [東方S2] 帕秋莉·诺蕾姬 交上去后发现自己没上榜 就想着加点黑科技 把循环展开一下 结果WA了.. 万恶的姆Q /* cogs 998. [東方S2] 帕秋莉· ...
- GoCN每日新闻(2019-11-07)
GoCN每日新闻(2019-11-07) GoCN每日新闻(2019-11-07) 1. [译] 排序运行时间能否做到 O(n)?让 Go 语言来告诉你 https://mp.weixin.qq.co ...
- [后渗透]Windows server 80端口复用后门
0x01 简介 该后门的基本原理是使用Windows 的远程管理管理服务WinRM,组合HTTP.sys驱动自带的端口复用功能,一起实现正向的端口复用后门. 具体细节信息请参考:https://pap ...
- 数据结构之LinkList
1.结构: 2.Link代码: public class Link { public int iData; public double dData; public Link next; public ...
- 【MonkeyRunner】[技术博客]用python编写脚本查看设备信息
[MonkeyRunner]用python编写脚本查看设备信息 原以为是个简单的操作,在实践的时候发现了一些问题. python脚本 test.py: from com.android.monkeyr ...
- 11 JavaScript Utility Libraries you Should Know in 2019
11 Useful Javascript utility libraries to speed your development.
- rtsp 客户端请求视频的时候支持输入用户名和密码的格式
rtsp://[<username>[:<password>]@]<server-address-or-name>[:<port>][/<path ...