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 ...
随机推荐
- 使用 RuPengGame游戏引擎包 建立游戏窗体 如鹏游戏引擎包下载地址 Thread Runnable 卖票实例
package com.swift; import com.rupeng.game.GameCore;//导入游戏引擎包 //实现Runnable接口 public class Game_RuPeng ...
- c++ 中十进制 八进制 十六进制 二进制转换 最简方法
#include<iostream> using namespace std; int main() { int i; cin>>dec>>i; //cin> ...
- 51nod——2478 小b接水(预处理 思维)
我本来想把每个谷都处理了,想了下觉得不好办.后来看其他人写的是处理每个位置,把每个位置可以接的水累加起来.整挺好. #include <bits/stdc++.h> using names ...
- Uva 长城守卫——1335 - Beijing Guards
二分查找+一定的技巧 #include<iostream> using namespace std; +; int n,r[maxn],Left[maxn],Right[maxn];//因 ...
- leetcode笔记(一)309. Best Time to Buy and Sell Stock with Cooldown
题目描述 (原题目链接) Say you have an array for which the ith element is the price of a given stock on day i. ...
- pycahrm git配置笔记
1. 在file - setting - plugins 中查看是否有github插件, 此处是用于处理插件位置
- k8s的pv和pvc简述
pvc:资源需要指定:1.accessMode:访问模型:对象列表: ReadWriteOnce – the volume can be mounted as read-write by a s ...
- jQuery编码中的一些技巧
缓存变量 DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $('#element').css('height',h-20); // ...
- thinkphp 3.2.3 - App.class.php 解析
class App { public static function init() { load_ext_file(COMMON_PATH); // { // /home/www/www.domain ...
- Admin站点
使用admin站点 a.在settings.py中设置语言和时区 LANGUAGE_CODE = 'zh-hans' # 使用中国语言 TIME_ZONE = 'Asia/Shanghai' # 使用 ...