Median number of unordered arrays (Set+deque) hdu5249

Source: Internet
Author: User
Tags strcmp

Kpi

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 901 Accepted Submission (s): 398

Problem description After you work, KPIs are all you have. I have developed a service that has gained a lot of popularity. Billions of of requests are pushed to a large pipeline and the service pulls requests from the pipe header at the same time. Let's define each request to have an important value. My KPI is calculated by the intermediate value of the important value requested within the current pipeline. Now I'm going to give you a service record, and sometimes I want to know the important worth of requests in the current pipeline. Input has about 100 sets of data.

The first row of each group of data has onen(1≤n≤10000) , representing the number of service records.

Next, there are n rows, each of which has 3 forms.
"In X": represents an important value ofx(0≤x≤9) The request was pushed into the pipeline.
"Out": the request to pull the pipe header on behalf of the service.
"Query: On behalf of I want to know the middle value of the request important value in the current pipeline. That is to say, if there are M requests in the current pipeline, I would like to know, ascending sort after the firstf lo or (m / 2) +1th Important value of the request.

To make the problem simple, all x is different, and if there are no values in the pipeline, there will be no "out" and "query" operations.
Output for each set of data, first a row

Case #i:
Then each time "query", output the middle value of important values in the current pipeline. Sample input6in 874queryoutin 24622in 12194query sample outputcase #1:87424622

Analysis: The topic said that the value of the addition and the value of the extraction, the first can be used to maintain a queue request with a double-ended queue, that is, FIFO, with Q.front () and Q.pop_front () read and delete the queue header elements, with Q.back () and Q.push_back () to read and join the team tail elements, In order to maintain the queue can be used in the Set container, respectively, the establishment of two Containers SA and SB, whether deleting and adding elements to ensure that size (SA) ==size (SB) or size (SB) +1==size (SB), then Sb.begin () is the median value

Program:

1#include"stdio.h"2#include"string.h"3#include"stdlib.h"4#include"algorithm"5#include"Queue"6#include"math.h"7#include"iostream"8#include"Vector"9 #defineM 100009Ten #defineINF 0x3f3f3f3f One #defineEPS 1e-9 A #definePI ACOs (-1.0) -#include"Map" -#include"Vector" the#include"Set" -#include"string" - using namespacestd; - intMain () + { -     intn,kk=1; +      while(SCANF ("%d", &n)!=-1) A     { at         Set<int>SA,SB; -Sa.insert (-1); -Sb.insert (1000000001); -deque<int>Q; -printf"Case #%d:\n", kk++); -          while(n--) in         { -             Charstr[111]; toscanf"%s", str); +             if(strcmp (str,"inch")==0) -             { the                 intK; *scanf"%d",&k); $ Q.push_back (k);Panax Notoginseng Sa.insert (k); -                 intLa=sa.size (); the                 intlb=sb.size (); +                 intu=*(Sa.rbegin ()); A                 intv=*(Sb.begin ()); the                 if(la>lb) +                 { - sa.erase (u); $ sb.insert (u); $                 } -u=*(Sa.rbegin ()); -v=*(Sb.begin ()); the                 if(u>v) -                 {Wuyi Sb.erase (v); the Sa.insert (v); - sa.erase (u); Wu sb.insert (u); -                 } About             } $             Else if(strcmp (str," out")==0) -             { -  -                 intu=Q.front (); A Q.pop_front (); + sa.erase (u); the sb.erase (u); -  $                 intLa=sa.size (); the                 intlb=sb.size (); the                 if(la+2==lb) the                 { theSa.insert (*(Sb.begin ())); -Sb.erase (*(Sb.begin ())); in                 } the                 Else if(la==lb+1) the                 { AboutSb.insert (*(Sa.rbegin ())); theSa.erase (*(Sa.rbegin ())); the                 } the             } +             Else -             { theprintf"%d\n",*(Sb.begin ()));Bayi             } the         } the     } -     return 0; - } the /* the  the  + the  in one -  in the  in the  in the  in94  in the Query the  the */
View Code

Median number of unordered arrays (Set+deque) hdu5249

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.