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 ...
随机推荐
- objc_msgSend method_getTypeEncoding 与 @encode
struct objc_method { SEL _Nonnull method_name OBJC2_UNAVAILABLE; cha ...
- HDU 6595 Everything Is Generated In Equal Probability (期望dp,线性推导)
Everything Is Generated In Equal Probability \[ Time Limit: 1000 ms\quad Memory Limit: 131072 kB \] ...
- 大文件断点续传插件webupload插件
java两台服务器之间,大文件上传(续传),采用了Socket通信机制以及JavaIO流两个技术点,具体思路如下: 实现思路: 1.服:利用ServerSocket搭建服务器,开启相应端口,进行长连接 ...
- ABP 03 解决 编辑User报错
1.编辑用户时,报错.后面有跟解决方案. 解决方案1: 2.导致出错的原因是这样的,这里的功能是请求服务端的html页面,渲染后显示编辑页面. 关键点是默认参数那儿 路径:\aspnet-core\s ...
- luogu P1447 [NOI2010]能量采集 欧拉反演
题面 题目要我们求的东西可以化为: \[\sum_{i=1}^{n}\sum_{j=1}^{m}2*gcd(i,j)-1\] \[-nm+2\sum_{i=1}^{n}\sum_{j=1}^{m}gc ...
- nginx rewrite规则说明
格式:rewrite regex replacement [flag] * rewrite配置可以在server.location以及if配置段内生效 * regex是用于匹配URI的正则表达式,其不 ...
- 【09NOIP提高组】Hankson 的趣味题(信息学奥赛一本通 1856)(洛谷 1072)
题目描述 Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫Hankson.现在,刚刚放学回家的Hankson 正在思考一个有趣的问题.今天在课堂上,老师讲解了如何求 ...
- centOS7开启ssh免密登陆
一.登陆服务器生成ssh-key 二.把ssh-key复制到被登陆机器上 三.设置权限 root# .ssh 文件夹权限 root# .ssh/authorized_keys 文件权限 四.测试是否正 ...
- 【Gamma】Scrum Meeting 4 & 助教参会记录
目录 前言 任务分配 燃尽图 会议照片 签入记录 上周助教交流总结 技术博客 一些说明 前言 第4次会议于5月29日22:00线上交流形式召开. 交流确认了各自的任务进度,并与助教进行了沟通.时长20 ...
- 第08组 Alpha冲刺(2/4)
队名 八组评分了吗 组长博客 小李的博客 作业博客 作业链接 组员1李昕晖(组长) 过去两天完成了哪些任务 文字/口头描述 11月17日了解各个小组的进度与难以攻破的地方,与隔壁第七组组长讨论进度发展 ...