1098. Insertion or Heap Sort (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At 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

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 思路:是什么排序方式可以依据插入排序的特点来判断:插入排序排了一部分的数列的前半部分是从小到大排列的,后面一部分和原数列一样。
至于堆排序的操作,每次取出未排序部分数列的最大值后,再对未排序部分进行过滤调整。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<cstdio>
using namespace std;
#define N_MAX 200+5
int n;
int a[N_MAX],b[N_MAX]; void adjust(int *a,int i,int N) {//将a[i]处的值调整为以它为根的子树中的最大值
int child;
int tmp=a[i];
for (; ( * i + ) < N;i=child) {
child = * i + ;
if (child != N - && a[child + ] > a[child]) {
child++;
}
if (tmp < a[child]) { a[i] = a[child];}
else break;
}
a[i] = tmp;
} int main() {
while (cin>>n) {
for (int i = ; i < n; i++)cin >> a[i];
for (int i = ; i < n; i++)cin >> b[i];
int k ;
for (k = ; k < n - && b[k] <= b[k + ]; k++);
int p = k + ;
for (; p < n&&b[p] == a[p]; p++);
if (p!=n) {
puts("Heap Sort");
int num = n - ,j;
for (j = num; j >= ;j--) {
if (b[j] < b[])break;
}
swap(b[j], b[]);
adjust(b, , j);
for (int i = ; i < n;i++) printf("%d%c", b[i], i + == n ? '\n' : ' ');
}
else {
puts("Insertion Sort");
sort(b,b+k+);
for (int i = ; i < n; i++)
printf("%d%c",b[i],i+==n?'\n':' ');
}
}
return ;
}

STL里也有关于堆的操作make_heap,pop_heap().这里取堆排序操作的下一步,可以直接用这些函数解决。

代码:

#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<cctype>
#include<cmath>
#include<cstring>
#include<vector>
#include<set>
#include<queue>
#include<limits.h>
#include<sstream>
using namespace std;
typedef long long ll;
#define N_MAX 1000+5
#define INF 0x3f3f3f3f
int n;
vector<int>oring;
vector<int>cur;
int main() {
while (cin>>n) {
oring.resize(n);
cur.resize(n);
for (int i = ; i < n; i++)cin >> oring[i];
for (int i = ; i < n; i++)cin >> cur[i];
int k=;
while (k < n-&&cur[k] <= cur[k + ])k++;
int p = k + ;
while (p < n&&cur[p] == oring[p])p++;
if (p != n) {
puts("Heap Sort");
int num ;
for (num = n - ; num >= ; num--)
if (cur[num] < cur[])break;
pop_heap(cur.begin(), cur.begin() + num+);
for(int i=;i<cur.size();i++)
printf("%d%c", cur[i], i + == cur.size() ? '\n' : ' ');
}
else {
puts("Insertion Sort");
sort(cur.begin(),cur.begin()+k+);
for (int i = ; i < cur.size(); i++)
printf("%d%c",cur[i],i+==cur.size()?'\n':' ');
} }
return ;
}

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

  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 Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]

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

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

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

  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. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

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

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

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

  8. 1098. Insertion or Heap Sort (25)

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

  9. PAT甲级——A1098 Insertion or Heap Sort

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

随机推荐

  1. rem适配方案

    页面布局单位计算 一般有两大类:绝对长度单位和相对长度单位 绝对长度单位: px 像素:是显示屏上显示的每一个小点,为显示的最小单位 in 英寸,1in = 96px cm 厘米,1cm = 37.8 ...

  2. es6中的变量声明

    目录 es6中的变量声明 变量的声明 es6中的变量声明 变量的声明 for (var i = 0; i < 5; i++) { console.log(i) } var声明 作用域问题 上面的 ...

  3. 五、Shell 基本运算符

    Shell 基本运算符 Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令 ...

  4. 四、Shell 数组

    Shell 数组 数组中可以存放多个值.Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与 PHP 类似). 与大部分编程语言类似,数组元素的下标由0开始. She ...

  5. GBK UTF8 GB2132

    GBK就是在保存你的帖子的时候,一个汉字占用两个字节,外国人看会出现乱码,为此我中华为自己汉字编码而形成之解决方案. UTF8就是在保存你的帖子的时候,一个汉字占用3个字节.但是外国人看的话不会乱码. ...

  6. MQTT的学习之Mosquitto发布-订阅(2)

    在<MQTT的学习之Mosquitto安装&使用(1)>一文末尾,我已经模拟了发布-订阅模式,只是那时在服务器直接模拟的,并不是java代码模拟的.下面贴出Java代码 1.首先引 ...

  7. 802. Find Eventual Safe States

    https://leetcode.com/problems/find-eventual-safe-states/description/ class Solution { public: vector ...

  8. CCPC_1003

    这个题可以暴力的哟,直接暴力的哟 不用做什么订立的哟 不需要特别判断的哟 去死吧!!!愚蠢的我! #include<bits/stdc++.h> using namespace std; ...

  9. jmeter XML格式的结果中各属性的含义

    最近在搞jmeter,生成xml的测试报告,对报告字段进行解释,可能是自己不会找,网上资源不多,好不容易找到的,记录下来: 感谢博主:http://blog.163.com/zhang_jing/bl ...

  10. 友推在Android 实现微信等分享代码的常见问题

    介绍,最近 做了一个项目,需要集成分享功能.果断选择 友推. 集成过程,参考友推官方提供的集成文档即可 废话不多说,主要说一下自己在集成过程中遇到的一些问题,主要有两个: 问题1. 引入youtui- ...