hdu5249---KPI(二分+树状数组)

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

6

in 874

query

out

in 24622

in 12194

query

Sample Output

Case #1:

874

24622

Source

2015年百度之星程序设计大赛 - 初赛(1)

Recommend

hujie | We have carefully selected several similar problems for you: 5251 5250 5247 5244 5243

用个树状数组来求中位数就行了,二分一下

然后in out 的时候更新下树状数组

/*************************************************************************
    > File Name: 1004.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年05月30日 星期六 17时32分56秒
 ************************************************************************/

#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 double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 10010;
int tree[N];
int xis[N];
int cnt;
int Stack[N << 1];
struct QUE {
    int flag;
    int op_num;
}que[N];

int BinSearch(int val) {
    int l = 1, r = cnt;
    int mid;
    while (l <= r) {
        mid = (l + r) >> 1;
        if (xis[mid] == val) {
            break;
        }
        else if (xis[mid] > val) {
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    return mid;
}

int lowbit(int x) {
    return x & (-x);
}

void add(int x, int val, int n) {
    for (int i = x; i <= n; i += lowbit(i)) {
        tree[i] += val;
    }
}

int sum(int x) {
    int ans = 0;
    for (int i = x; i; i -= lowbit(i)) {
        ans += tree[i];
    }
    return ans;
}

char str[20];

int main() {
    int n;
    int icase = 1;
    while (~scanf("%d", &n)) {
        cnt = 0;
        int cur;
        memset(tree, 0, sizeof(tree));
        for (int i = 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++);
        int s = 0, e = -1, res = 0;
        for (int i = 1; i <= n; ++i) {
            if (que[i].flag == 0) {
                ++res;
                int val = BinSearch(que[i].op_num);
                Stack[++e] = val;
                add(val, 1, cnt);
            }
            else if (que[i].flag == 1) {
                --res;
                int val = Stack[s];
                add(val, -1, cnt);
                ++s;
            }
            else {
                int l = 1, r = cnt, mid, ans;
                while (l <= r) {
                    mid = (l + r) >> 1;
                    int les = sum(mid - 1);
                    int equ = sum(mid);
                    int m = (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;
}
时间: 2024-08-07 15:50:13

hdu5249---KPI(二分+树状数组)的相关文章

11525 - Permutation(二分+树状数组)

题目链接:点击打开链接 题意:从1~k的所有排列中找到第n个排列, n由公式给出. 思路:可以发现, 这个公式就是康托展开公式(康托展开百科:点击打开链接). 那么s[i]的意思就是i个数中当前数排在第几. 如此, 可以用二分+树状数组快速求解, 和一道BC题目神似. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<st

Codeforces 374D Inna and Sequence 二分+树状数组

题目链接:点击打开链接 给定n个操作,m长的序列a 下面n个数 if(co>=0)则向字符串添加一个co (开始是空字符串) else 删除字符串中有a的下标的字符 直接在序列上搞,简单模拟 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h&

【bzoj2527】[Poi2011]Meteors 整体二分+树状数组

题目描述 有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下陨石雨.BIU已经预测了接下来K场陨石雨的情况.BIU的第i个成员国希望能够收集Pi单位的陨石样本.你的任务是判断对于每个国家,它需要在第几次陨石雨之后,才能收集足够的陨石. 输入 第一行是两个数N,M. 第二行有M个数,第i个数Oi表示第i段轨道上有第Oi个国家的太空站. 第三行有N个数,第i个数Pi表示第i个国家希望收集的陨石数量. 第四行有一个

【51nod】 第K大区间2(二分+树状数组)

[51nod] 第K大区间2(二分+树状数组) 第K大区间2 ﹡    LH (命题人) 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 定义一个长度为奇数的区间的值为其所包含的的元素的中位数.中位数_百度百科 现给出n个数,求将所有长度为奇数的区间的值排序后,第K大的值为多少. 样例解释: [l,r]表示区间的值 [1]:3 [2]:1 [3]:2 [4]:4 [1,3]:2 [2,4]:2 第三大是2 Input 第一行两个数n和k(1<=n<=100000,k&l

【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询

Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. Input 第一行N,M 接下来M行,每行形如1 a b c或2 a b c Output 输出每个询问的结果 Sample Input 2 5 1 1 2 1 1 1 2 2 2 1 1 2 2 1 1 1 2 1 2 3 Sample Output 1 2 1 HINT

【BZOJ-2527】Meteors 整体二分 + 树状数组

2527: [Poi2011]Meteors Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 831  Solved: 306[Submit][Status][Discuss] Description Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The planet is unsuitable for colo

【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改

题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. 输入 第一行N,M接下来M行,每行形如1 a b c或2 a b c 输出 输出每个询问的结果 样例输入 2 5 1 1 2 1 1 1 2 2 2 1 1 2 2 1 1 1 2 1 2 3 样例输出 1 2 1 题解 整体二分+树状数组区间修改 当年naive的树套树题解 前两天由于要

Codeforces Round #470 (Div 2) B 数学 C 二分+树状数组 D 字典树

Codeforces Round #470 B. Primal Sport 数学题,对 x2 和 x1 分解质因子即可. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b;

hdu-5493 Queue(二分+树状数组)

题目链接: Queue Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1093    Accepted Submission(s): 566 Problem Description N people numbered from 1 to N are waiting in a bank for service. They all stan