POJ3784---Running Median(树状数组+二分)
来源:互联网 发布:debian和centos 区别 编辑:程序博客网 时间:2024/06/16 22:50
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;}
- POJ3784---Running Median(树状数组+二分)
- poj3784 Running Median
- 【优先队列:支持动态插入,寻找中位数】:poj3784,Running Median
- POJ3784 Running Median 对顶堆维护动态中位数(思维)
- hdu 3648 Median Filter 树状数组+二分查找+优化
- poj2892 树状数组+二分
- poj2182(二分+树状数组)
- hdu5493 树状数组+二分
- hdu5493Queue 树状数组+二分
- hdoj5493【树状数组+二分】
- HDU2852【树状数组+二分】
- Codeforces846D(二分+树状数组)
- CodeForces540E【树状数组+二分】
- TOJ 4074 Running Laps -- 树状数组
- zoj 3279【树状数组+二分】
- POJ 2892 树状数组+二分
- HDU 4311 树状数组+二分
- poj 2892 树状数组 二分
- 租车系统改进版
- c语言之回调函数
- NYOJ 925 国王的烦恼
- android listview适配器里区分不同按钮的点击事件
- 时间按季、月、日、时、分划分数据
- POJ3784---Running Median(树状数组+二分)
- memory---linear address space
- Java程序设计(选择)
- 100!(100的阶乘)后面有几个0问题
- Android SDK 在线更新镜像服务器资源
- linux dd指令用法中参数bs,count
- Android平台上的JNI技术介绍
- gitlab rpm安装
- Linux 内核编译