[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(){
    unsigned int n,i,x,s1,s2;
    scanf("%d",&n);
    if(n % 2 != 0){
        for(i=1;i<=n;i++){
            scanf("%d",&x);
            pq.push(x);
            if(pq.size() == n/2+2){
                pq.pop();
            }
        }
        printf("%.1lf\n",(double)pq.top());
    }
    else{
        for(i=1;i<=n;i++){
            scanf("%d",&x);
            pq.push(x);
            if(pq.size() == n/2+2){
                pq.pop();
            }
        }
        s1 = pq.top();
        pq.pop();
        s2 = pq.top();
        printf("%.1lf\n",(double)(s1+s2)/2);
    }
    return 0;
}

#2 from PegasusWang http://www.cnblogs.com/PegasusWang/archive/2013/03/23/2977597.html

使用priority_queue,同样超内存

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue> //for priority_queue
using namespace std;
int main()
{
priority_queue<unsigned int> ipq;
int n;
scanf("%d", &n);
unsigned int t;
for (int i = 1; i <= n / 2 + 1; ++i)
{
scanf("%u", &t);
ipq.push(t);
}
int tn = n / 2 + 2;
while (tn <= n)
{
scanf("%u", &t);
ipq.push(t);
++tn;
ipq.pop();
}
if (n & 1) //if n is odd
printf("%u.0\n", ipq.top());
else
{
int t1 = ipq.top();
ipq.pop();
printf("%.1f", (double)(t1 + ipq.top()) / 2);
}
return 0;
}

#3 from 不许偷懒啦小鬼 http://blog.csdn.net/yujuan_mao/article/details/7985424

使用priorty_queue,还是超内存

#include<cstdio>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    unsigned int n,temp,i,j,k,m,x,y;
    priority_queue<unsigned int,vector<unsigned int>,greater<unsigned int> > Q;
    cin>>n;
    for(i=0;i<n;i++){
        scanf("%u",&m);
        Q.push(m);
        if(Q.size()>(n/2+1))
            Q.pop();
    }
    if(n%2)
        printf("%.1lf\n",1.0*Q.top());
    else{
        x=Q.top();
        Q.pop();
        y=Q.top();
        printf("%.1lf\n",1.0*(x+y)/2);
    }
    return 0;
}

#4 from http://www.zgxue.com/163/1639818.html

使用数组和堆,AC

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn=250000;
int a[maxn/2+10];
int main()
{
    int n;double ans;
    while(scanf("%d",&n)!=EOF)
    {
       int num=0,x;
       for(int i=0;i<n/2+1;i++)
       scanf("%d",&a[i]);
       make_heap(a,a+n/2+1);
       for(int i=n/2+1;i<n;i++)
       {
           scanf("%d",&x);
           if(x<a[0])
           {
               pop_heap(a,a+n/2+1);
               a[n/2]=x;
               push_heap(a,a+n/2+1);
           }
       }
       if(n%2)
       {
           ans=(double)a[0];
           printf("%.1lf\n",ans);
       }
       else
       {
           ans=(double)a[0];
           pop_heap(a,a+n/2+1);
           ans+=(double)a[0];
           printf("%.1lf\n",ans/2.0);
       }

    }
    return 0;
}

The standard container adaptor priority_queue calls make_heap, push_heap and pop_heap automatically to maintain heap properties for a container.

时间: 2025-01-12 11:27:47

[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

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

三星I9308(移动版)正确?Root的方法,进入正确的recovery的关键(网上很多方法是误导)

三星I9308(移动版)正确?Root的方法,进入正确的recovery的关键(网上很多方法是误导) 1)首先在电脑上安装手机驱动:下载地址:点击这里下载 2)手机设置USB调试   方法1:设置--开发者选项--USB调试[打钓](andriod4.3版以上,关于设备--内部版本号连点7下可调出-开发者选项)  方法2:设置-应用程序(或开发人员选项)-USB调试[打钩]开启USB调试参考点这  方法3:搜索下载一个手机应用:开启USB调试  然后通过USB线连接手机,系统会自动安装好驱动!

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

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

UVA 10603 Fill(正确代码尽管非常搓,网上很多代码都不能AC)

题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1544">click here~ 此题我预计是加强过数据,在我纠结了非常久的时候我交了好几份网上的代码不是WA就是TLE.在我非常迷茫的时候我又交了一份,AC了(尽管我用随机数据找到了他代码一个不能过的数据). 给了我信心.然后我拿他的代码用随机数跟我的代码进行測试.再用FC找不同..发现了一个致命的错误.一般来说,BFS或者DFS都是须要

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

Ubuntu13.04 &nbsp; resolv.conf 总是被清除-网上很多转摘的方法没法用

Ubuntu13.04 的DNS 文件  /etc/resolv.conf  每次重启后总是会被清空, 每次去设置的话很麻烦. 网上的说法: 指定ip的方式还可以用. 但是dhcp的方式说是修改resolvconf/resolv.conf.d/base 文件就可以,经过尝试发现不可以. 实际上修改 /etc/resolvconf/resolv.conf.d/head 文件才能保证 resolv.conf  在重启后不被清空. head 文件的内容就会被写入 resolv.conf 文件. # D

ReactNative如何在JS中引用原生自定义控件(rn变化太快,网上很多教程有坑,这个我研究后可用,特意分享)

直接写一个Demo例子,有相关功底的肯定明白,会对特别的地方进行提醒,本文基于https://blog.csdn.net/lintcgirl/article/details/53489490,但是按此链接文章不可用. 首先是JAVA部分: 1 import com.facebook.react.ReactActivity; 2 3 public class MainActivity extends ReactActivity { 4 5 /** 6 * Returns the name of t