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
/*
* 这题思路比较简单:
1.中间序列 前面有序 后面和原始序列相同 则为插入排序。
2.堆排序 后面的数 应该是原数组中从大到小的数 如果遇到不满足这个条件的数 说明堆排序即将进行到当前这个数
在走一趟堆排序即可
*/
#include "iostream"
#include "algorithm"
using namespace std;bool judge(int a[],int b[],int n) { /* 判断是不是插入排序 */
int len = ;
int i;
for ( i = ; i < n - ; i++)
if (b[i] > b[i + ]) {
len = i + ;
break;
}
for (i=len; i < n; i++) {
if (a[i] != b[i])
return false;
}
sort(b, b + len + );
return true;
}
void print(int a[], int n) {
for (int i = ; i < n; i++) {
if (i == )
cout << a[i];
else
cout << " " << a[i];
}
cout << endl;
}
void adjust(int b[], int n) {
int parent = ;
int child;
int temp = b[parent];
for (; parent * + <= n;parent = child) {
child = parent * + ;
if (child + <= n && b[child + ] > b[child])
child = child + ;
if (b[child] <= temp)
break;
b[parent] = b[child];
}
b[parent] = temp;
}
int main() {
int a[], b[];
int n;
cin >> n;
for (int i = ; i < n; i++) {
cin >> a[i];
}
for (int i = ; i < n; i++) {
cin >> b[i];
}
if (judge(a, b, n)) {
cout << "Insertion Sort" << endl;
print(b, n);
}
else {
cout << "Heap Sort" << endl;
int i;
sort(a, a + n);
for (i = n-; i >= ; i--) {
if (a[i] != b[i])
break;
}
int temp = b[];
b[] = b[i];
b[i] = temp;
adjust(b,i-); /* 剩下的元素调整为最大堆 */
print(b, n);
}
}

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

1098. Insertion or Heap Sort (25)的更多相关文章

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

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

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

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

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

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

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

  5. 【PAT甲级】1098 Insertion or Heap Sort (25 分)

    题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...

  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 (25分)

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

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

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

  9. pat1098. Insertion or Heap Sort (25)

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

随机推荐

  1. Android 通过http访问服务器

    目前Android 与服务器交互有两种方式:1.Socket 2. Http : 但由于Http的封装性以及性能比socket要好,所以推荐使用http方式和服务器交互: 通过http访问服务器有三种 ...

  2. Git教程之使用GitHub

    我们一直用GitHub作为免费的远程仓库,如果是个人的开源项目,放到GitHub上是完全没有问题的.其实GitHub还是一个开源协作社区,通过GitHub,既可以让别人参与你的开源项目,也可以参与别人 ...

  3. CCS使用TIPS

    2013-06-20 09:37:49 CCS使用TIPS: 代码编写: CCS中通过Using CodeSense方便写代码,跟VC助手类似,具体使用方法在ccs的help中搜索using visu ...

  4. Hibernate个人总结

    编写Hibernate第一个程序 Hibernate是目前最流行的持久层框架,专注于数据库操作.使用Hibernate框架能够使开发人员从繁琐的SQL语句和复杂的JDBC中解脱出来.Hibernate ...

  5. naotu.baidu.com 非常棒的脑图在线工具

    1.png 2.txt 短租 前台功能 房源查看 房源搜索 城市房源 注册登录 预定房源 房源退订 在线支付 评价房源 个人中心 我的订单 我的账户 我的收藏 消息通知 管理员后台 房源发布 会员管理 ...

  6. oralce索引和分区索引的使用

    oracle分区表和分区索引的本质就是将数据分段存储,包括表和索引(索引从本质上来讲也是表),表分区会将表分成多个段分别存储.由此数据查询过程改变为先根据查询条件定位分区表,然后从该表中查询数据,从而 ...

  7. SGU 438 The Glorious Karlutka River =) ★(动态+分层网络流)

    [题意]有一条东西向流淌的河,宽为W,河中有N块石头,每块石头的坐标(Xi, Yi)和最大承受人数Ci已知.现在有M个游客在河的南岸,他们想穿越这条河流,但是每个人每次最远只能跳D米,每跳一次耗时1秒 ...

  8. ssh-keygen+ssh-copy-id 在linux下实现ssh无密码登录访问

    环境: 192.168.2.10 192.168.2.11 实现:2.10 ssh无需密码登录到2.11 在2.10 ssh到2.11机器上,需要密码,这样对一些脚本工作不方便,因为需要密码,也就是需 ...

  9. 【转】Android - Button(按钮)的响应点击事件的4种写法

    原文网址:http://www.yrom.net/blog/2011/12/12/android-4-onclicklistener-of-button/ Button控件setOnclickList ...

  10. 查询Table name, Column name, 拼接执行sql文本, 游标, 存储过程, 临时表

    018_Proc_UpdateTranslations.sql: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO if (exists (select ...