首页 > ACM题库 > HDU-杭电 > HDU 4251-The Famous ICPC Team Again-排序-[解题报告]HOJ
2015
05-23

HDU 4251-The Famous ICPC Team Again-排序-[解题报告]HOJ

The Famous ICPC Team Again

问题描述 :

When Mr. B, Mr. G and Mr. M were preparing for the 2012 ACM-ICPC World Final Contest, Mr. B had collected a large set of contest problems for their daily training. When they decided to take training, Mr. B would choose one of them from the problem set. All the problems in the problem set had been sorted by their time of publish. Each time Prof. S, their coach, would tell them to choose one problem published within a particular time interval. That is to say, if problems had been sorted in a line, each time they would choose one of them from a specified segment of the line.

Moreover, when collecting the problems, Mr. B had also known an estimation of each problem’s difficultness. When he was asked to choose a problem, if he chose the easiest one, Mr. G would complain that “Hey, what a trivial problem!”; if he chose the hardest one, Mr. M would grumble that it took too much time to finish it. To address this dilemma, Mr. B decided to take the one with the medium difficulty. Therefore, he needed a way to know the median number in the given interval of the sequence.

输入:

For each test case, the first line contains a single integer n (1 <= n <= 100,000) indicating the total number of problems. The second line contains n integers xi (0 <= xi <= 1,000,000,000), separated by single space, denoting the difficultness of each problem, already sorted by publish time. The next line contains a single integer m (1 <= m <= 100,000), specifying number of queries. Then m lines follow, each line contains a pair of integers, A and B (1 <= A <= B <= n), denoting that Mr. B needed to choose a problem between positions A and B (inclusively, positions are counted from 1). It is guaranteed that the number of items between A and B is odd.

输出:

For each test case, the first line contains a single integer n (1 <= n <= 100,000) indicating the total number of problems. The second line contains n integers xi (0 <= xi <= 1,000,000,000), separated by single space, denoting the difficultness of each problem, already sorted by publish time. The next line contains a single integer m (1 <= m <= 100,000), specifying number of queries. Then m lines follow, each line contains a pair of integers, A and B (1 <= A <= B <= n), denoting that Mr. B needed to choose a problem between positions A and B (inclusively, positions are counted from 1). It is guaranteed that the number of items between A and B is odd.

样例输入:

5
5 3 2 4 1
3
1 3
2 4
3 5
5
10 6 4 8 2
3
1 3
2 4
3 5

样例输出:

Case 1:
3
3
2
Case 2:
6
6
4

来源:http://acm.hdu.edu.cn/showproblem.php?pid=4251

题意:求数列中任意区间内的中位数;

和线段树差不多的结构, 在每一层中都可以知道中位数,把小于中位数的值放到下一层的左边,大于放在右边

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<cmath>
using namespace std;
#define M 100007

int a[20][M], toleft[20][M], sorted[M], n;
void build( int p, int l, int r )
{
	if( l == r ) return;
	int m = (l+r)>>1, lp = l, rp = m+1, num = 0;

	for( int i = l; i <= r; i++ ){
		if( a[p][i] < sorted[m] ) num++;
	}
	num = m - l + 1 - num;       //用于处理多个中位数,表示放在左边的中位数的个数
	for( int i = l; i <= r; i++ ){
		if( i == l ) toleft[p][i] = 0;
		else toleft[p][i] = toleft[p][i-1];
		if( a[p][i] < sorted[m] ){
			toleft[p][i]++;
			a[p+1][lp++] = a[p][i];
		}
		else if( a[p][i] > sorted[m] ){
			a[p+1][rp++] = a[p][i];
		}
		else if( num > 0 ){   //中位数放在左边
			toleft[p][i]++;
			a[p+1][lp++] = a[p][i];
			num--;
		}
		else a[p+1][rp++] = a[p][i]; //中位数放在右边
	}
	build( p+1, l, m );
	build( p+1, m+1, r );
}

int query( int p, int l, int r, int x, int y, int k )
{
	if( l==r ) return a[p][l];
	int s, ss, newl, newr, newk, m = (l+r)>>1;
	if( l == x ){
		s = 0;
		ss = toleft[p][y];
	}
	else{
		s = toleft[p][x-1];
		ss = toleft[p][y];
	}
	if( k <= ss - s ){
		return query( p+1, l, m, l+s, l+ss-1, k );
	}
	else{
		newl = m + 1 + x - l - s;//相对位置
		newr = m + 1 + y - l - ss;
		newk = k - ( ss - s );
		return query( p+1, m+1, r, newl, newr, newk );
	}
}
int main()
{
	int m, t = 0, x, y, k;
	while( scanf( "%d", &n ) == 1 ){
		printf( "Case %d:\n", ++t );
		for( int i = 1; i <= n; i++ ){
			scanf( "%d", &a[0][i] );
			sorted[i] = a[0][i];
		}
		sort( sorted + 1, sorted + n + 1); //排序用于求中位数
		build( 0, 1, n );
		scanf( "%d", &m );
		while( m-- ){
			scanf( "%d%d", &x, &y );
			k = (y-x)/2 + 1;
			int ans = query( 0, 1, n, x, y, k );
			printf( "%d\n", ans );
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/freezuoguan/article/details/8490643