[Ural1306] Sequence Median (Online a lot of puzzle, this is right!) Industry Conscience! )

Source: Internet
Author: User

The lesson of blood:

1. Do not have the best of the letter.

2. C + + STL is very pit-daddy.

Test results Analysis and comparison:

#1是我自己写的, using priority_queue, Hyper Memory:

#include <cstdio>#include<cstring>#include<algorithm>#include<queue>using namespaceStd;priority_queue<unsignedint>PQ;intMain () {unsignedintn,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

Using priority_queue, same hyper memory

#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 Don't be lazy, kid http://blog.csdn.net/yujuan_mao/article/details/7985424.

Using Priorty_queue, or hyper-memory

#include <cstdio>#include<iostream>#include<queue>#include<vector>#include<algorithm>using namespacestd;intMain () {unsignedintN,temp,i,j,k,m,x,y; Priority_queue<unsignedint, vector<unsignedint>,greater<unsignedint> >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

Using arrays and heaps, AC

#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespacestd;Const intmaxn=250000;inta[maxn/2+Ten];intMain () {intNDoubleans;  while(SCANF ("%d", &n)! =EOF) {       intnum=0, X;  for(intI=0; i<n/2+1; i++) scanf ("%d",&A[i]); Make_heap (A,a+n/2+1);  for(inti=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.

[Ural1306] Sequence Median (Online a lot of puzzle, this is right!) Industry Conscience! )

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.