POJ3784---Running Median(树状数组+二分)

该程序设计题目要求读取一系列32位有符号整数,并在每个奇数位置的数值后输出到目前为止的中位数。输入包含数据集数量P和每个数据集的元素数量M。解决方案是通过树状数组实现,将数值离散化并在线性时间内更新中位数。
摘要由CSDN通过智能技术生成

Description
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of the elements received so far.

Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by an odd decimal integer M, (1 ≤ M ≤ 9999), giving the total number of signed integers to be processed. The remaining line(s) in the dataset consists of the values, 10 per line, separated by a single space. The last line in the dataset may contain less than 10 values.

Output
For each data set the first line of output contains the data set number, a single space and the number of medians output (which should be one-half the number of input values plus one). The output medians will be on the following lines, 10 per line separated by a single space. The last line may have less than 10 elements, but at least 1 element. There should be no blank lines in the output.

Sample Input

3
1 9
1 2 3 4 5 6 7 8 9
2 9
9 8 7 6 5 4 3 2 1
3 23
23 41 13 22 -3 24 -31 -11 -8 -7
3 5 103 211 -311 -45 -67 -73 -81 -99
-33 24 56

Sample Output

1 5
1 2 3 4 5
2 5
9 8 7 6 5
3 12
23 23 22 22 13 3 5 5 3 -3
-7 -3

Source
Greater New York Regional 2009

把数据先离散化,然后遍历,把数字依次插入到树状数组里去, 求中值二分就行了

/*************************************************************************
    > File Name: POJ3784.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年04月14日 星期二 11时57分18秒
 ************************************************************************/

#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;

const int N = 12000;
int tree[N];
int xis[N];
int arr[N];
int st[N];
int cnt;
int n;

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

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

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

int search(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 main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        cnt = 0;
        memset(tree, 0, sizeof(tree));
        int icase;
        scanf("%d%d", &icase, &n);
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d", &arr[i]);
            xis[++cnt] = arr[i];
        }
        sort(xis + 1, xis + 1 + cnt);
        cnt = unique(xis + 1, xis + 1 + cnt) - xis - 1;
        int ret = 0;
        int low = inf, high = -1;
        for (int i = 1; i <= n; ++i)
        {
            int val = search(arr[i]);
            add(val);
            low = min(low, val);
            high = max(high, val);
            if (i == 1)
            {
                st[++ret] = xis[val];
                continue;
            }
            if (i & 1)
            {
                int l = low, r = high, mid;
                int a;
                while (l <= r)
                {
                    mid = (l + r) >> 1;
                    int num1 = sum(mid);
                    if (num1 <= i / 2)
                    {
                        l = mid + 1;
                    }
                    else
                    {
                        r = mid - 1;
                        a = mid;
                    }
                }
                st[++ret] = xis[a];
            }
        }
        printf("%d %d\n", icase, ret);
        int g = 0;
        for (int i = 1; i <= ret; ++i)
        {
            ++g;
            printf("%d", st[i]);
            if (i < ret && g < 10)
            {
                printf(" ");
            }
            else if (i < ret && g == 10)
            {
                g = 0;
                printf("\n");
            }
            else
            {
                printf("\n");
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值