zoukankan      html  css  js  c++  java
  • 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

    inputoutput
    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
    ",(a+b)/2.0);
            }
            else
            {
                unsigned int a=q.top();
                printf("%.1lf
    ",a*1.0);
            }
        }
        return 0;
    }
  • 相关阅读:
    AFO NOI2018退役——菜鸡一直是菜鸡
    NOI前总结
    洛谷3732:[HAOI2017]供给侧改革——题解
    BZOJ4037:[HAOI2015]数字串拆分——题解
    洛谷4717:【模板】 快速沃尔什变换——题解
    BZOJ3192:[JLOI2013]删除物品——题解
    BZOJ2288:[POJ Challenge]生日礼物——题解
    BZOJ1150:[APIO/CTSC2007]数据备份——题解
    BZOJ3155:Preprefix sum——题解
    Codility---FrogRiverOne
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4245724.html
Copyright © 2011-2022 走看看