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
There are about 100 sets of data.
The first row of each group of data has an n (1≤n≤10000) representing the number of service records.
Next, there are n rows, each of which has 3 forms.
"In X": A request that represents an important value of X (0≤x≤109) is 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 the important value of the request for the first floor (M/2) +1th in ascending order.
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 one line is output
Case #i:
Then each time "query", output the middle value of important values in the current pipeline.
Sample Input
6
In 874
Query
Out
In 24622
In 12194
Query
Sample Output
Case #1:
874
24622
Source
2015 Baidu Star Program Design Competition-Preliminary (1)
Recommend
Hujie | We have carefully selected several similar problems for you:5251 5250 5247 5244 5243
Use a tree-like array to find the median, two minutes.
Then update the tree-like array when in the
/************************************************************************* > File name:1004.cpp > Author: ALex > Mail: [email protected] > Created time:2015 May 30 Saturday 17:32 56 seconds *********************************** *************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Static Const intN =10010;intTree[n];intXis[n];intCntintStack[n <<1];structQUE {intFlagintOp_num;} Que[n];intBinsearch (intVal) {intL =1, r = CNT;intMid while(L <= R) {mid = (L + r) >>1;if(Xis[mid] = = val) { Break; }Else if(Xis[mid] > val) {R = mid-1; }Else{L = mid +1; } }returnMid;}intLowbit (intx) {returnX & (-X);}voidAddintXintValintN) { for(inti = x; I <= N; i + = Lowbit (i)) {Tree[i] + = val; }}intSumintx) {intAns =0; for(inti = x; I I-= Lowbit (i)) {ans + = tree[i]; }returnAns;}Charstr[ -];intMain () {intNintIcase =1; while(~scanf("%d", &n)) {cnt =0;intCurmemset(Tree,0,sizeof(tree)); for(inti =1; I <= N; ++i) {scanf('%s ', str);if(str[0] ==' I ') {scanf("%d", &cur); XIS[++CNT] = cur; Que[i].op_num = cur; Que[i].flag =0;//In num}Else if(str[0] ==' O ') {Que[i].flag =1;// out}Else{Que[i].flag =2;//Query}} sort (XIs +1, XIs + cnt +1); CNT = unique (XIs +1, XIs +1+ CNT)-XIs-1;printf("Case #%d:\n", icase++);ints =0, E =-1, res =0; for(inti =1; I <= N; ++i) {if(Que[i].flag = =0) {++res;intval = Binsearch (que[i].op_num); Stack[++e] = val; Add (Val,1, CNT); }Else if(Que[i].flag = =1) {--res;intval = Stack[s]; Add (Val,-1, CNT); ++s; }Else{intL =1, R = cnt, Mid, ans; while(L <= R) {mid = (L + r) >>1;intLes = SUM (Mid-1);intequ = SUM (mid);intm = (Res >>1) +1;if(Les < m && equ >= m) {ans = mid; Break; }Else if(Equ < m) {L = mid +1; }Else{R = mid-1; } }printf("%d\n", Xis[ans]); } } }return 0;}
hdu5249---KPI (binary + Tree array)