ural 1306(堆-优先级序列)
2013-08-02 10:12
260 查看
很大数量的数,求中位数
思路:此题不能存储所有元素,否则会MLE,想到用优先队列(默认值大的优先级高),存储一半,剩下的一半依次和队首比较,若小于队首,则将队首元素出队,新元素入队。
最后,若n为奇数,队首元素即为中间值;n为偶数,队列前两个的平均值为答案。
思路:此题不能存储所有元素,否则会MLE,想到用优先队列(默认值大的优先级高),存储一半,剩下的一半依次和队首比较,若小于队首,则将队首元素出队,新元素入队。
最后,若n为奇数,队首元素即为中间值;n为偶数,队列前两个的平均值为答案。
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <queue> using namespace std; typedef unsigned int lint; priority_queue<lint> pq; int main() { int i, n; lint val, t; scanf("%d", &n); for (i = 0; i <= n/2; ++i) { scanf("%d", &val); pq.push(val); } for (; i < n; ++i) { scanf("%d", &val); t = pq.top(); if (val < t) { pq.pop(); pq.push(val); } } double ans; if (n&1) ans = pq.top(); else { ans = pq.top(); pq.pop(); ans += pq.top(); ans /= 2.0; } printf("%.1lf\n", ans); return 0; }
相关文章推荐
- ural 1306. Sequence Median(优先级队列 priority_queue用法)
- ural 1306. Sequence Median(优先级队列 priority_queue用法)
- 用最大堆模拟最大优先级序列(Python实现)
- Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)
- ural 1306. Sequence Median(优先队列)
- (hdu step 8.1.5)看病要排队(优先队列的基本使用——在看病排队时,优先级高的排在前面.求在这种情况下的出队序列)
- 优先级,结合性,序列点, 你理解清楚了么?
- 堆的TopK问题,优先级序列,堆排序d
- ural 1306 Sequence Median
- URAL 1306. Sequence Median(优先队列)
- URAL 1306 - Sequence Median 小内存求中位数
- Ural 1306. Sequence Median
- Ural 1306 Sequence Median
- URAL 1306-Sequence Median(堆)
- URAL 1306. Sequence Median
- OJ1306-最长公共子上升序列【dp】
- ural 鹰蛋&51nod 1306
- ural 1306. Sequence Median
- ural 1306 (heap推排序)
- URAL 1296. Hyperjump(最大子序列和)