zoukankan      html  css  js  c++  java
  • LA 4329 树状数组

    N <tex2html_verbatim_mark>(3$ le$N$ le$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>(1$ le$T$ le$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 ( 1$ le$ai$ le$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)场。
    #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
    ",ans);
        }
        return 0;
    }
    
    
    
    
    
  • 相关阅读:
    window 10 node.js 安装 2502 2503错误解决方法
    高清方案在手机微信上的一个奇葩问题,当字数变多,会莫名其妙的变大
    非node环境 vue-rouder 学习笔录4 命名视图
    非node环境 vue-rouder 学习笔录3 嵌套路由
    非node环境 vue-rouder 学习笔录2 路由监听和动态路由匹配
    非node环境 vue-rouder 学习笔录1
    VUE入门实例,模版组件用法
    用swiper可隐藏滑动导航和内容滑动联动
    在一次引入vue的时候使用swiper出现swiper无效情况
    [BZOJ 3230]相似子串
  • 原文地址:https://www.cnblogs.com/xiong-/p/3567101.html
Copyright © 2011-2022 走看看