Uva 1428 Ping pong (树状数组,Fenwick树)

N(3$ \le$N$ \le$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$ \le$T$ \le$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 a1a2...aN follow, indicating the skill rank of each player, in the order of west to east ( 1$ \le$ai$ \le$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个乒乓运动员住在一条街上,他们经常组织比赛切磋,每个运动员有一个能力值。每场比赛需要三个人,裁判和两个运动员,规则是裁判必须住在两个运动员中间而且能力值也必须在他们之间。问共能组织多少场比赛。
分析:题目意思很明确,对于每个裁判i,只要求出前面比ai小的数和后面比ai大的数相乘,加上后面比ai小的数加上前面比ai大的数。公式就是 c[i]*(n-i-d[i])+(i-1-c[i])*d[i],c[i]和d[i]分别表示i前后比i小的数的个数。
要求出c[i]和d[i]就要用到Fenwick树(其实就是树状数组),很典型的求逆序数方法,正反求两遍即可,复杂度O(nlogr),r指的是ai的最大值。

AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 100005;
int fwt[maxn],c[maxn],d[maxn],a[maxn];
int n,mxn;
inline int lowbit(int x){return x&(-x);}

void add(int x,int cg){
    while(x<=mxn){
        fwt[x]+=cg;
        x+=lowbit(x);
    }
}
int query(int x){
    int ans =0;
    while(x>0){
        ans+=fwt[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    int T;
    cin>>T;
    while(T--){
        scanf("%d",&n);
        mxn = -1;
        long long ans = 0;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]),mxn = max(a[i],mxn);
        memset(fwt,0,sizeof(fwt));
        for(int i=1;i<=n;i++){
            add(a[i],1);
            c[i] = query(a[i]-1);
        }
        memset(fwt,0,sizeof(fwt));
        for(int i=n;i>=1;i--){
            add(a[i],1);
            d[i] = query(a[i]-1);
        }
        for(int i=1;i<=n;i++){
            ans+=(long long)c[i]*(n-i-d[i])+(long long)(i-1-c[i])*d[i];
        }
        printf("%lld\n",ans);
    }
    return 0;
}




1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值