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 ...
随机推荐
- FOR ALL ENTRIES的使用
使用FOR ALL ENTRIES时注意: 1.一定要确定要有是否为空的判断 2.一定要注明两个表之间数据的关系 eg: IF GT_TJ30T[] IS NOT INITIAL. SELE ...
- php中时间转换函数
date("Y-m-d H:i",$unixtime) 1.php中获得今天零点的时间戳 要获得零点的unix时间戳,可以使用 $todaytime=strtotime(“tod ...
- node.js中事件触发器events的使用
node.js是基于事件驱动的,通过events,我们可以方便的创建事件,并通过触发事件来调用我们自定义的监听函数. 所有能触发事件的对象都应该是 EventEmitter 类的实例,一般我们自定义一 ...
- 比特币测试网络搭建以及RPC服务开启-配置注意事项
.bitcoin QA Test环境 启动指定参数: "C:\Program Files (x86)\Bitcoin\bitcoin-qt.exe" -testnet -serve ...
- @__CheckForDebuggerJustMyCode@4
最近在编译一个项目MiniFSWatcher,其中有一部分程序需要安装驱动minispy,编译过程中报错 严重性 代码 说明 项目 文件 行 禁止显示状态 错误 LNK2019 无法解析的外部符号 @ ...
- Fiddler抓包使用教程-安装配置
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/72876628 本文出自[赵彦军的博客] Fiddler是什么? Fiddler是一 ...
- Vim完全教程
一.简介 世界上只有三种编辑器,EMACS.VIM和其它. 我们所处的时代是非常幸运的,有越来越多的编辑器,相对于古老的VIM和EMACS,它们被称为现代编辑器.我们来看看这两个古董有多大年纪了: ...
- 2Y - sort
给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-5000 ...
- PAT 1028 人口普查(20)(STL-set+思路+测试点分析)
1028 人口普查(20)(20 分) 某城镇进行人口普查,得到了全体居民的生日.现请你写个程序,找出镇上最年长和最年轻的人. 这里确保每个输入的日期都是合法的,但不一定是合理的--假设已知镇上没有超 ...
- 向kafka批量发送已存在的txt文件
需要利用kafka发送txt或log文件,有两种方式:1.自己写程序:2.利用kafka自带脚本. 自己写程序: import org.apache.kafka.clients.producer.Ka ...