1098. Insertion or Heap Sort (25)
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
/*
* 这题思路比较简单:
1.中间序列 前面有序 后面和原始序列相同 则为插入排序。
2.堆排序 后面的数 应该是原数组中从大到小的数 如果遇到不满足这个条件的数 说明堆排序即将进行到当前这个数
在走一趟堆排序即可
*/
#include "iostream"
#include "algorithm"
using namespace std;bool judge(int a[],int b[],int n) { /* 判断是不是插入排序 */
int len = ;
int i;
for ( i = ; i < n - ; i++)
if (b[i] > b[i + ]) {
len = i + ;
break;
}
for (i=len; i < n; i++) {
if (a[i] != b[i])
return false;
}
sort(b, b + len + );
return true;
}
void print(int a[], int n) {
for (int i = ; i < n; i++) {
if (i == )
cout << a[i];
else
cout << " " << a[i];
}
cout << endl;
}
void adjust(int b[], int n) {
int parent = ;
int child;
int temp = b[parent];
for (; parent * + <= n;parent = child) {
child = parent * + ;
if (child + <= n && b[child + ] > b[child])
child = child + ;
if (b[child] <= temp)
break;
b[parent] = b[child];
}
b[parent] = temp;
}
int main() {
int a[], b[];
int n;
cin >> n;
for (int i = ; i < n; i++) {
cin >> a[i];
}
for (int i = ; i < n; i++) {
cin >> b[i];
}
if (judge(a, b, n)) {
cout << "Insertion Sort" << endl;
print(b, n);
}
else {
cout << "Heap Sort" << endl;
int i;
sort(a, a + n);
for (i = n-; i >= ; i--) {
if (a[i] != b[i])
break;
}
int temp = b[];
b[] = b[i];
b[i] = temp;
adjust(b,i-); /* 剩下的元素调整为最大堆 */
print(b, n);
}
}
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
1098. Insertion or Heap Sort (25)的更多相关文章
- pat 甲级 1098. Insertion or Heap Sort (25)
		
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
 - 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)-(插入排序和堆排序)
		
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
 - 【PAT甲级】1098 Insertion or Heap Sort (25 分)
		
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
 - PAT Advanced 1098  Insertion or Heap Sort (25) [heap sort(堆排序)]
		
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
 - 1098 Insertion or Heap Sort (25分)
		
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
 - PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)
		
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...
 - pat1098. Insertion or Heap Sort (25)
		
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
 
随机推荐
- C#操作xml文档,cuid,dategridview显示数据
			
界面 //所有的数据全部在集合中 //dgv控件绑定List集合,List集合中有User类的对象,对象里有属性,把属性绑定到没列的数据上 dgv.Da ...
 - 107. Binary Tree Level Order Traversal II
			
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
 - SDUT2157——Greatest Number(STL二分查找)
			
Greatest Number 题目描述Saya likes math, because she think math can make her cleverer.One day, Kudo invi ...
 - Android EditText属性
			
1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" // 以”.”形式显示文本 ( ...
 - Foreman--管理PuppetClient
			
一. 环境: 1. server: puppetmaster+activemq+foreman1.3 server1.xxx.com(10.8.1.201) 2. client: fedora 19 ...
 - 无效的 URI: 未能分析证书颁发机构/主机
			
出 现该错误的原因是URL中少了一个斜杠,正常的URL是“http:”后边有两个斜杠,而我在修改配置文件中的URL的IP地址部分时,不小心删掉了一个 斜杠,例如:http:/blog.csdn.net ...
 - linux 查看系统信息命令
			
linux 查看系统信息命令是linux初学者必备的基础知识, 这些命令也非常有用, 因为进入linux第一件事就可能是首先查看系统信息, 因此必要的系统的学习一下这些linux系统信息命令还是非常有 ...
 - poj 1364 King(差分约束)
			
题目:http://poj.org/problem?id=1364 #include <iostream> #include <cstdio> #include <cst ...
 - POJ  3207  Ikki's Story IV - Panda's Trick  (2-SAT,基础)
			
题意: 有一个环,环上n个点,现在在m个点对之间连一条线,线可以往圆外面绕,也可以往里面绕,问是否必定会相交? 思路: 根据所给的m条边可知,假设给的是a-b,那么a-b要么得绕环外,要么只能在环内, ...
 - 剑指Offer:互为变位词
			
// 判断两个单词是否互为变位词: 如果两个单词中的字母相同,并且每个字母出现的次数也相同, 那么这两个单词互为变位词 #include <stdio.h> #include <st ...