UVA - 1428 Ping pong

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.

 

题解:
  树状数组裸题吧,考虑维护c[i],表示在a[i]之前有多少人rank比a[i]小,d[i]表示a[i]之后有多少人rank比i小,考虑枚举中点i,那么对于一个裁判i对答案的贡献就显然ans+=(i-1-c[i])*d[i]+(n-i-d[i])*c[i]。(i-1-c[i]表示i之前比a[i]大的人数,n-i-d[i]同理)。那么答案就将所有的加起来就可以了,用一棵值域树状数组就可以nlogn求了。

 

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<stdio.h>
#define MAXN 100100
#define ll long long
using namespace std;
int a[MAXN],tr[MAXN],c[MAXN],d[MAXN];
int n,t,maxx;

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

int query(int x){
    int ret=0;
    while(x){
        ret+=tr[x];
        x-=lb(x);
    }
    return ret;
}

void insert(int ps,int h){
    while(ps<=maxx){
        tr[ps]+=h;
        ps+=lb(ps);
    }
}

int main(){
    scanf("%d",&t);
    while(t--){
        memset(a,0,sizeof(a));
        memset(tr,0,sizeof(tr));
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));maxx=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]),maxx=max(maxx,a[i]);
        for(int i=1;i<=n;i++){
            c[i]=query(a[i]-1);
            insert(a[i],1);
        }
        memset(tr,0,sizeof(tr));
        for(int i=n;i>=1;i--){
            d[i]=query(a[i]-1);
            insert(a[i],1);
        }
        ll ans=0;
        for(int i=1;i<=n;i++){
            ans+=(ll)(i-1-c[i])*(ll)d[i];
            ans+=(ll)(n-i-d[i])*(ll)c[i];
        }
        printf("%lld\n",ans);
    }
}

 

转载于:https://www.cnblogs.com/renjianshige/p/7470790.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值