hdu 5249區間第k大(學習了下樹狀數組的搞法),hdu5249

來源:互聯網
上載者:User

hdu 5249區間第k大(學習了下樹狀數組的搞法),hdu5249

KPITime 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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.