这道题是个排序题目,日常水;写这个这要是学习一下STL的伟大之处。
make_hesp:创建堆
push_heap:加入新元素后更新
pop_heap:推出堆首后更新
sort_heap:排序
参考的博客:点我传送
#include <algorithm>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
//#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
#define Foru(i,a,b) for(i = a;i < b;i ++)
#define Ford(i,a,b) for(i = a;i > b; i --)
#define MAXN 100001
int book[1010],num[1010];
bool cmp(int a,int b){
return a > b;
}
int main(){
int T,n,i,j,k;
scanf("%d",&T);
while(T --){
scanf("%d",&n);
Foru(i,0,n){
scanf("%d",&book[i]);
push_heap(book,book + i + 1,cmp);
}
// for(j = 0;j < n ;j ++){
// printf("%d%c",book[j],j == n-1?'\n':' ');
// }
Foru(i,0,n){
printf("%d%c",book[0],i == n-1?'\n':' ');
if(n - i - 1 >= 0)
pop_heap(book,book + n - i,cmp);
}
}
return 0;
}
/*
2
3 2 1 3
9 1 4 7 2 5 8 3 6 9
*/