1306. Sequence Median

Time limit: 1.0 second
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

input output
4
3
6
4
5
4.5
Problem Source: II Collegiate Students Urals Programming Contest. Yekaterinburg, April 3-4, 1998
Difficulty: 153
 
题意:求n个数的中位数
分析:注意看空间限制。。。
显然,我们只能存下一半的数据,
怎么办呢。。。。
。。。。
那我们就存最大的那一半(n / 2 + 1)数据就好了。。
。。。然后我们就用一个堆或者其他什么存下来。。。
偶数就去存下来的前两个,奇数就取存下来的第一个
用优先队列是不行的,因为优先队列申请空间是一段一段的。。。
只能用make_heap了
 #include <cstdio>
#include <queue>
#include <iostream>
using namespace std;
inline void SetIO(string Name)
{
string Input = Name+".in",
Output = Name+".out";
freopen(Input.c_str(), "r", stdin),
freopen(Output.c_str(), "w", stdout);
} int n, Arr[ / + ]; inline void Input()
{
scanf("%d", &n);
} inline void Solve()
{
for(int i = ; i < n / + ; i++) scanf("%d", &Arr[i]);
make_heap(Arr, Arr + n / + );
for(int i = n / + ; i < n; i++)
{
scanf("%d", &Arr[n / + ]);
push_heap(Arr, Arr + n / + );
pop_heap(Arr, Arr + n / + );
}
if(n & ) printf("%.1lf\n",1.0 * Arr[]);
else
{
unsigned int x = Arr[];
pop_heap(Arr, Arr + n / + );
unsigned int y = Arr[];
printf("%.1lf\n", 1.0 * (x + y) / 2.0);
}
} int main()
{
#ifndef ONLINE_JUDGE
SetIO("G");
#endif
Input();
Solve();
return ;
}

ural 1306. Sequence Median的更多相关文章

  1. URAL 1306 Sequence Median(优先队列)

    题意:求一串数字里的中位数.内存为1M.每个数范围是0到2的31次方-1. 思路:很容易想到把数字全部读入,然后排序,但是会超内存.用计数排序但是数又太大.由于我们只需要第n/2.n/2+1大(n为偶 ...

  2. URAL 1306 - Sequence Median 小内存求中位数

    [题意]给出n(1~250000)个数(int以内),求中位数 [题解]一开始直接sort,发现MLE,才发现内存限制1024k,那么就不能开int[250000]的数组了(4*250000=1,00 ...

  3. 1306.Sequence Median(堆排序)

    1306 URAL真是没水题 以为简单的排序就好了 ME  内存限制很紧 堆排序 或者 STL 用堆排序做的 正好复习一下 都忘了 #include <iostream> #include ...

  4. poj 2623 Sequence Median 堆的灵活运用

    I - Sequence Median Time Limit:1000MS     Memory Limit:1024KB     64bit IO Format:%I64d & %I64u ...

  5. Ural 1248 Sequence Sum 题解

    目录 Ural 1248 Sequence Sum 题解 题意 题解 程序 Ural 1248 Sequence Sum 题解 题意 给定\(n\)个用科学计数法表示的实数\((10^{-100}\s ...

  6. (poj)Sequence Median

    Description Given a sequence of N nonnegative integers. Let's define the median of such sequence. If ...

  7. URAL1306 Sequence Median(卡内存神题)

    给出n个数,n<=250000,求这n个数的中位数,内存限制1mb 卡内存的神题,用数组存下来刚好1mb,再加上执行时消耗内存.立即爆. 因此我们用优先队列存储一半的数. 网上的某些代码,用pr ...

  8. 二分法&三分法

    ural History Exam    二分 #include <iostream> #include <cstdlib> using namespace std; //二分 ...

  9. 投入OJ的怀抱~~~~~~~~~~

    OpenJudge C20182024 信箱(1) 账号 修改设定 退出小组 管理员 frank 林舒 Dzx someone 李文新 公告 11-05 程序设计与算法(大学先修课) 成员(61910 ...

随机推荐

  1. CC MayClg 15 T3

    www.codechef.com/MAY15/problems/CHAPD 一道比较神奇的题目... 看到题目后自己yy出了个傻逼算法...然后对拍都是对的...提交都是错的...然后一看" ...

  2. linux增加自定义path和manpath

    linux安装软件到自定义路径时,新安装的命令需要带上路径才可以执行,不能像系统自带命令那样可以直接使用. 这个时候可以通过修改环境变量PATH和MANPATH,来实现像系统命令一样使用新安装的命令并 ...

  3. HTML控件ID和NAME属性及在CS页面获得.ASPX页面中HTML控件的值

    <转载>来自网络 一.ID是在客户端脚本里用!NAME是用于获取提交表单的某表单域信息,在form里面,如果不指定Name的话,就不会发送到服务器端,所以有name属性的控件,必须指定na ...

  4. Windows命令行提取日期时间

    参考: http://elicecn.blog.163.com/blog/static/174017473200931910320556/ SET str="%date:~0,4%%date ...

  5. jQuery ajax跨域请求的解决方法

    在Ajax应用中,jQuery的Ajax请求是非常容易而且方便的,但是初学者经常会犯一个错误,那就是Ajax请求的url不是本地或者同一个服务器下面的URI,最后导致虽然请求200,但是不会返回任何数 ...

  6. Java for LeetCode 073 Set Matrix Zeroes

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 解题思路: ...

  7. Java for LeetCode 069 Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. 解题思路一: public int mySqrt(int x) ...

  8. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  9. codeforces B. Dima and Text Messages 解题报告

    题目链接:http://codeforces.com/problemset/problem/358/B 题目意思:给出n个单词(假设为word1,word2.word3...wordn)和一句test ...

  10. UITableView:下拉刷新和上拉加载更多

    [转载请注明出处] 本文将说明让UIScrollView支持"下拉刷新"和"上拉加载更多"的实现机制,并实现一个可用的tableView子类,以下主要以&quo ...