UVA - 1428 Ping pong

Posted 人间失格

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
    }
}

 

以上是关于UVA - 1428 Ping pong的主要内容,如果未能解决你的问题,请参考以下文章

HDU2492 Ping pong

Python:检查 IRC 连接是不是丢失(PING PONG?)

(websocket)协议中Ping Pong,Socket通讯ping pong(长连接),心跳包

FastAPI websocket ping/pong 超时

如何使用 Python websockets 库获取“ping”调用的“pong”响应?

POJ3928 Ping pong