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 ...
随机推荐
- HDU6006:Engineer Assignment(状压DP)
传送门 题意 给出n个工程,m个工程师,每个工程和工程师需要/拥有若干个技能,询问能够完成的最大工程个数,每个工程师用一次 分析 dp[i][j]表示前i个工程用的工程师集合为j的最大工程个数,那么有 ...
- HDU2844【背包问题(二进制优化)】
题意: n,m 给出A1,A2,A3...AN;C1,C2,C3...CN; 给出硬币的价值和个数,问在1-M中间能构造出多少个组合 思路: n种物品的价值,n种物品的个数: 一种物品能组成多种物品的 ...
- 51nod 1031+斐波那契和杨辉三角的一些基础知识
直接斐波那契... #include<stdio.h> #include<queue> #include<string.h> #include<iostrea ...
- luogu P3600 随机数生成器【dp】
把期望改成方案数最后除一下,设h[i]为最大值恰好是i的方案数,那么要求的就是Σh[i]*i 首先包含其他区间的区间是没有意义的,用单调栈去掉 然后恰好不好求,就改成h[i]表示最大值最大是i的方案数 ...
- 关于如何隐藏UITabbar的问题
关于如何隐藏UITabbar的问题,曾经困扰过很多人. 1,设为Hidden, 这种方法虽然将TabBar隐藏掉,但是下面是一片空白,没有起到隐藏的实际功效 2,设置tabbar.frame = CG ...
- 3.Python自我修炼(升仙中....整数,布尔值,字符串,for循环)
python学习(整数,布尔值,字符串,for循环) 1.整数 在python3中所有的整数都是int类型. 但在python2中如果数据量比较大. 会使用long类型.但是在python3中不存 ...
- 前端开发 - Emmet使用手册
Emmet (前身为 Zen Coding) 是一个能大幅度提高前端开发效率的一个工具: 基本上,大多数的文本编辑器都会允许你存储和重用一些代码块,我们称之为"片段".虽然片段能很 ...
- [题解](组合数/二位前缀和)luogu_P2822组合数问题
首先要知道C(n,m)=C(n-1,m)+C(n-1,m-1),这样显然是一个杨辉三角,这样大部分的问题就解决了, 那么判能否整除只需要杨辉三角对k取模即可, 而对于多组数据的k都是一样的,所以用前缀 ...
- Crusher Django 学习笔记3 学习使用模板系统
http://crusher-milling.blogspot.com/2013/09/crusher-django-tutorial3-using-template.html 顺便学习一下 goag ...
- oop典型应用,代码。
遍历获得一个实体类的所有属性名,以及该类的所有属性的值.//先定义一个类: public class User{ public string name { get; set; } public str ...