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. 问题005:如何配置JDK,Java运行环境?

    方法一:我的电脑右击-->属性-->高级-->环境变量-->Path 方法二:set path是查询环境变灵, set path=路径

  2. 【搜索】【入门】洛谷P1036 选数

    题目描述 已知 n个整数x1​,x2​,…,xn​,以及1个整数k(k<n).从nn个整数中任选kk个整数相加,可分别得到一系列的和. 例如当n=4,k=3,4个整数分别为3,7,12,19时, ...

  3. SummerVocation_Learning--java的基本概念

    基本数据类型:四类八种. 四类:整数型(默认int),浮点型(默认double),逻辑型(布尔型),文本型(字符型). 八种:int, byte, short, long; double, float ...

  4. STL笔记(に)--vector容器

    Vector 1.可变长的动态数组 2.需包含头文件#include<vector> (当然,如果用了万能头文件#include<bits/stdc++.h>则可忽略) 3.支 ...

  5. 洛谷P2468 [SDOI2010]粟粟的书架(二分答案 前缀和 主席树)

    题意 题目链接 给出一个矩形,每个点都有一些值,每次询问一个子矩阵最少需要拿几个数才能构成给出的值 Sol 这题是真坑啊.. 首先出题人强行把两个题拼到了一起, 对于前$50 \%$的数据,考虑二分答 ...

  6. SVG path

    在网页上画一图形,比如星星或波浪线,开始是想着图形软件画一个的,后来发现SVG这绘图程序的语言,感觉甚是可以,就发了些时间学了一下,在此做一简单分享和记录. 菜鸟上是这么介绍的(SVG 是使用 XML ...

  7. python 进度条 打印

  8. nuxt.js express模板项目虚拟目录部署问题汇总

    声明环境 反向代理:nginx或者iis的ARR 模板项目:nuxt-express 部署环境:windows 经过了一段时间在windows环境部署项目来看,关于虚拟目录的问题汇总如下, 发布场景假 ...

  9. vue购物车的实现

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 饭卡 HDU - 2546(dp)

    电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够).所以大家 ...