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 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 2 31−1 inclusive.

Output

You should print the value of the median with exactly one digit after decimal point.

Sample Input

input output
4
3
6
4
5
4.5

这道题 你会发现存不下,只能存一半

然后瞎搞搞,就出来了

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100001
const int inf=0x7fffffff;   //无限大
priority_queue<unsigned int> q;
int main()
{
    int n;
    while(cin>>n)
    {
        while(!q.empty())
            q.pop();
        unsigned int x;
        int kill=n/2+1;
        unsigned int fuck;
        for(int i=0;i<kill;i++)
        {
            cin>>x;
            q.push(x);
        }
        for(int i=kill;i<n;i++)
        {
            cin>>x;
            fuck=q.top();
            if(x<fuck)
            {
                q.pop();
                q.push(x);
            }
        }
        if(n%2==0)
        {
            unsigned int a=q.top();
            q.pop();
            unsigned int b=q.top();
            printf("%.1lf\n",(a+b)/2.0);
        }
        else
        {
            unsigned int a=q.top();
            printf("%.1lf\n",a*1.0);
        }
    }
    return 0;
}
时间: 2024-10-05 06:50:43

poj 2623 Sequence Median 堆的灵活运用的相关文章

POJ 2442 Sequence【堆】

题目链接:http://poj.org/problem?id=2442 题目大意:给出一个m*n的矩阵,从每一行中取出一个数相加,能得到n^m个不同的结果,要求输出其中前n项. 建立一个以n元数组为底层数组的堆,在这里,利用stl中的make_heap,pop_heap,push_heap等函数解决. 1.将第一组数据输入arr1数组,升序排序. 2.将接下来的数据输入到arr2数组中,并且heap[i]=arr1[0]+arr2[0...n-1],make_heap(heap,heap+n).

[ACM] POJ 2442 Sequence (堆的性质)

Sequence Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7011   Accepted: 2262 Description Given m sequences, each contains n non-negative integer. Now we may select one number from each sequence to form a sequence with m integers. It's

POJ 2442 Sequence(堆的使用练习)

题目地址:POJ 2442 真心没想到这题的思路..原来是从第一行逐步向下加,每次都只保存前n小的数.顺便练习了下堆..不过感觉堆的这种用法用的不太多啊.. 又是手残..把j写成了i,于是就改啊改..改的跟题解上的几乎一样了= = !.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #inc

[ACM] POJ 1442 Black Box (堆,优先队列)

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7099   Accepted: 2888 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt

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

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

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

Buy Tickets+POJ+线段树单点更新的灵活运用

Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12927   Accepted: 6410 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue- The Lunar New Year wa