Address: http://acm.uestc.edu.cn/#/problem/show/1339
Topic:
learned and online gamesTime limit:6000/2000ms (java/others) Memory limit:125535/65535kb (java/others)SubmitStatus
Once in the online game confession, but the other side is a fake girl's shemale, and this has a shadow of the learned, insisted on the game and the reality of clearly. One day unexpectedly by the network game female player confession, although in the game accepted Yazi confession and married, but still not sure Yazi is really true girl. Finally, one day, Lucian belongs to the four-person association of the rich President decided to hold an offline party, the results found that four people incredibly are the same high school students, and even three other people are really cute girls.
Learned not fully accept this fact, they found that Yazi incredibly is a serious reality game confusion patients, not only in real life with the game role name "Lucian" called People, but also no one else adhesion to the British riding body. In order for Yazi to return to normal, the four-person game life continues to the school.
......
Today, the plan to save Yazi is to play the game!
This game is like this ~
There is something like a queue, you can push an element to the end of the queue, or you can pop out of the first element of the queue
Now the question is, what is the median of this queue?
The median is defined as the value of the number of k/2+1 after the queue is sorted in ascending order, and K is the size of the queue
Input
The first row is an nN, which indicates that there are N operations
Next nn behavior one of these three actions:
1 x, push a number x into this queue
2, pops up the first number of this queue
3, how much is the median of this queue queried?
1<=n<=1000000 1<=n<=1000000, ensure that the input is an integer in the int range.
Make sure that every number in the queue is different oh ~
Output
For each query output answer.
Guaranteed to have a solution ~
Sample Input and output
Sample Input |
Sample Output |
61 231 4323 |
244 |
Ideas:
The man and the online game
Start with Ching method,, and then do not write for half a day, write fried ...
Helpless under the same as the B-question himself thought.
And then found that regardless of the deletion to add a number, the position of the number of digits in the median of the original median, either left or right, or not move, so it can be classified as discussed under the good ....
Ps: Found set really good, automatic sequencing,,
It would be nice to have a queue record for the number of deletions to be added,
Well, that's it.
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cmath>5#include <cstring>6#include <queue>7#include <stack>8#include <map>9#include <Set>Ten#include <vector> One#include <cstdlib> A#include <string> - - #definePI ACOs ((double)-1) the #defineE exp (double (1)) - using namespacestd; - Set<int>p; -queue<int>Q; + Set<int>:: iterator it,tit; - intMain (void) + { A intT; atCin>>T; - while(t--) - { - intb; -scanf"%d",&a); - if(a==1) in { -scanf"%d",&b); to P.insert (b); + Q.push (b); - if(p.size () = =1) the { *it=P.begin (); $ Continue;Panax Notoginseng } - if(b > (*it) && p.size ()%2==0) theit++; + if(b < (*it) && p.size ()%2!=0) Ait--; the } + Else if(a==2) - { $tit=P.find (Q.front ()); $ if(*tit > *it && p.size ()%2==0) -it--; - Else if(*tit < *it && p.size ()%2!=0) theit++; - Else if(*tit==*it && p.size ()%2==0)Wuyiit--; the Else if(*tit==*it && p.size ()%2!=0) -it++; Wu Q.pop (); - p.erase (tit); About } $ Else - { - if(p.size () = =1) -it=P.begin (); Aprintf"%d\n",*it); + } the } - return 0; $}
View Code
Hdu1339 learned and online games