1098 Insertion or Heap Sort
1098 Insertion or Heap Sort (25 分)
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 Nnumbers. 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.
* 注意标记行
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = ; int n;
int a[maxn];
int b[maxn]; void InsertSort(int v){
int tmp=b[v], i;
for(i=v-;i>=;i--){
if(b[i]>tmp){
b[i+]=b[i];
}else{
break;
}
}
b[i+] = tmp; for(int i=;i<n;i++){
if(i!=) printf(" ");
printf("%d",b[i]);
}
} void HeapSort(int v){ // MAX HEAP
int tmp=b[];
b[]=b[v];
b[v]=tmp;
tmp = b[];
int i=, child;
for(;(i+)*-<=v-; i=child){
child = (i+)*-;
if(b[child+]>b[child] && child+<=v-){
child++;
}
if(tmp<b[child]){
b[i]=b[child];
}else{
break;
}
}
b[i] = tmp;
for(int i=;i<n;i++){
if(i!=) printf(" ");
printf("%d",b[i]);
}
} void Judge(){
int i,ptr;;
for(i=;i<n&&b[i-]<=b[i];i++); // !!!注意这里
ptr=i;
for(; i<n&&a[i]==b[i];i++);
if(i==n){
printf("Insertion Sort\n");
InsertSort(ptr);
}else{
sort(a, a+n);
for(i=n-;i>=&&a[i]==b[i];i--);
printf("Heap Sort\n");
HeapSort(i);
}
} int main(){
cin>>n;
for(int i=; i<n; i++){
scanf("%d",&a[i]);
}
for(int i=; i<n; i++){
scanf("%d",&b[i]);
}
Judge();
}
1098 Insertion or Heap Sort的更多相关文章
- 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 ...
- 1098 Insertion or Heap Sort——PAT甲级真题
1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...
- 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 (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- 1098 Insertion or Heap Sort (25 分)(堆)
这里的第二序列相当于是排序还没拍好的序列 对于第二个样例的第二个序列其实已经是大顶堆了 然后才进行的堆排序 知道这个就好做了 #include<bits/stdc++.h> using n ...
随机推荐
- Openflow的架构+源码剖析 转载
Openvswitch的架构网上有如下的图表示: Openvswitch原理与代码分析(1):总体架构 Openvswitch原理与代码分析(2): ovs-vswitchd的启动 Openvswit ...
- iOS.Crash.Case-[__NSArrayM objectForKeyedSubscript:]
1. [__NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance - source code and s ...
- git分支切换时的时间戳问题
1.为什么git仓库没有保留文件修改时的时间戳? 摘自:https://git.wiki.kernel.org/index.php/Git_FAQ#Why_isn.27t_Git_preservin ...
- eclipse中查找某一个字符串
想要完全匹配查找一个字符串,如iCard,而iCardSys或iiCard这种都不行,可以用正则表达式来查找,如下: 参考链接:http://blog.csdn.net/u014656992/arti ...
- 洛谷1288 取数游戏II
原题链接 因为保证有\(0\)权边,所以整个游戏实际上就是两条链. 很容易发现当先手距离\(0\)权边有奇数条边,那么必胜. 策略为:每次都将边上权值取光,逼迫后手向\(0\)权边靠拢.若此时后手不取 ...
- CH6901 骑士放置
原题链接 和棋盘覆盖(题解)差不多.. 同样对格子染色,显然日字的对角格子是不同色,直接在对应节点连边,然后就是二分图最大独立集问题. #include<cstdio> #include& ...
- 显示实现接口的好处c#比java好的地方
所谓Go语言式的接口,就是不用显示声明类型T实现了接口I,只要类型T的公开方法完全满足接口I的要求,就可以把类型T的对象用在需要接口I的地方.这种做法的学名叫做Structural Typing,有人 ...
- MySQL中的联结表
使用联结能够实现用一条SELECT语句检索出存储在多个表中的数据.联结是一种机制,用来在一条SELECT语句中关联表,不是物理实体,其在实际的数据库表中并不存在,DBMS会根据需要建立联结,且会在查询 ...
- Spring 系列教程之容器的功能
Spring 系列教程之容器的功能 经过前面几章的分析,相信大家已经对 Spring 中的容器功能有了简单的了解,在前面的章节中我们一直以 BeanFacotry 接口以及它的默认实现类 XmlBea ...
- Mac虚拟机
2018-06-21 需要的Mac静像是ios或是cdr的,如果是dmg,可以参考这个转换http://blog.sina.com.cn/s/blog_60b45f230101kkbf.html 或 ...