URAL1306-Sequence Median(优先队列)
来源:互联网 发布:sqlserver大数据处理 编辑:程序博客网 时间:2024/06/06 12:42
1306. Sequence Median
Time limit: 1.0 second
Memory limit: 1 MB
Language limit: C, C++, Pascal
Memory limit: 1 MB
Language limit: C, C++, Pascal
Given a sequence of N nonnegative integers. Let's define the median of such sequence. If N is odd the median is the element with stands in the middle of the sequence after it is sorted. One may notice that in this case the median has position (N+1)/2 in sorted sequence if sequence elements are numbered starting with 1. If N is even then the median is the semi-sum of the two "middle" elements of sorted sequence. I.e. semi-sum of the elements in positions N/2 and (N/2)+1 of sorted sequence. But original sequence might be unsorted.
Your task is to write program to find the median of given sequence.
Input
The first line of input contains the only integer number N — the length of the sequence. Sequence itself follows in subsequent lines, one number in a line. The length of the sequence lies in the range from 1 to 250000. Each element of the sequence is a positive integer not greater than 231−1 inclusive.
Output
You should print the value of the median with exactly one digit after decimal point.
Sample
43645
4.5
Problem Source: II Collegiate Students Urals Programming Contest. Yekaterinburg, April 3-4, 1998
题意:求25000个数的中位数,但内存卡的很死,不能完全开下数组
思路:只要用优先队列维护前二分之一加一大的数即可。
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <string>#include <algorithm>#include <queue>using namespace std;priority_queue<int, vector<int>, greater<int> > pq;int main(){ int n; while(~scanf("%d",&n)){ int cnt = n/2+1; while(!pq.empty()) pq.pop(); for(int i = 0; i < n; i++){ int d; cin >> d; if(pq.size()<cnt){ pq.push(d); } else if(pq.top()<d){ pq.pop(); pq.push(d); } } double median; if(n&1){ median = pq.top(); }else{ int a = pq.top(); pq.pop(); int b = pq.top(); median = (a*1.0+b*1.0)/2.0; } printf("%.1lf\n",median); } return 0;}
0 0
- URAL1306-Sequence Median(优先队列)
- URAL1306--Sequence Median(优先队列)
- URAL1306 Sequence Median(卡内存神题)
- ural 1306. Sequence Median(优先队列)
- 优先级队列 ural1306(poj2623)
- hdu6047Maximum Sequence(优先队列)
- Sequence(优先队列,或堆)
- 【优先队列】:poj2442,Sequence
- Sequence(优先队列)
- ural 1306. Sequence Median(优先级队列 priority_queue用法)
- (poj)Sequence Median
- Sequence——优先队列
- POJ 2442 Sequence(优先队列)
- Sequence Poj2442 (堆/优先队列)
- Maximum Sequence 贪心 优先队列
- POJ 题目2442 Sequence(技巧,优先队列)
- POJ 2442 Sequence(stl+优先队列||堆)
- Sequence 优先队列模拟堆 poj 2442
- Machine Schedule 6.3.3
- Python Class Object
- Struts2学习笔记(2)值传递
- php 根据字符串生成对应数组方法
- Plugins for Sublime Text 2
- URAL1306-Sequence Median(优先队列)
- java 中使用 sqlite
- Android4.4 v2.0大图标的修改
- 新浪微博字数统计
- 获取WinCE电池电量
- HDU2119_Matrix(二分图/最小点覆盖=最大匹配)
- 【队列,树】poj 3253
- python 一个变量打印99乘法表
- 2014.08.15 周五-没记录-web