码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2492 pingpang

时间:2014-08-23 15:27:21      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:acm   算法   二叉索引树   树状数组   hdu   

Problem Description
N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). 

Each player has a unique skill rank. To improve their skill rank, they often compete with each other. If two players want to compete, they must choose a referee among other ping pong players and hold the game in the referee‘s house. For some reason, the contestants can’t choose a referee whose skill rank is higher or lower than both of theirs.

The contestants have to walk to the referee’s house, and because they are lazy, they want to make their total walking distance no more than the distance between their houses. Of course all players live in different houses and the position of their houses are all different. If the referee or any of the two contestants is different, we call two games different. Now is the problem: how many different games can be held in this ping pong street?
 

Input
The first line of the input contains an integer T(1<=T<=20), indicating the number of test cases, followed by T lines each of which describes a test case.


Every test case consists of N + 1 integers. The first integer is N, the number of players. Then N distinct integers a1, a2 … aN follow, indicating the skill rank of each player, in the order of west to east. (1 <= ai <= 100000, i = 1 … N).
 

Output
For each test case, output a single line contains an integer, the total number of different games.
 

Sample Input
1 3 1 2 3
 

Sample Output
1
 

Source
一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术,每个人都有一个不同的技能值a(i)。每场比赛需要3个人:两名选手,一名裁判。他们有一个奇怪的规定,即裁判必须住在两名选手的中间,并且技能值也在两名选手之间。问一共能组织多少种比赛。
【输入格式】输入第一行为数据组数T(1<=T<=20)每组数据占一行,首先是整数n(3《=你《=20000),然后是n个不同的整数,即a(1),a(2)……a(n)(1<=a(i)<=100000),按照住所从左到右的顺序给出每个乒乓爱好者的技能值。
【输出格式】对于每组数据,输出比赛总数的值。
【分析】我们考虑第i个人当裁判的情形。假设a(1)到a(i-1)中有c(i)个比a(i)小,那么就有i-1-c(i)个比a(i)大;同理,假设a(i+1)到a(n)中有d(i)个比a(i)小,那么就是n-i-d(i)个比a(i)大,根据乘法原理和加法原理,i当裁判时有c(i)*(n-i-d(i))+d(i)*(i-1-c(i))种比赛。这样问题就转换为了求c(i)和d(i)。
c(i)可以这么求,从左到右扫描所有的a(i),令x[j]表示目前为止已经考虑过的所有a(i)中是否存在一个a(i)=j(x[j]=0表示不存在,=1表示存在),则c(i)就是前缀和x[1]+x[2]+...x[a(i)-1]。]
这道题就是树状数组的典型应用,当然也可以用线段树解,但是代码冗长效率还不高,下面贴上我写的代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define A 100010
#define N 20010
int C[A],c[N],d[N];
int a[N];
int maxa=0;
int max(int a,int b)
{
	if(a>b) return a;
	return b;
}
int lowbit(int x)
{
	return x&-x;
}
int sum(int x)
{
	int ret=0;
	while(x>0){
		ret+=C[x];
		x-=lowbit(x);
	}
	return ret;
}
void add(int x,int d)
{
	while(x<=maxa){//注意不是n,是a[]中最大的那个值 
		C[x]+=d;
		x+=lowbit(x);
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		memset(C,0,sizeof(C));
		memset(a,0,sizeof(a));
		memset(c,0,sizeof(c));
		memset(d,0,sizeof(d));
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			maxa=max(maxa,a[i]);
		}
		for(int i=1;i<=n;i++){
			add(a[i],1);
			c[i]=sum(a[i]-1);//左边比a[i]小的人的个数 
		}
		memset(C,0,sizeof(C));
		for(int i=n;i>=1;i--){
			add(a[i],1);
			d[i]=sum(a[i]-1);//右边比a[i]小的人的个数 
		}
		long long ans=0;
		for(int i=1;i<=n;i++){
			ans+=(long long)(c[i]*(n-i-d[i]))+(long long)(d[i]*(i-1-c[i]));
		}
		printf("%I64d\n",ans);
	}
	return 0;
}
/*
3
3 1 2 3
5 1 4 9 8 2
5 1 3 2 4 5
*/



HDU 2492 pingpang

标签:acm   算法   二叉索引树   树状数组   hdu   

原文地址:http://blog.csdn.net/wangxiaoyan381/article/details/38777927

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!