这个题很简单,可以用sort秒掉,但似乎是想考二叉堆排序。 用sort写了个居然是0ms,二叉堆也是0ms /* * File: main.cpp * Author: Mi * * Created on 2011年2月24日, 下午9:06 */ #include <cstdlib> #include <stdio.h> #define N 10005 using namespace std; /* * */ int heap[N],hs=0; void swap(int &a,int &b) { a^=b; b^=a; a^=b; } void sink(int p) { int q=p<<1,a=heap[p]; while(q<=hs) { if(q<hs&&heap[q++]<heap[q]) q++; if(heap[q]>=a) break; heap[p]=heap[q]; p=q; q=p<<1; } heap[p]=a; } void deletemin() { heap[1]=heap[hs--]; sink(1); } void insert(int x) { int i=hs; heap[hs++]=x; while(heap[i]<heap[i>>1]&&i>0) { swap(heap[i],heap[i>>1]); i=i>>1; } } void del() { int i=1; heap[1]=heap[hs-1]; hs--; while(1) { if((i<<1)<hs&&(i<<1|1)<hs) { if(heap[i<<1]<heap[i<<1|1]) { if(heap[i]>heap[i<<1]) { swap(heap[i],heap[i<<1]); i=i<<1; } else break; } else { if(heap[i]>heap[i<<1|1]) { swap(heap[i],heap[i<<1|1]); i=i<<1|1; } else break; } } else break; } } int main(int argc, char** argv) { int n,i,x; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&x); insert(x); } for(i=1;i<n/2;i++) del(); printf("%d/n",heap[1]); return 0; }