HDU 2492 Ping pong (树状数组+离散化+找规律)

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
题目大意:一个街上住着n个乒乓球爱好者。每个人有不同的技能值a1,他们经常比赛。比赛规定:两个选手,一个裁判。裁判的技能和住址必须都在两个选手之间。问这些人一共能组织多少(不重复的)比赛。
这题思路大概都一样,只是实现方式不一样。
思路:仔细想想就知道,考虑第i个人当裁判。他能组织比赛是s次。1到a(i-1)之间技能比他小的乘以a(i+1)到n之间技能比他大的,总和纪录为s1.并且, 1到a(i-1)之间技能比他大的乘以a(i+1)到n之间技能比他小的的s2. s=s1+s2. 因为ai的值各不相同。所以,可以算出s1: 1到a(i-1)之间技能比他小的(正序数)c, a(i+1)到n之间技能比他大的就等于 a(i+1)到n的总数(n-ai)减去 右边比ai小的。而右边比ai小的等于总数比ai小的个数ai-1 减去 c(正序数) 即可。s2同理。
陷阱:此题有个陷阱,如果用我这种思路,必须离散化。因为题目没说ai<N...只说ai各不相同。开始没考虑到WA十几次...仔细看题,果断离散化,一次AC...
下面上我的AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 100010
using namespace std;
typedef struct
{
    int v;
    int id;
}P;
P p[MAXN];
int c[MAXN];
int a[MAXN],b[MAXN];

int cmp(P x,P y)
{
    return x.v<y.v;
}

int lowbit(int x)
{
    return x&(-x);
}
void updata(int x,int d)
{
    while(x<MAXN)
    {
        c[x]=c[x]+d;
        x=x+lowbit(x);
    }
}
int sum(int x)
{
    int res = 0;
    while(x>0)
    {
        res=res+c[x];
        x=x-lowbit(x);
    }
    return res;
}
int main()
{
    int t;
    int n,i;
    int lef,rig,sum1,sum2;
    __int64 ans;
    scanf("%d",&t);
    while(t--)
    {
        memset(c,0,sizeof(c));
        memset(a,0,sizeof(a));
        memset(p,0,sizeof(p));
        lef=0,rig=0,sum1=0,sum2=0;
        ans=0;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
            p[i].id=i;
            p[i].v=b[i];
        }
        //离散化,WA 十几次,仔细看题,没说ai<N,只说了ai各不相同。
        //可能做范围n的题多了...所以想当然了...离散化一次就AC了
        sort(p+1,p+n+1,cmp);
        a[p[1].id]=1; 
        for(i=2;i<=n;i++)
        {
            if(p[i].v!=p[i-1].v)
            a[p[i].id]=i;
            else
            a[p[i].id]=a[p[i-1].id];
        }
        for(i=1;i<=n;i++)
        {
            updata(a[i],1);            //以当前这个人为裁判
            lef=sum(a[i]-1);           //顺序数,左边比他小
            rig=sum(n)-sum(a[i]);      //逆序数,左边比他大
            sum1=(n-a[i]-rig)*lef;             //右边比他大的
            sum2=(a[i]-1-lef)*rig;              //右边比他小的
            ans=ans+sum1+sum2;
        }
        printf("%I64d\n",ans);
    }
}

还有一个思路:见AC代码,这个是我从网上摘抄的普通解法。跟我方法效率基本一样。
#include<stdio.h>
#include<string.h>
#define max 100001
int arr[max];
int left_lower[max],left_bigger[max];
int right_lower[max],right_bigger[max];

int lowbit(int i)
{
	return i&(-i);
}

void update(int x,int i)
{
	while(x<=max)
	{
		arr[x]+=i;
		x+=lowbit(x);
	}
}

int sum(int x)
{
	int ans=0;
	while(x>0)
	{
		ans+=arr[x];
		x-=lowbit(x);
	}
	return ans;
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,i,a[max];
		scanf("%d",&n);

		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		memset(arr,0,sizeof(arr));
		for(i=1;i<=n;i++)
		{
			update(a[i],1);
			left_bigger[i]=sum(max)-sum(a[i]);
			left_lower[i]=sum(a[i]-1);
		}

		memset(arr,0,sizeof(arr));
		for(i=n;i>=1;i--)
		{
			update(a[i],1);
			right_bigger[i]=sum(max)-sum(a[i]);
			right_lower[i]=sum(a[i]-1);
		}
		__int64 res=0;
		for(i=1;i<=n;i++)
		{
		res+=left_lower[i]*right_bigger[i]+left_bigger[i]*right_lower[i];
		}
		printf("%I64d\n",res);
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值