本来想练习一下堆排序,试了交了几次发现不是MLE,就是WA。。。发现,直接排序会超内存,用优先队列吧,顺便学习一下。。。
下个学期学C++,好好学习STL。
这个题可恶的精度,unsigned int 刚刚好。。
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <vector> 5 #include <algorithm> 6 using namespace std; 7 int main() 8 { 9 int n,i; 10 long long a,b; 11 scanf("%d",&n); 12 priority_queue<unsigned int, vector<unsigned int>, greater<unsigned int> > q; 13 for(i = 1;i <= n;i ++) 14 { 15 scanf("%lld",&a); 16 q.push(a); 17 if(q.size() > n/2+1) 18 q.pop(); 19 } 20 if(n%2) 21 printf("%d\n",q.top()); 22 else 23 { 24 a = q.top(); 25 q.pop(); 26 b = q.top(); 27 if((a+b)%2 == 0) 28 printf("%lld\n",(a+b)/2); 29 else 30 printf("%.1lf\n",(a+b)*1.0/2); 31 } 32 return 0; 33 }