pat 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25)
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)的更多相关文章
- PAT甲级1098. Insertion or Heap Sort
PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...
- PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...
- PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- 【PAT甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
- 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 ...
- PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- 1098. Insertion or Heap Sort (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT甲级——A1098 Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
随机推荐
- SVN不显示状态图标
1,输入win+R,输入regedit,调出注册表信息 2,按下Ctrl+F,在注册表里搜索“ShellIconOverlayIdentifiers” 3,将TortoiseAdded.Tortois ...
- Steamroller-freecodecamp算法题目
Steamroller 1.要求 对嵌套的数组进行扁平化处理.你必须考虑到不同层级的嵌套. 2.思路 设定结果数组res 用for循环遍历arr的元素,判断是否为数组,是,则用res=res.conc ...
- win10如何修改host文件
首先找到host文件,一般位于:C:\Windows\System32\drivers\etc 之后用记事本打开,直接修改.保存txt文件到桌面. 最后删除后缀名,再粘贴回去就可以了.
- GNU C中__attribute__
__attribute__基本介绍: 1. __attribute__ 可以设置函数属性.变量属性和类型属性. 2. __attribute__ 语法格式为:__attribute__ ((attri ...
- linux正则表达式企业级深度实践案例1
linux正则表达式结合三剑客企业级实践: 1.取系统ip [root@redhat~]# ifconfig eth0 解答: 替换命令: sed 's#支持正则位置##g' file 先取第 ...
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 水题:HDU-1088-Write a simple HTML Browser(模拟题)
解题心得: 1.仔细读题,细心细心...... 2.题的几个要求:超过八十个字符换一行,<br>换行,<hr>打印一个分割线,最后打印一个新的空行.主要是输出要求比较多. 3. ...
- firewall-cmd 防火墙命令详解 及 TCP Wrappers
firewall-cmd 常用参数及作用 参数 作用 --get-default-zone 查询默认的区域名称 --set-default-zone=<区域名称> 设置默认的区域,使其永久 ...
- vue时时监听input输入框中 输入内容 写法
Vue input 监听 使用 v-on:input="change" 实现即可 App.vue <template> <div> <md-field ...
- 在 Amazon AWS 搭建及部署网站:(二)安装、配置软件,启动网站
现在,我们已经有了一台EC2主机,具备了基本的硬件环境.下面,开始软件环境的配置. 第一步:连接服务器 后面所有的一切,都需要在SSH终端窗口操作.首先,我们需要一个SSH客户端.PuTTY是很常用的 ...