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 ...
随机推荐
- SpringMVC 多视图解析器 跳转问题
在SpringMVC的配置文件中加入以下配置: <!-- 下面红色的配置必须要在--> <mvc:default-servlet-handler /> <bean id ...
- springboot中加入druid对sql进行监控
springboot作为现在十分流行的框架,简化Spring应用的初始搭建以及开发过程,现在我们就使用springboot来进行简单的web项目搭建并对项目sql进行监控. 项目的搭建就省略了,spr ...
- 5.Cisco Packet Tracer里关于交换机或路由器配置文件和系统映像备份与恢复
我们会将交换机或路由器的配置文件和系统镜像直接备份到tftp服务器上,所以我们需要准备一台tftp的服务器 1我们需要给服务器配一个ip地址,给路由器的f0/1端口配置一个ip地址,路由器与服务器能相 ...
- content is king – Bill Gates (1/3/1996) 内容为王 - 比尔盖茨
以下中文版本由谷歌翻译 内容为王 - 比尔盖茨(1/3/1996) 内容是我期望在互联网上赚取大部分真钱的地方,就像在广播中一样. 半个世纪前开始的电视革命催生了许多行业,包括制造电视机,但长期的赢家 ...
- ZendFramework-2.4 源代码 - 关于MVC - Controller层
// 1.控制器管理器 class ServiceManager implements ServiceLocatorInterface { public function __construct(Co ...
- PHP使用CURL_MULTI实现多线程采集
$connomains = array( "http://www.baidu.com/", "http://www.hao123.com/", "ht ...
- python3 练习题100例 (十)
题目十:判断101-200之间有多少个素数,并输出所有素数. #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 题目十 ...
- python3.7 os模块
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 os模块 #os模块是与操作系统交互的一个接口 # os.get ...
- Assignment HDU - 2853(二分图匹配 KM 新边旧边)
传送门: Assignment HDU - 2853 题意:题意直接那松神的题意了.给了你n个公司和m个任务,然后给你了每个公司处理每个任务的效率.然后他已经给你了每个公司的分配方案,让你求出最多能增 ...
- UVA:11297-Census(二维线段树)
Census Time Limit: 8 sec Description This year, there have been many problems with population calcul ...