[PAT A1098]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?
输入格式
Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, Nintegers 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.
输出格式
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 resulting 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.
输入样例1
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0
输出样例1
Insertion Sort
1 2 3 5 7 8 9 4 6 0
输入样例2
10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9
输出样例2
Heap Sort
5 4 3 1 0 2 6 7 8 9
解析
1.题目大意;就是输入一个初始序列,然后给一个中间序列,要我们判断是根据插入排序还是堆排序形成的中间步骤,并且要求我们把下一步形成的序列输出来。
2.存在的问题(希望有大佬来指点):对于第三组数据总是过不了,22行处原来的判断条件是if (in1 == mid),后来改为if (in1 == mid && i != 2)便能过了。我自己想既然输入的序列不会是初始序列,那么不用加后面的判断条件,怎样的输入会使得程序过不了,还请大佬们能指点一二。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> in1, in2, mid;
int n;
void creat();
void DownAdjust(int low, int high);
int main()
{
scanf("%d", &n);
in1.resize(n + 1);
in2.resize(n + 1);
mid.resize(n + 1);
for (int i = 1; i <= n; i++) {
scanf("%d", &in1[i]);
in2[i] = in1[i];
}
for (int i = 1; i <= n; i++) scanf("%d", &mid[i]);
bool flag = true;//需不需要使用堆排序
for (int i = 2; i <= n; i++) {
if (in1 == mid && i != 2) {
flag = false;
printf("Insertion Sort\n");
sort(in1.begin(), in1.begin() + i + 1);
for (int j = 1; j <= n; j++) {
printf("%d", in1[j]);
if (j != n) printf(" ");
}
}
sort(in1.begin() + 1, in1.begin() + i + 1);
}
if (flag) {
creat();
for (int i = n; i > 1; i--) {
if (mid == in2) {
printf("Heap Sort\n");
swap(in2[1], in2[i]);
DownAdjust(1, i - 1);
for (int j = 1; j <= n; j++) {
printf("%d", in2[j]);
if (j != n) printf(" ");
}
}
swap(in2[1], in2[i]);
DownAdjust(1, i - 1);
}
}
return 0;
}
void creat() {
for (int i = n / 2; i >= 1; i--)
DownAdjust(i, n);
}
void DownAdjust(int low, int high) {
int i = low, j = 2 * i;
while (j <= high) {
if (j + 1 <= high && in2[j + 1] > in2[j]) j = j + 1;
if (in2[i] >= in2[j])break;
swap(in2[i], in2[j]);
i = j;
j = i * 2;
}
}