1098 Insertion or Heap Sort——PAT甲级真题
1098 Insertion or Heap Sort
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
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
题目大意:给你一组初始序列和中间序列,让你根据序列来判断是插入排序还是堆排序。然后输出中间序列的下一趟排序
解法1:按照题目所给要求对插入排序的每一步进行模拟,每一趟的排序结果和中间序列进行比较,如果相同则为插入排序,否则为堆排序。注意:因为序列是按照从小到大排列,所以根据堆的性质我们要构建的堆是大根堆。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int init_array[N], final_array[N], tmp_array[N];
int n;
bool same_array() {
for (int i = 1; i <= n; i++) {
if (init_array[i] != final_array[i]) return false;
}
return true;
}
void print_array() {
for (int i = 1; i <= n; i++) {
cout << init_array[i];
if (i != n)
cout << " ";
else
cout << endl;
}
}
bool insert_sort() {
int i, j;
bool flag = false;
for (i = 2; i <= n; i++) {
if (i != 2 && same_array()) flag = true;
if (init_array[i] < init_array[i - 1]) {
init_array[0] = init_array[i];
for (j = i - 1; init_array[0] < init_array[j]; j--)
init_array[j + 1] = init_array[j];
init_array[j + 1] = init_array[0];
if (flag) {
return true;
}
}
}
return false;
}
void down(int u, int sizes) {
int t = u;
if (u * 2 <= sizes && init_array[u * 2] > init_array[t]) t = u * 2;
if (u * 2 + 1 <= sizes && init_array[u * 2 + 1] > init_array[t])
t = u * 2 + 1;
if (t != u) {
swap(init_array[t], init_array[u]);
down(t, sizes);
}
}
void up(int u) {
while (u / 2 && init_array[u / 2] > init_array[u]) {
swap(init_array[u / 2], init_array[u]);
u /= 2;
}
}
void heap_sort() {
//初建堆,因为是从小打到输出所以构建的是大顶堆
for (int i = n / 2; i >= 1; i--) down(i, n);
bool flag = false;
for (int i = n; i > 1; i--) {
if (i != n && same_array()) flag = true;
swap(init_array[1], init_array[i]);
down(1, i - 1);
if (flag) {
print_array();
return;
}
}
}
int main() {
scanf("%d", &n);
// sizes = n;
for (int i = 1; i <= n; i++) {
scanf("%d", &init_array[i]);
tmp_array[i] = init_array[i];
}
for (int i = 1; i <= n; i++) scanf("%d", &final_array[i]);
if (insert_sort()) {
puts("Insertion Sort");
print_array();
} else {
puts("Heap Sort");
for (int i = 1; i <= n; i++) init_array[i] = tmp_array[i];
heap_sort();
}
return 0;
}
解法2:参考柳神的解法 ,堆排序大致思路相同,关键的是对插入排序的优化。根据插入排序的性质可以知道结点L(i)前L(1 ~i - 1)个元素都是有序序列,结点L(i + 1, n)个元素都是无序序列。所以我们只需要在中间序列中找到结点L(i)然后比较中间序列的L(i + 1, n)和原序列的L(i + 1, n),如果相同则为插入排序,如果不同则为堆排序。如果是插入排序直接对L(1 ~ i + 1)个元素进行sort即可。
利用插入排序优化的代码如下:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int init_array[N], final_array[N];
int n;
void down(int h[], int u, int sizes) {
int t = u;
if (u * 2 <= sizes && h[u * 2] > h[t]) t = u * 2;
if (u * 2 + 1 <= sizes && h[u * 2 + 1] > h[t])
t = u * 2 + 1;
if (t != u) {
swap(h[t], h[u]);
down(h, t, sizes);
}
}
int main() {
scanf("%d", &n);
// sizes = n;
for (int i = 1; i <= n; i++) {
scanf("%d", &init_array[i]);
}
for (int i = 1; i <= n; i++) scanf("%d", &final_array[i]);
int p = 1;
while(p < n && final_array[p] <= final_array[p + 1]) p++;
bool flag = true;
for (int i = p + 1; i <= n; i++) {
if (init_array[i] != final_array[i]) {
flag = false;
break;
}
}
if (flag) {
puts("Insertion Sort");
sort(final_array + 1, final_array + p + 2);
}
else {
puts("Heap Sort");
int index = n;
while(index > 2 && final_array[index] > final_array[1]) index--;
swap(final_array[1], final_array[index]);
down(final_array,1, index - 1);
}
for (int i = 1; i <= n; i++) {
cout << final_array[i];
if (i != n)
cout << " ";
else
cout << endl;
}
return 0;
}
1098 Insertion or Heap Sort——PAT甲级真题的更多相关文章
- 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 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 1098 Insertion or Heap Sort
1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
- 【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)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
随机推荐
- 使用Docker Registry管理Docker镜像
文章目录 使用Docker Registry管理Docker镜像 1.使用Docker Hub管理镜像 1.1注册与登录 1.2创建仓库 1.3推送镜像 2. 使用私有仓库管理镜像 2.1 搭建私有仓 ...
- MySQL数据库---配置文件及数据文件
1.主配置文件 #/usr/local/mysql/bin/mysqld --verbose --help |grep -A 1 'Default options' #cat /etc/my.cnf ...
- 23.centos 7配置网络
1.ifconfig:查看网卡信息 如果centos7 最小化安装没有ifconfig这个命令,可以使用yum install net-tools 来安装. centos7 网卡命名规则: en ...
- Nginx 指定域名(或子域名)和网站绑定
问题起因 博主最近在 CentOS 上面部署另外一个网站,但并不想通过端口号来访问,因为端口号对于 SEO 优化不利,且用户访问较繁琐(使用域名不就是为了方便用户访问吗?再引入端口号岂不是和使用域名的 ...
- Git轻松入门2:分支篇
什么是分支 在玩剧情类游戏时,不同的选择会触发不同的剧情路线,每条剧情路线都会独立发展,最终走向不同的结局. Git中所谓的"分支(branch)"就如同游戏中的剧情路线,用户可以 ...
- Preliminaries for Benelux Algorithm Programming Contest 2019
A. Architecture 如果行最大值中的最大值和列最大值中的最大值不同的话,那么一定会产生矛盾,可以手模一个样例看看. 当满足行列最大值相同条件的时候,就可以判定了. 因为其余的地方一定可以构 ...
- CodeForces 1119D(差分+前缀和+二分)
题意:给你一个数组,数组每次每个数都+1,有q次查询每一查询+L到+R中出现的所有不重复的数字个数. +L到+R其实就相当于是0到+(R-L+1) 感觉自己写的好啰嗦,直接上代码加注释: 1 #inc ...
- Codeforces Round #647 (Div. 2) A. Johnny and Ancient Computer
题目链接:https://codeforces.com/contest/1362/problem/A 题意 有一个正整数 $a$,可选择的操作如下: $a \times 2$ $a \times 4$ ...
- 20162017-acmicpc-south-pacific-regional-contest-sppc-16 B.Ballon Warehouse
题意:给你一个无限长且元素均为\(0\)的排列,每次给你一对\((x,y)\),表示在所有\(x\)的后面插入一个元素\(y\),最后给你一个区间\((l,r)\),输出\([l,r-1]\)中的所有 ...
- leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独
leetcode 36 感觉就是遍历. 保存好状态,就是各行各列还有各分区divide的情况 用数组做. 空间小时间大 class Solution { public: bool isValidSud ...