回复 8楼 pangding
程序代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
#define N 50005
int heap[N];
int top = 0;
bool cmp(const int &a,const int &b){
return a > b;
}
int main ()
{
int n;
scanf("%d",&n);
string name;
for(int i = 0; i < n; i++){
cin >> name;
if(name == "add"){
//printf("top==%d\n",top);
int x;
cin >> x;
heap[top++]=x;
make_heap(&heap[0],&heap[top],cmp);
}
else if(name == "query"){
//printf("top==%d\n",top);
if(top == 0) printf("empty\n");
else{
// sort_heap(&heap[0],&heap[top-1],cmp);
printf("%d\n",heap[0]);
}
}else if(name == "del"){
//printf("top==%d\n",top);
if(top == 0) printf("empty\n");
else{
pop_heap(&heap[0],&heap[top],cmp);
top--;
}
}
}
return 0;
}
STL里面的heap函数调用效率不是很高啊。。。虽然简单了,但还是tle了