HDU PING PONG 树状数组+离散化处理

写在前面:

之前看了很多解题报告,都觉得他们代码是最普通的方法,如果数据过大的话,肯定会爆掉。之后请教了队长,才知道,要对数据进行离散化处理,这样能够避免数据过大已经程序的崩溃。

Ping pong

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 42   Accepted Submission(s) : 17
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
2008 Asia Regional Beijing
 

Statistic |  Submit |  Back


题目大意:

按位置顺序给你每个人的战斗力指数,两个人要进行比赛必须要找一个战斗力处于两人之间的人做裁判,裁判不同或者两个队员不同都算作是不同的比赛,问现给的人中,能够组成多少场不同的比赛。



解题思路:

裁判一定是三个人中战斗力指数中等的那个人,那么以裁判为临界点,我只要找出它的左面比他大的乘以右面比他小的,然后再加上左面比他小的乘以右面比他大的,就是该人做裁判的时候,能够组建不同种类的比赛的次数。

那么每个人都可以做裁判,所以要遍历一次。

不难看出,这是一个改动点去查询区间的题目,和求逆序数类似,所以,我们用树状数组这种数据结构来做。

每次进来一个人,每插入一个点,即更新这个点的信息,然后找左小和右大的点的个数。战斗力的数值也就决定了该点的位置。如果找右小左大的,那么就从后面开始插入。

最后,左小*右大+左大*右小,即可得出结果。

但是需要注意的是,我们现在因为数据比较小,把战斗力的数值当做该点的位置,如果位置的最大值小于战斗力的数值怎么办呢,那么就会造成数组越界的情况。所以,我们一开始将战斗力编号,在插入前,寻找该点的排名,之后插入该点的排名,即可解决这种情况。

这里大家最好好好思考一下,这是一个很重要的方法。。。。。




代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define MAX 100005
int c[MAX],a[MAX];
int zx[MAX],yd[MAX];
int zd[MAX],yx[MAX];
int s[MAX];
int binsrch(int *r,int n,int k)
{     int low,high,mid,found;
      low=1;  
	  high=n; 
	  found=0;
      while((low<=high)&&(found==0))
      {     mid=(low+high)/2;
             if(k>r[mid])  
				 low=mid+1;
             else 
				 if(k==r[mid]) 
					 found=1;
                   else   
					   high=mid-1;
       }   
     if(found==1)
              return(mid);
        else
              return(0);
}
int lowbit(int x)
{
	return x&(-x);
}
int sum(int end)
{
	int sum=0;
	while(end>0)
	{
		sum+=c[end];
		end-=lowbit(end);
	}
	return sum;
}
void updata(int pos,int num)
{
	while(pos<=MAX)
	{
		c[pos]+=num;
		pos+=lowbit(pos);
	}
}
int main()
{
	int t;
	int i,j,k;
	int n;
	__int64 sum1;
	int flag;
	 scanf("%d",&t);
	
		while(t--)
		{
		sum1=0;
		memset(a,0,sizeof(a));
		memset(c,0,sizeof(c));
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
	        s[i]=a[i];
		}
		sort(s+1,s+1+n);
		for(i=1;i<=n;i++)
		{
			flag=binsrch(s,n,a[i]);
			updata(flag,1);
			zd[i]=sum(MAX)-sum(flag);
			zx[i]=sum(flag-1);
		}
		memset(c,0,sizeof(c));
		for(j=n;j>=1;j--)
		{
			flag=binsrch(s,n,a[j]);
			updata(flag,1);
			yd[j]=sum(MAX)-sum(flag);
			yx[j]=sum(flag-1);
		}
		for(k=1;k<=n;k++)
			sum1+=zx[k]*yd[k]+zd[k]*yx[k];
		printf("%I64d\n",sum1);
		}
	

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值