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
把数据先离散化,然后遍历,把数字依次插入到树状数组里去, 求中值二分就行了
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; }
相关文章推荐
- bzoj 3521/5083 [Poi2014]Salad Bar/普及 (卡常)st表+二分+树状数组
- ZOJ 3635——Cinema in Akiba(树状数组+二分)
- Codeforces 600B Queries about less or equal elements 【离散化去重二分查找 + 树状数组】
- PAT (Advanced Level) 1029. Median (25) 求两个有序数组的中位数,二分
- 【BZOJ】3110 [Zjoi2013]K大数查询 整体二分+树状数组 || 树套树
- 【整体二分+树状数组区间加区间和】BZOJ3110 [Zjoi2013]K大数查询
- POJ 2828 Buy Tickets(树状数组+二分)
- zoj 3635 Cinema in Akiba(树状数组+二分)
- HDU 5493 Queue 树状数组+二分
- Hdu 4217 Data Structure?【二分+树状数组】
- [二分+树状数组]51 Nod 1685——第K大区间2
- hdu 2852 KiKi's K-Number(树状数组+二分)
- POJ-2892(树状数组 + 二分)
- 51Nod-1711平均数(二分+树状数组|线段树)
- 二分树状数组-洛谷P1168 中位数
- 【POJ2104】【整体二分+树状数组】区间第k大
- uva 11525排列(树状数组 + 二分)
- [整体二分+树状数组]BZOJ 2527——[Poi2011]Meteors
- HDU P4339(树状数组+二分查询)
- codeforces 191 E (树状数组+二分)