zoukankan      html  css  js  c++  java
  • hdu2492 Ping pong

    hdu2492 Ping pong 

    题意:一群乒乓爱好者居住在一条直线上,如果两个人想打比赛需要一个裁判,裁判的 位置 必须在两者之间 ,裁判的能力也必须不大于 参赛者最大的,不小于参赛者最小的

    白皮的题解:考虑 第 i 个 为裁判 的情况 如果 左边 比 a[i] 小的 人数 为 c[i],则 有  i-c[i]-1 个人 比 a[i] 大,同样 右边 比 a[i] 小 的人数为 d[i] ,则比a[i]大的人为 n - i - d[i];

    则方案 为 d[i]*( i - c[i] - 1 ) + c[i] * (n - i - d[i])

    用 x[ a[i] ] 表示 比 a[i] 小的个数,这就涉及到了 树状数组

    代码……

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <queue>
    #include <stack>
    #include <set>
    #include <string>
    using namespace std;
    typedef long long ll;
    const double ESP = 10e-8;
    const int MOD = 1000000007;
    typedef long long LL;
    const int MAXN = 20000 + 10;
    const int MAXA = 100000 + 10;
    int bit[MAXA];
    int c[MAXN];
    int d[MAXN];
    int arr[MAXN];
    
    int sum(int i){
        int s = 0;
        while(i > 0){
            s += bit[i];
            i -= i&-i;
        }
        return s;
    }
    
    void add(int i,int x){
        while(i < MAXA){
            bit[i] += x;
            i += i&-i;
        }
    }
    int main(){
    //    freopen("input.txt","r",stdin);
        int t;
        scanf("%d",&t);
        while(t--){
            int n;
            scanf("%d",&n);
            memset(bit,0,sizeof(bit));
            memset(c,0,sizeof(c));
            memset(d,0,sizeof(d));
            for(int i = 1;i <= n;i++){
                scanf("%d",&arr[i]);
                c[i] = sum(arr[i]);
                add(arr[i],1);
            }
            memset(bit,0,sizeof(bit));
            for(int i = n;i > 0;i--){
                d[i] = sum(arr[i]-1);
                add(arr[i],1);
            }
            LL ans = 0;
            for(int i = 1;i <= n;i++){
                ans += c[i]*(n-i-d[i]) + d[i]*(i-c[i]-1);
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    HDU 1140 War on Weather (三维点之间距离)
    HDU 1174 爆头(三维空间点与直线关系)
    POJ 2653 Pick-up sticks(计算几何 求线段交点)
    POJ 3792 Area of Polycubes(模拟)
    HDU 2372 El Dorado(DP)
    HDU 2985 Another lottery(水题)
    Radar Installation(POJ 1328 贪心)
    The Pilots Brothers' refrigerator(POJ2965枚举)
    Flip Game(枚举)
    Counterfeit Dollar (枚举)
  • 原文地址:https://www.cnblogs.com/hanbinggan/p/4681048.html
Copyright © 2011-2022 走看看