首页 > 其他 > 详细

LA 4329 树状数组

时间:2014-02-26 02:04:28      阅读:255      评论:0      收藏:0      [点我收藏+]

N <tex2html_verbatim_mark>(3bubuko.com,布布扣Nbubuko.com,布布扣20000) <tex2html_verbatim_mark>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 <tex2html_verbatim_mark>(1bubuko.com,布布扣Tbubuko.com,布布扣20) <tex2html_verbatim_mark>, indicating the number of test cases, followed by T <tex2html_verbatim_mark>lines each of which describes a test case.

Every test case consists of N + 1 <tex2html_verbatim_mark>integers. The first integer is N <tex2html_verbatim_mark>, the number of players. Then N <tex2html_verbatim_mark>distinct integers a1, a2...aN <tex2html_verbatim_mark>follow, indicating the skill rank of each player, in the order of west to east ( 1bubuko.com,布布扣aibubuko.com,布布扣100000 <tex2html_verbatim_mark>, i = 1...N <tex2html_verbatim_mark>).

 

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的左边比他实力小的人数ci,统计右边比他实力小的人数di,那么
裁判i的形成的比赛有ci(n-di-i)+di(i-ci-1)场。
bubuko.com,布布扣
#include <iostream>
#include <cstdio>
using namespace std;

inline int lowbit(int x){ return x&(-x);}
const int maxn=20005;
int c[maxn],d[maxn],A[maxn];

struct FenWickTree{
    int n,C[100005];
    void resize(int n){ this->n=n;}
    void clear(){fill(C,C+n+1,0);}

    void add(int x,int d){
        while(x<=n){
            C[x]+=d;x+=lowbit(x);
        }
    }

    int sum(int x){
        int ret=0;
        while(x>=1){
            ret+=C[x];x-=lowbit(x);
        }
        return ret;
    }
}f;

int main()
{
    int T,i,cmax,n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        cmax=0;
        for(i=1;i<=n;i++){ scanf("%d",A+i);cmax=cmax<A[i]?A[i]:cmax;}
        f.resize(cmax);
        f.clear();
        for(i=1;i<=n;i++){ f.add(A[i],1);c[i]=f.sum(A[i]-1);}
        f.clear();
        for(i=n;i>=1;i--){ f.add(A[i],1);d[i]=f.sum(A[i]-1);}
        long long ans=0;
        for(i=2;i<n;i++) ans+=(long long)d[i]*(i-c[i]-1)+(long long)c[i]*(n-i-d[i]);
        printf("%lld\n",ans);
    }
    return 0;
}
bubuko.com,布布扣

 

LA 4329 树状数组

原文:http://www.cnblogs.com/xiong-/p/3567101.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!