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 (≤). 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 resulting 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
 //注意,这里的排序为排序迭代过程中完成的某一步而已,并不是最终结果
//我们可以直接用sort来代替排序的部分,无论是合并排序还是插入排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, a;
vector<int>nums, ans, temp;
void down(int index, int n)
{
int t = temp[index];
while ( * index + < n)
{
int child = * index + ;
if (child + < n && temp[child] < temp[child + ])
++child;
if (temp[child] > t)
{
temp[index] = temp[child];
index = child;
}
else
break;
}
temp[index] = t;
}
int main()
{
cin >> N;
for (int i = ; i < N; ++i)
{
cin >> a;
nums.push_back(a);
}
for (int i = ; i < N; ++i)
{
cin >> a;
ans.push_back(a);
}
temp = nums;
bool f = false;
for (int i = ; i < N; ++i)//进行插入排序
{
sort(temp.begin(), temp.begin() + i + );//第一趟排序应该是排序前两个数,第i趟排序分别排序前i+1个数
if (temp == ans)
{
f = true;
cout << "Insertion Sort" << endl;
sort(temp.begin(), temp.begin() + i + );//再一次迭代
break;
}
}
if (f == false)//那就是堆排序
{
cout << "Heap Sort" << endl;
temp = nums;
for (int i = N / ; i >= ; --i)//将前一半元素进行下滤
down(i, N);
for(int i=N-;i>;--i)//N-1趟排序,每次排序得到一个第N-i大的值放到相应位置
{
swap(temp[i], temp[]);
down(, i);
if (temp == ans)
{
swap(temp[i-], temp[]);//进行下一次迭代
down(, i - );
break;
}
}
}
for (int i = ; i < N; ++i)
cout << temp[i] << (i < N - ? " " : "");
return ;
}

PAT甲级——A1098 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. PAT A1098 Insertion or Heap Sort (25 分)——堆排序和插入排序,未完待续。。

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

  5. A1098. Insertion or Heap Sort

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

  6. PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]

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

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

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

  8. PTA Insertion or Heap Sort

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

  9. 1098 Insertion or Heap Sort

    1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...

随机推荐

  1. Servlet接口的抽象方法实现

    1.init:初始化方法,在Servlet被创建时执行,只会执行一次2.service:提供服务,每此Servelet被访问时service都会执行3.destroy:销毁方法,在服务器正常关闭时执行 ...

  2. Apache Spark 2.2.0 中文文档 - Spark Streaming 编程指南

    Spark Streaming 编程指南 概述 一个入门示例 基础概念 依赖 初始化 StreamingContext Discretized Streams (DStreams)(离散化流) Inp ...

  3. 【数论分块】[BZOJ2956、LuoguP2260] 模积和

    十年OI一场空,忘记取模见祖宗 题目: 求$$\sum_{i=1}^{n}\sum_{j=1}^{m} (n \bmod i)(m \bmod i)$$ (其中i,j不相等) 暴力拆式子: $$ANS ...

  4. 推荐一个Java设计模式写的很好的博客

    博客地址:https://quanke.gitbooks.io/design-pattern-java/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E8%AE%BE%E8 ...

  5. log4j2 按日期分割,自动清理历史文件

    方式一:定义CronTriggeringPolicy <?xml version="1.0" encoding="UTF-8"?> <Conf ...

  6. luoguP2580 于是他错误的点名开始了 [Trie]

    题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉欧拉(详情请见已结束比赛CON900). ...

  7. 模拟求root——cf1067B

    注意最后一轮要单独求一下 且最后只能有一个root #include <bits/stdc++.h> using namespace std; #define MOD 1000000007 ...

  8. SQLite3与C++的结合应用

    SQLite并没有一次性做到位,只有下载这些东西是不能放在vs2010中并马上使用的,下载下来的文件中有sqlite3.c/h/dll/def,还是不够用的.我们需要的sqlite3.lib文件并不在 ...

  9. tyvj 1194 划分大理石(多重背包)

    传送门 解题思路 二进制优化多重背包裸题. 代码 #include<iostream> #include<cstdio> #include<cstring> #in ...

  10. Responder对象

    Responder对象 响应者是一个对象,它可以响应事件并处理它们.所有响应者对象是类的,最终从UIResponder的( IOS)或NSResponder ( OS X)继承实例.这些类声明一个编程 ...