HDU2492 Ping pong 树状数组的妙用

Ping pong

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

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 个乒乓球运动员,他们要举行比赛。要求是一场比赛要三个人,两个运动员一个裁判,裁判必须住在这两个运动员中间并且他的技能值也要在这两个运动员中间。问最多可以举行多少场比赛。

分析

这个问题可以简化为:找出第 i 个人(假设他是裁判)左边比他技能值小的人数 c,右边比他技能值大的人数 d,这样左边比他技能值大的人数为 (i - 1 - c),右边比他技能值小的人数为 (n - i - d),那么 c * d + (i - 1 - c) * (n - i - d) 即为这个人当裁判时能举行的比赛次数。然后剩下的计算只需要 O(n) 的复杂度就可以算出答案。

经过上面的分析,我们只需要找出第 i 个人左边比他小的个数和右边比他大的个数,可以用两个树状数组来维护前面提到的 c 和 d。树状数组中维护的是初始数组中数字出现的个数。先找第 i 个人左边比他小的个数,从左往右遍历一遍初始数组,每读到一个数 x 就在树状数组中把这个值作为下标,tree[x]++,此时对原数组来说,sum(i-1) 的值就是 i 左边比他小的数的个数,因此我们用另一个数组把 sum(i-1) 的值保存下来。后面找右边大的数的个数也是一样的操作。

代码

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 7;

int a[maxn], c[maxn], d[maxn];  // a 是初始数组,c 保存 i 左边比他小的数的个数,d 保存 i 右边比他大的数的个数
int tree[maxn];  // 用于统计的树状数组
int T, N, MAXN;  // MAXN 为树状数组的下标最大值

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

void Add(int x, int d){
    for(int i = x; i <= MAXN; i += lowbit(i)){
        tree[i] += d;
    }
}

int Sum(int x){
    int res = 0;
    for(int i = x; i > 0; i -= lowbit(i)){
        res += tree[i];
    }
    return res;
}

int main()
{
    scanf("%d", &T);
    while(T--){
        memset(tree, 0, sizeof tree);
        scanf("%d", &N);
        MAXN = 0;
        for(int i = 1; i <= N; i++){
            scanf("%d", &a[i]);
            if(a[i] > MAXN){  // 由于是把数字作为树状数组下标,所以需要保存一个最大值
                MAXN = a[i];
            }
        }
        // 统计第 i 个数左边比它小的数的个数
        int dex = 1;
        for(int i = 1; i <= N; i++){
            Add(a[i], 1);
            c[dex++] = Sum(a[i] - 1);
        }
        // 统计第 i 个数右边比它大的数的个数
        memset(tree, 0, sizeof tree);
        dex = N;
        for(int i = N; i >= 1; i--){
            Add(a[i], 1);
            d[dex--] = Sum(MAXN) - Sum(a[i]);
        }
        // 根据之前的推导求和即为答案
        LL ans = 0;
        for(int i = 1; i <= N; i++){
            ans += (LL)c[i] * d[i] + (LL)(i - 1 - c[i]) * (N - i - d[i]); 
        }
        printf("%lld\n", ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值