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的更多相关文章

  1. PAT甲级1098. Insertion or Heap Sort

    PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...

  2. PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...

  3. pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  4. 1098 Insertion or Heap Sort——PAT甲级真题

    1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  5. 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 ...

  6. 1098. Insertion or Heap Sort (25)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  7. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  8. PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)

    题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...

  9. 1098 Insertion or Heap Sort (25 分)(堆)

    这里的第二序列相当于是排序还没拍好的序列 对于第二个样例的第二个序列其实已经是大顶堆了 然后才进行的堆排序 知道这个就好做了 #include<bits/stdc++.h> using n ...

随机推荐

  1. solr简介、学习详细过程!(超详细~)

    solr是什么呢? 一.Solr它是一种开放源码的.基于 Lucene Java 的搜索服务器,易于加入到 Web 应用程序中. 二.Solr 提供了层面搜索(就是统计).命中醒目显示并且支持多种输出 ...

  2. IOS安卓常见问题

    一.IOS自带safari浏览器   1.safari不支持fixed+input输入框.   解决方案: http://www.haorooms.com/post/ios_fixed_input   ...

  3. java的eclipse的使用

    1下载eclipse地址:www.eclipse.org/downloads/ 解压就可安装 注意: 这可能你是没有安装java运行环境(jre或jdk) 直接www.java.com,下载就行 下一 ...

  4. mysql 单表查询

    一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数   二 ...

  5. ajax 之POST请求,参数序列化

    比如,,我们在没有使用jquery的时候,没有$.post来让我们使用,那我们像下面这样直接发送: var params1 = { username: username, passwrod: pass ...

  6. linux 发送Post请求 json格式

    curl -H "Content-type: application/json" -X POST -d '{"text":"总体来说很不错,环境挺好的 ...

  7. nginx 的 负载均衡

    一.正向代理和反向代理 1.正向代理 正向代理类似一个跳板机,代理访问外部资源. 正向代理是客户端和目标服务器之间的代理服务器(中间服务器).为了从指定的服务器取得内容,客户端向代理服务器发送一个请求 ...

  8. linux下查看内存使用情况

    基本内存术语解读 1> free -m 同样是做为缓存,buffers和cache又有啥区别呢? 于是又查了些资料,发现buffers实际应该是叫“缓冲”,其英文解释是:A buffer is ...

  9. [Chrome Headless + Python] 截长图 (Take Full-page Screenshot)

    # -*- coding: utf-8 -*- import time import os from selenium import webdriver from selenium.webdriver ...

  10. [SoapUI] 在SoapUI中通过Groovy脚本执行window命令杀掉进程

    //杀Excel进程 String line def p = "taskkill /F /IM EXCEL.exe".execute() def bri = new Buffere ...