标签:遍历 ++ 代码 scan 特点 a10 can using n+1
#include<bits/stdc++.h>
using namespace std;
void downAdjust(vector<int> &b, int low, int high){
int i = 1, j = 2*i;
while(j <= high){
if(j+1 <= high && b[j] < b[j+1]){
j = j + 1;
}
if(b[i] < b[j]){
swap(b[i], b[j]);
i = j;
j = i*2;
}else{
break;
}
}
}
int main(){
int n, p = 2;
scanf("%d", &n);
vector<int> a(n+1), b(n+1);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(int i = 1; i <= n; i++) scanf("%d", &b[i]);
while(p <= n && b[p-1] <= b[p]) p++;
int index = p;
while(p <= n && a[p] == b[p]) p++;
if(p == n+1){
printf("Insertion Sort\n");
sort(b.begin()+1, b.begin()+index+1);
}else{
printf("Heap Sort\n");
p = n;
while(p > 2 && b[p] >= b[1]) p--;
swap(b[1], b[p]);
downAdjust(b, 1, p-1);
}
printf("%d", b[1]);
for(int i = 2; i <= n; i++){
printf(" %d", b[i]);
}
return 0;
}
A1098 Insertion or Heap Sort (25分)
标签:遍历 ++ 代码 scan 特点 a10 can using n+1
原文地址:https://www.cnblogs.com/tsruixi/p/12354450.html