URAL 1306. Sequence Median(优先队列)

题目链接

本来想练习一下堆排序,试了交了几次发现不是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 }

转载于:https://www.cnblogs.com/naix-x/archive/2012/07/21/2602350.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值