URAL1306-Sequence Median(优先队列)

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

题意:求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;
}

URAL1306-Sequence Median(优先队列),布布扣,bubuko.com

时间: 2024-08-03 07:17:39

URAL1306-Sequence Median(优先队列)的相关文章

URAL1306 Sequence Median(卡内存神题)

给出n个数,n<=250000,求这n个数的中位数,内存限制1mb 卡内存的神题,用数组存下来刚好1mb,再加上执行时消耗内存.立即爆. 因此我们用优先队列存储一半的数. 网上的某些代码,用priority_queue全爆内存. 我存的125000长度的数组.加上STL的make_heap() #include<cstdio> #include<queue> using namespace std; int a[125010]; int main() { int n,x; d

[Ural1306] Sequence Median(网上很多题解骗人,这才是对的!业界良心!)

血的教训: 1. 尽信题解,不如无题解! 2. C++ STL很坑爹.. 测试结果分析与比较: #1是我自己写的,使用priority_queue,超内存: #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; priority_queue <unsigned int> pq; int main(){ unsi

URAL 1306 Sequence Median(优先队列)

题意:求一串数字里的中位数.内存为1M.每个数范围是0到2的31次方-1. 思路:很容易想到把数字全部读入,然后排序,但是会超内存.用计数排序但是数又太大.由于我们只需要第n/2.n/2+1大(n为偶数)或第(n+1)/2大(n为奇数).所以可以用优先队列来维护最值,这样只需要存一半元素(n/2+1个元素)就可以了. #include<cstdio> #include<algorithm> #include<queue> #define UL unsigned int

ural 1306. Sequence Median

1306. Sequence Median Time limit: 1.0 secondMemory limit: 1 MBLanguage 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

poj 2623 Sequence Median 堆的灵活运用

I - Sequence Median Time Limit:1000MS     Memory Limit:1024KB     64bit IO Format:%I64d & %I64u Submit Status Description 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

HDU 6047 17多校 Maximum Sequence(优先队列)

Problem Description Steph is extremely obsessed with "sequence problems" that are usually seen on magazines: Given the sequence 11, 23, 30, 35, what is the next number? Steph always finds them too easy for such a genius like himself until one da

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

[题意]给出n(1~250000)个数(int以内),求中位数 [题解]一开始直接sort,发现MLE,才发现内存限制1024k,那么就不能开int[250000]的数组了(4*250000=1,000,000大约就是1M内存). 后来发现可以使用长度为n/2+1的优先队列,即包含前一半的数以及中位数,其他数在读入的时候就剔除,这样可以省一半的空间. 1 #include<bits/stdc++.h> 2 #define eps 1e-9 3 #define FOR(i,j,k) for(in

URAL 1306-Sequence Median(堆)

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 th

解题报告——POJ 2623

Sequence Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15792   Accepted: 4409 Description 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