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, 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 resuling 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 此题是2015年春季的研究生入学考试复试时的机试题,链接 http://www.patest.cn/contests/graduate-entrance-exam-2015-03-20
#include<cstdio>
#include<cstring> void headshift(int *a,int i,int len)//数组 要处理的节点下标 当前处理的堆的长度
{
if(i>=len/ || i<) return;//叶节点可看作一个满足性质的堆,所以只对非叶子结点进行处理即可
int max=i; //调整一个节点为(节点值、左孩子节点值、右孩子节点值)三者中的最大值
if(*i+<=len- && a[max]<a[*i+]) max=*i+;
if(*i+<=len- && a[max]<a[*i+]) max=*i+;
if(max!=i)
{
int temp=a[max];
a[max]=a[i],a[i]=temp;
headshift(a,max,len);//某节点与其某个孩子节点的数值交换调整后,可能会影响该孩子节点的堆的性质,所以要向下调整
}
return;
} int match(int *temp,int *a,int len)
{
for(int i=;i<len;i++)
if(temp[i]!=a[i]) return ;
return ;
} void print(int *a,int len)
{
for(int i=;i<len;i++)
{
if(i) printf(" %d",a[i]);
else printf("%d",a[i]);
}
return;
} void Insertion(int *a,int i)
{
int loc=i-,temp=a[i];
for(;loc>=;loc--) if(a[loc]<=temp) break;//寻找应该插入的位置
loc++;
if(loc!=i) for(int j=i;j>loc;j--) a[j]=a[j-];//比当前处理值大的元素相应后移
a[loc]=temp;
}
int main()
{ int num=,data[]={},sorttemp[]={},a[]={};
scanf("%d",&num);
for(int i=;i<num;i++) scanf("%d",data+i);
for(int i=;i<num;i++) scanf("%d",sorttemp+i); for(int i=;i<num;i++) a[i]=data[i];
for(int i=;i<num;i++) //i=0时,不构成插入排序的定义 因为0元素之前没有有序子序列
{
Insertion(a,i);
if(match(sorttemp,a,num))//if(match) 再处理一轮+输出+return
{
if(i<num-) i++; //i++易漏掉 one more iteration
Insertion(a,i);
printf("Insertion Sort\n");
print(a,num);
return ;
}
} for(int i=;i<num;i++) a[i]=data[i];
int len=num,temp=; for(int i=num/-;i>=;i--) //构建大根堆
headshift(a,i,len); for(int i=num-;i>=;i--) //排序
{
temp=a[],a[]=a[i],a[i]=temp;//顶尾交换 表示当前最大值已处理
len--;//堆长度-1
headshift(a,,len);//headshift新顶
if(match(sorttemp,a,num))//if(match) 再处理一轮+输出+return
{
i--; //i-- 易漏掉 one more iteration
temp=a[],a[]=a[i],a[i]=temp;
len--;
headshift(a,,len);
printf("Heap Sort\n");
print(a,num);
return ;
}
}
return ;
}
PAT (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)的更多相关文章
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- 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) 1098. Insertion or Heap Sort (25)
简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...
- PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- 【PAT甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
- 1098. Insertion or Heap Sort (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1098 Insertion or Heap Sort (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 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 ...
随机推荐
- PhpStorm之配置数据库连接
打开编辑器,找到编辑器右侧的 Database 点击 Database,点击左上角的 + ,选择Data Source ,再点击需要连接的数据库类型(因为我的数据库是MySQL,所以使用MySQL数据 ...
- 萌新java入门笔记
首先声明以下内容只是散乱笔记,如果有误还望大侠指出!不胜感激! 基本数据类型: 大体和C语言类似: boolean truth = true;//逻辑型 //文字型 char c; String st ...
- lightoj 1125【背包·从n个选m个】
题意: 给你 n 个背包,然后给你两个数,D,M,问你从n个里面挑M个出来,有多少种方法能够整除D: 思路: 试想我先不挑M个出来的话,仅仅是构造一个D的倍数,其实就是构造一个数的话, 其实就是个递推 ...
- atcoder057D(组合数模板)
题目链接:http://abc057.contest.atcoder.jp/tasks/abc057_d 题意:给出n个数,可以选择x~y个数,使其平均值最大,求其最大平均数以及选择方案数. 思路:只 ...
- bzoj 4200: [Noi2015]小园丁与老司机【dp+有上下界最小流】
洛谷上有个点死活卡不过去,不知道是哪里写丑了orz 参考:https://www.cnblogs.com/ditoly/p/BZOJ4200.html 从上往下dp,设f为不向左右走直接上去的值,g为 ...
- 洛谷P2480 [SDOI2010]古代猪文(卢卡斯定理+中国剩余定理)
传送门 好吧我数学差的好像不是一点半点…… 题目求的是$G^{\sum_{d|n}C^d_n}mod\ 999911659$ 我们可以利用费马小定理$a^{k}\equiv a^{k\ mod\ (p ...
- java小游戏——猜数字
import java.util.ArrayList; import java.util.List; import java.util.Random; public class Num01 { sta ...
- centos6.7版本下配置ssh密钥登录
需要提前说明的是我使用的系统是centos6.7的版本. 1.我使用的是Putty登录 #ssh-keygen (生成公钥和私钥的命令) 回车之后会提示密钥要存放的目录,默认的目录是当前目录下的.ss ...
- C#的特性学习
转自:https://www.cnblogs.com/rohelm/archive/2012/04/19/2456088.html 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类 ...
- node项目 Error: Cannot find module 'mongoose'
这是因为你部署的项目没有添加mongoose,使用 在自己项目的根目录下:npm install mongoose --save