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;
    }
    
    
    
    
    
  • 相关阅读:
    禁止修改的消息首部
    文章详情列表接口 小程序及 模拟器 条数 错误 浏览器正确 调大mysql 查询超时时间 同一接口,小程序环境的TTFB相比浏览器约大5倍。
    七牛云霍锴:实时音视频 SDK 设计实践
    java网页数据抓取
    IDEA properties文件中文自动转为ASCII码(properties输入中文乱码问题)
    阿里官方Java代码规范标准《阿里巴巴Java开发手册 终极版 v1.3.0》下载
    从零开始配置Ubuntu20.04 amd64 virtualenvwarapper angr环境 并做ais3_crackme的CFG图
    请求被中止: 未能创建 SSL/TLS 安全通道 解决方案
    Docker
    爬虫
  • 原文地址:https://www.cnblogs.com/xiong-/p/3567101.html
Copyright © 2011-2022 走看看