HDU 2492 PingPong (树状数组)

问题描述:
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个队员,每个人的战斗力给出。要进行一场比赛,需要一个裁判,裁判的战斗力必须处于两个人中间。问能组成几场比赛。裁判不同,或者比赛的队员有一个不同就认为是不同的比赛。

思路分析:

树状数组。
我一开始想的是。枚举裁判,找出有多少比他大的有多少比他小的。
想乘就是答案了。
不过写了一发wa了。没有找到原因。
后来看了看题解。思路是没问题的。
我们可以求出在这个数,左边有几个比他小的。那么左边的总数(i-1)减去比他小的就是比他大的。i-b[i]-1。
倒着来一遍求出右边有多少比他小的。同理,比他大的就是n-i-c[i]。
那么答案就是:b[i]*(n-i-c[i])+(i-b[i]-1)*c[i]。
挺好的一个题。
这个可以不用离散化,如数据过大,可以使用离散化来优化程序。

在下面附上我wa的代码,希望有大神找到错误,可以指出来。

wa的代码:

#include<bits/stdc++.h>
using namespace std;
#define N 100000
int e[2*N+5];
int n,m,maxx,T;
struct Node
{
    int num;
    int NUM;
    int Max,Min;
    void get_NUM()
    {
        NUM=maxx+1-num;
    }
}a[N+5];
bool com1(Node a,Node b)
{
    return a.num<b.num;
}
bool com2(Node a,Node b)
{
    return (maxx+1-a.num)<(maxx+1-b.num);
}
int lowbit(int x)
{
    return x&(-x);
}
int get_sum(int x)
{
    int sum=0;
    for(;x>0;x-=lowbit(x))
    {
        sum+=e[x];
    }
    return sum;
}
void update(int x,int v)
{
    for(;x<=maxx;x+=lowbit(x))
    {
        e[x]+=v;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>T;
    while(T--)
    {
        int i,j,k;
        cin>>n;
        for(i=0;i<n;i++)
        {
            cin>>a[i].num;
            maxx=max(maxx,a[i].num);
        }
        sort(a,a+n,com1);
        memset(e,0,sizeof(e));
        for(i=0;i<n;i++)
        {
            update(a[i].num,1);
            a[i].Min=get_sum(a[i].num-1);
        }
        memset(e,0,sizeof(e));
        sort(a,a+n,com2);
        for(i=0;i<n;i++)
        {
            a[i].get_NUM();
            update(a[i].NUM,1);
            a[i].Max=get_sum(a[i].NUM-1);
        }
        long long sum=0;
        for(i=0;i<n;i++)
        {
            sum+=a[i].Max*a[i].Min;
        }
        cout<<sum<<endl;
    }
    return 0;
}

ac代码:

#include <cstdio>
#include <algorithm>
using namespace std;

int sum[100001],a[20001],b[20001],c[20001],n,mn;

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

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

void add(int x)
{
    while(x<=mn)
    {
        sum[x]++;
        x+=lowbit(x);
    }
}

int main()
{
    int T,i;

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d",&n);

        mn=0;

        for(i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            mn=max(a[i],mn);
        }

        for(i=0; i<=mn; i++) sum[i]=0;

        for(i=1; i<=n; i++)
        {
            add(a[i]);
            b[i]=query(a[i]-1);//求出第i个人的前面rank比他小的人数,存入数组b中
        }

        for(i=0; i<=mn; i++) sum[i]=0;

        for(i=n; i>=1; i--)
        {
            add(a[i]);
            c[i]=query(a[i]-1);//求出第i个人的后面rank比他小的人数,存入数组c中
        }

        long long ans=0;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值