zoukankan      html  css  js  c++  java
  • POJ 3928 & hdu 2492 & Uva1428 PingPong 【树状数组】

    Ping pong

                                                      Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)  

    链接:hdu 2492

    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

    题意

    有N个人从左到右排成一排,每一个人有一个唯一的技能值,选出三个人——两名比赛选手另一名裁判,裁判的技能值不能比这两个人都高,也不能比这两个人都低,而且这两个人到裁判的距离总和不能大于他们之间的距离。不同的人比赛或者比赛时候的裁判不同算不同的比赛。求一共能比几场。

    分析

    将题意抽象出来,就是已知数列{aN}(3<=N<=20000)当中ai。从左往右取三个不同的数(能够不相邻)。求使这三个数排成升序或降序的取法数。

    设L为第i个人左边的人中,技能值小于他的人数, R为第i个人左边的人中,技能值小于他的人数,那么选第i个人作为裁判的方法数Ans[i]等于(L[i] * (N - i - R[i]) +(i - 1 - L[i]) * R[i])。终于输出的答案就是枚举N个人,对Ans[i]求和。那么如今的问题就是怎样求L和R数组,那么非常清晰,直接树状数组对区间求和就可以,方法类似于求逆序数。

     

    /****************************>>>>HEADFILES<<<<****************************/
    #include <set>
    #include <map>
    #include <list>
    #include <cmath>
    #include <queue>
    #include <vector>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    using namespace std;
    /****************************>>>>>DEFINE<<<<<*****************************/
    #define fst             first
    #define snd             second
    #define root            1,N,1
    #define lson            l,mid,rt<<1
    #define rson            mid+1,r,rt<<1|1
    #define PB(a)           push_back(a)
    #define MP(a,b)         make_pair(a,b)
    #define CASE(T)         for(scanf("%d",&T);T--;)
    #define FIN             freopen("input.txt","r",stdin)
    #define FOUT            freopen("output.txt","w",stdout)
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    //typedef __int64         LL;
    typedef long long       LL;
    typedef pair<int, int>   PII;
    const int INF = 0x3f3f3f3f;
    const int maxn = 100000 + 5;
    const int maxm = 20000 + 5;
    /****************************>>>>SEPARATOR<<<<****************************/
    int T, N, MAX, a[maxm];
    int S[maxn], L[maxm], R[maxm];
    inline int lowbit(int x) { return x & (-x); }
    void Add(int pos, int val)
    {
        for(; pos <= MAX; pos += lowbit(pos)) S[pos] += val;
    }
    int Query(int pos)
    {
        int ret = 0;
        for(; pos > 0; pos -= lowbit(pos)) ret += S[pos];
        return ret;
    }
    int main()
    {
    //    FIN;
        CASE(T)
        {
            scanf("%d", &N);
            memset(S, 0, sizeof(S));
            MAX = 0;
            for(int i = 1; i <= N; i++)
            {
                scanf("%d", &a[i]);
                MAX = max(MAX, a[i]);
            }
            for(int i = 1; i <= N; i++)
            {
                L[i] = Query(a[i]);
                Add(a[i], 1);
            }
            memset(S, 0, sizeof(S));
            for(int i = N; i >= 1; i--)
            {
                R[i] = Query(a[i]);
                Add(a[i], 1);
            }
            LL ans = 0;
            for(int i = 2; i < N; i++)
            {
                ans += (L[i] * (N - i - R[i]) + (i - 1 - L[i]) * R[i]);
            }
            cout << ans << endl;
        }
    }

  • 相关阅读:
    YARN架构设计详解
    HDFS文件上传
    HDFS Namenode启动过程
    (转)计算机原理学习(1)-- 冯诺依曼体系和CPU工作原理
    (转)python之from_bytes、to_bytes
    (转)Python3入门之线程threading常用方法
    (转)Python3.5 queue模块详解
    (转) argparse — 解析命令参数和选项
    (转)Python3之pickle模块
    (转)linux用户态和内核态理解
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/6874291.html
Copyright © 2011-2022 走看看