您的位置:首页 > 其它

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

2015-04-14 12:18 239 查看
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
;
int xis
;
int arr
;
int st
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: