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;
    }
  • 相关阅读:
    [moka同学笔记]一、Yii2.0课程笔记(魏曦老师教程)
    [moka同学笔记]yii2.0导航栏
    [moka同学笔记]Yii2.0 dropDownList的使用(二)
    [moka同学笔记]Yii2.0验证码
    [moka同学摘录]Yii2 csv数据导出扩展
    [moka同学笔记]PHPexcel之excel导出和导入
    [moka同学代码]PHP初级知识:上传文件源码
    [moka同学摘录]在Centos 6.5下成功安装和配置了vim7.4
    [moka同学摘录]iptables防火墙规则的添加、删除、修改、保存
    [moka同学笔记]linux服务器防火墙的设置
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4245724.html
Copyright © 2011-2022 走看看