HDU 2492 Ping pong(树状数组)

Ping pong

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5575 Accepted Submission(s): 2085

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

题意:有t组数据,每行数据的第一个数 n 表示有n个人,每个位置上的数据代表选手的技能值,现在要三个人组队,按照位置的顺序,三个人中间的人是裁判,两边的选手,裁判的技能值要跟位置一样位于两位选手之间。
解题思路:以自己为裁判,然后找左边有多少人比较小,再找右边有多少人比自己大,然后两个数相乘就是以他为裁判的比赛场数,当然还有相反的,找右边比自己小的,左边比自己大的,再相乘相加。这样子就变成了:和找逆序数、顺序数差不多了,找左边(右边)比自己大(小)有多少个数,用树状数组最好最快!所以两个for循环就全部找到,然后相乘相加。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int ma=100005;
int x[ma],xx[ma],y[ma],yy[ma],a[ma],b[ma],s[ma];
int lo(int x)
{
    return x&(-x);
}
int sum(int e)
{
    int sum=0;
    while(e>0)
    {
        sum+=b[e];
        e-=lo(e);
    }
    return sum;
}
void update(int pos,int num)
{
    while(pos<=ma)
    {
        b[pos]+=num;
        pos+=lo(pos);
    }
}
int main()
{
    int t,n,flag;
    long long int summ;
    scanf("%d",&t);
    while(t--)
    {
        summ=0;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=n;i++)//求左边比该点技能值大的数的个数,小的数的个数 
        {

            x[i]=sum(a[i]);//输入的i个数中 有 sum(a[i]) 个比a[i]小  
            xx[i]=i-1-x[i];//输入的i个数中 有 i-1-sum(a[i]) 个比a[i]大  
            update(a[i],1);
        }
        memset(b,0,sizeof(b));
        int j=1;//代表现在输入的数的个数  
        for(int i=n;i>=1;i--,j++)//求右边比该点技能值大的数的个数,小的数的个数  
        {

            y[i]=sum(a[i]);//输入a[i]后 输入的那些数中有 sum(a[i]) 个比a[i]小的  
            yy[i]=j-1-y[i];//输入a[i]后输入的那些数中有 j-1-sum(a[i] 个比a[i]大的 
            update(a[i],1);
        }
        for(int i=1;i<=n;i++)
            summ+=xx[i]*y[i]+x[i]*yy[i];
        printf("%lld\n",summ);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值