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 ...
随机推荐
- Codechef August Challenge 2019 Chef and Gordon Ramsay
[传送门] 题目即求所有的三元组,相对大小关系同 $p_1,p_2,p_3$. 题解说都很清楚,这里写一下过程整理一下思路. 如果我们枚举中间这个元素,那么就是统计子树内外有多少个大于这个数和小于这个 ...
- fitnesse生成的FitNesseRoot路径问题
运行fitnesse命令的时候,会生成FitNesseRoot这个文件夹. 但是需要注意的是你在哪个路径下开启服务,就在当前路径下生成FitNesseRoot这个文件夹,而不是说你的fitnesse- ...
- ES6 String和Number扩展
一.String 扩展 ①传统上,JavaScript 只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6 又提供了三种新方法. includes():返回布尔值,表示是否 ...
- DIP大作业---图像分割
数字图像处理课程的大作业,要求如下: 图像分割就是把图像分成若干个特定的.具有独特性质的区域并提出感兴趣目标的技术和过程.它是由图像处理到图像分析的关键步骤.现有的图像分割方法主要分以下几类:基于阈值 ...
- pip命令提示unknow or unsupported command install解决方法
执行pip命令安装模块,提示unknow or unsupported command install 原因: 使用where pip查看, 电脑中装了loadrunner,存在多个pip,不知道使用 ...
- 获取shell脚本自身所在目录
解决了使用ln -s target linkName创造软链接无法正确取到真实脚本的问题. #!/bin/bash SOURCE="$0" while [ -h "$SO ...
- mysql 全连接和 oracle 全连接查询、区别
oracle的全连接查询可以直接用full on,但是在mysql中没有full join,mysql使用union实现全连接. oracle的全连接 select * from a full joi ...
- 编译错误: file not found with angled include use quotes instead #include <lualib.h> 和 #include "lualib.h"
http://stackoverflow.com/questions/17465902/use-of-external-c-headers-in-objective-c 问题: 7down votef ...
- Appium-Python-Windows 环境搭建
目录 1.安装JDK 2.安装Android SDK 3.安装Node.js 4.安装Appium server 5.安装Python 6.安装Appium-Python-Client 7.安装pyt ...
- Centos7 Nginx安装使用
一.Nginx简介 1.什么是nginx Nginx是一款使用C语言开发的高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.由俄罗斯的程序设计师Igor Sysoev ...