zoukankan      html  css  js  c++  java
  • hdu2429Ping pong

    Problem Description
    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.
     
    Sample Input
    1 3 1 2 3
     
    Sample Output
    1
     
    Source
     
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    
    using namespace std;
    
    int an[51000];
    int s[110000];
    const int N = 109999;
    int lb(int x)
    {
        return x&(-x);
    }
    void add(int i)
    {
        if(i>N) return;
        s[i]++;
        add(i+lb(i));
    }
    int get(int i)
    {
        if(i == 0)return 0;
        return s[i]+get(i-lb(i));
    }
    int taa[51000],tab[51000],tba[51000],tbb[51000];
    
    int main()
    {
        int z;
        cin>>z;
        while(z--)
        {
            int n,i,j,k;
            cin>>n;
            memset(s,0,sizeof(s));
            memset(taa,0,sizeof(taa));
            memset(tab,0,sizeof(tab));
            for(i = 1;i<=n;i++)
                scanf("%d",&an[i]);
            for(i = 1;i<=n;i++)
            {
                taa[i] = get(an[i]-1);
                tab[i] = i-1-get(an[i]);
                add(an[i]);
            }
            memset(s,0,sizeof(s));
            memset(tba,0,sizeof(tba));
            memset(tbb,0,sizeof(tbb));
            for(i = n;i>=1;i--)
            {
                tba[i] = get(an[i]-1);
                tbb[i] = n-i-get(an[i]);
                add(an[i]);
            }
            long long ans = 0;
            for(i = 1;i<=n;i++)
            {
                ans += taa[i]*tbb[i]+tab[i]*tba[i];
                ans += (i-1-taa[i]-tab[i])*(n-i)+(taa[i]+tab[i])*(n-i-tba[i]-tbb[i]);
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Discuz X3.2 分区 gid 完美伪静态方法 Apache/Nginx
    如何批量转换 WordPress 文章分类
    修改 WordPress 文件上传目录
    如何验证 jemalloc 优化 Nginx 是否生效
    .Net Core 1.1 + CentOs 7 环境配置
    DotNet Core中使用dapper
    爬虫 HttpHelper
    iTextSharp 不适用模板 代码拼接PDF
    .Net中的加密解密
    .Net Core使用 MiniProfiler 进行性能分析
  • 原文地址:https://www.cnblogs.com/wos1239/p/4564554.html
Copyright © 2011-2022 走看看