题意
给定长度n,数组的初始状态a和中间状态b。判断该中间状态是由堆排序or插入排序变化而来,并输出下一个变化序列。
思路
首先根据数组a一步步进行插入排序,每次排完序后和中间状态b比较,相同则说明是插入排序,再计算下一次排序结果并输出。
如果a插入排序完成后也没和b相同,则说明是堆排序。此时由堆排序性质知道a、b数组后半段相同,以此知道剩余堆的len,移动、调整一次即可。
优化方向
由于堆排序比插入排序快,更好的方法是首先a一步步进行堆排序,而非插入。这里追求代码简洁而没走这条路,但存在n设置足够大时,通过这条路卡TLE的情况。
知识点回顾
开始想用快排先得到最终序列,结果写半天一直段错误SF……原因是内层循环王道用的while,y总用的do while,后者需要a[i] < x
,用混了。贴下代码,以后要经常回顾,理解细节。
void qs(int l, int r){
if(l >= r)return;
int i = l - 1, j = r + 1, x = a[l + r >> 1];
while(i < j){
do i ++; while(a[i] < x);
do j --; while(a[j] > x);
if(i < j) swap(a[i], a[j]);
}
qs(l, j), qs(j + 1, r);
}
坑点
while(a[j] > a[0]) a[j + 1] = a[j--];
上面这句话正常编译器逻辑该是最后进行j–,但PAT系统里j–在语句初进行了,导致TLE。这儿卡了好长时间。
解法
#include<bits/stdc++.h>
using namespace std;
const int N = 101;
int a[N], b[N], n, j, f;
int main(){
cin>>n;
for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);
for(int i = 1; i <= n; i ++) scanf("%d", &b[i]);
for(int i = 2; i <= n; i ++){
a[0] = a[i];
j = i - 1, f = 0;
while(a[j] > a[0]) a[j + 1] = a[j], j--;
a[j + 1] = a[0];
for(int k = 1; k <= n; k ++)
if(a[k] != b[k]) f = 1;
if(f) continue;
else{
puts("Insertion Sort");
a[0] = a[i + 1];
while(a[i] > a[0]) a[i + 1] = a[i], i--;
a[i + 1] = a[0];
for(int k = 1; k <= n; k ++){
cout<<a[k];
if(k != n) cout<<' ';
}
return 0;
}
}
puts("Heap Sort");
int len = n, p = 1, tar;
while(a[len] == b[len]) len--;
swap(b[len], b[1]);
while(p * 2 < len){
tar = p;
if(b[p * 2] > b[tar]) tar = p * 2;
if(p * 2 + 1 < len && b[p * 2 + 1] > b[tar]) tar = p * 2 + 1;
if(tar != p){
swap(b[tar], b[p]);
p = tar;
}
}
for(int i = 1; i <= n; i ++){
cout<<b[i];
if(i != n) cout<<' ';
}
return 0;
}
原题
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 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.
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