zoukankan      html  css  js  c++  java
  • UVA

    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
    ",ans);
        }
    }
  • 相关阅读:
    小程序配置 全局配置
    浅谈 Nginx和LVS的各种优缺点
    LVS负载均衡(LVS简介、三种工作模式、十种调度算法)
    用Camshift算法对指定目标进行跟踪
    AsyncTask源代码解析
    shell中的${},##和%%的使用
    hdu 1081 &amp; poj 1050 To The Max(最大和的子矩阵)
    POJ 1141 Brackets Sequence (区间DP)
    Ejb in action(六)——拦截器
    7.JAVA编程思想笔记隐藏实施过程
  • 原文地址:https://www.cnblogs.com/renjianshige/p/7470790.html
Copyright © 2011-2022 走看看