hdu 5249區間第k大(學習了下樹狀數組的搞法),hdu5249
KPI
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 205 Accepted Submission(s): 70
Problem Description你工作以後, KPI 就是你的全部了. 我開發了一個服務,取得了很大的知名度。數十億的請求被推到一個大管道後同時服務從管頭提取要求。讓我們來定義每個請求都有一個重要值。我的KPI是由當前管道內請求的重要值的中間值來計算。現在給你服務記錄,有時我想知道當前管道內請求的重要值得中間值。
Input有大約100組資料。
每組資料第一行有一個n(1≤n≤10000),代表格服務記錄數。
接下來有n行,每一行有3種形式
"in x": 代表重要值為x(0≤x≤109)的請求被推進管道。
"out": 代表格服務拉取了管道頭部的請求。
"query: 代表我想知道當前管道內請求重要值的中間值. 那就是說,如果當前管道內有m條請求, 我想知道,升序排序後第floor(m/2)+1th 條請求的重要值.
為了讓題目簡單,所有的x都不同,並且如果管道內沒有值,就不會有"out"和"query"操作。
Output對於每組資料,先輸出一行
Case #i:
然後每一次"query",輸出當前管道內重要值的中間值。
Sample Input
6in 874queryoutin 24622in 12194query
Sample Output
Case #1:87424622
Source2015年百度之星程式設計大賽 - 初賽(1)
Recommendhujie | We have carefully selected several similar problems for you: 5251 5250 5248 5247 5246
主席樹隨便搞搞就行。聽別人說說樹狀數組也能搞,於是學習了下,果然神奇
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 1e4 + 10;int c[maxn],tot;void Modify(int x,int d){for(int i = x; i <= tot; i += i&-i) c[i] += d;}int findk(int k,int limt){int ans = 0,cnt = 0;for(int i = limt; i >= 0; i--) {ans += (1<<i);if(ans > tot || cnt + c[ans] >= k) ans -= (1<<i);else cnt += c[ans];}return ans + 1;}char Q[maxn];int date[maxn],sorted[maxn];int main(int argc, char const *argv[]){int n;int cas = 1;while(scanf("%d",&n) == 1) {printf("Case #%d:\n",cas++);int cur = 1;for(int i = 1; i <= n; i++) {char cmd[10];scanf("%s",cmd);Q[i] = cmd[0];if(Q[i]=='i') {scanf("%d",date + cur);sorted[cur] = date[cur];++cur;}}tot = cur;memset(c,0,sizeof(c[0])*tot);sort(sorted+1,sorted+cur);int ql = 0,qr = 0;for(int i = 1; i <= n; i++) {if(Q[i]=='i') {++qr;int pos = lower_bound(sorted+1,sorted+cur,date[qr]) - sorted;Modify(pos,1);}else if(Q[i] == 'o') {++ql;int pos = lower_bound(sorted+1,sorted+cur,date[ql]) - sorted;Modify(pos,-1);}else {int k = (qr - ql + 2) >> 1;printf("%d\n",sorted[findk(k,18)]);}}}return 0;}