Sequence Median
来源:互联网 发布:erp源码 编辑:程序博客网 时间:2024/06/12 01:10
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
#include <iostream>#include <cstdio>#include <queue>#include <vector>#include <algorithm>using namespace std;int main(){ int n,i; long long a,b; scanf("%d",&n); priority_queue<unsigned int, vector<unsigned int>, greater<unsigned int> > q;//用优先队列,默认从小到大排序 for(i = 1; i <= n; i ++) { scanf("%lld",&a); q.push(a); if(q.size() > n/2+1)//中位数储存位置 q.pop(); } if(n%2) printf("%d\n",q.top()); else { a = q.top(); q.pop(); b = q.top(); if((a+b)%2 == 0) printf("%lld\n",(a+b)/2); else printf("%.1lf\n",(a+b)*1.0/2); } return 0;}
- 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
- (poj)Sequence Median
- poj2623 Sequence Median
- poj-2623-Sequence Median
- ural 1306 Sequence Median
- URAL 1306-Sequence Median(堆)
- POJ2623 Sequence Median【中位数+排序】
- pku 2623 sequence median 水题 解题报告
- URAL1306-Sequence Median(优先队列)
- URAL1306--Sequence Median(优先队列)
- ural 1306. Sequence Median(优先队列)
- URAL1306 Sequence Median(卡内存神题)
- hdu 2034 人见人爱A-B
- Android google map api key的申请与使用(debug/release)
- HDU 1285 确定比赛名次——拓扑排序
- 以前自己写的一个分页的存储过程
- Jboss7.1.1安装
- Sequence Median
- JavaStringBuffer和string的区别
- xcode 中 c++ Standard Library的选择 和 如何混编c++
- error LNK2019: 无法解析的外部符号
- POJ1579-记忆化搜索
- Java程序员成长之路(你必须知道的优化、分析源代码的Java工具
- google管理员工具的功能
- 鼠标悬浮图片显示原图 移出鼠标后原图消失 js
- cocos2d-x v3.0新特性及使用