zoukankan      html  css  js  c++  java
  • POJ3928 Pingpong(统计比 K 小的个数 + 树状数组)

    Ping pong
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2691   Accepted: 996

    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
    题意:n个乒乓球爱好者,进行比赛。每个人都有一个技能值 ai。每场比赛需要 3 个人:两名选手,一名裁判。他们有一个奇怪的规定,即裁判必须住在两名选手之间,并且技能值也介于两名选手之间,问一共能组织多少种比赛
    分析: 枚举裁判k,看看k前面有多小比他小,后面比他大 或者 前面有多少比他大后面有多少比他小,乘加
    树状数组解决统计k后面有多少比他大的数
    解决方案:每个数用一个结构体{id和value}表示,按照value从小到大排序,然后使让后面大的id加1,即可
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int Max = 20000 + 10;
    typedef long long LL;
    struct Node
    {
        int id,value;
    };
    Node data[Max];
    int n,c[Max];
    int cmp(Node x, Node y)
    {
        return x.value < y.value;
    }
    int lowbit(int k)
    {
        return k & (-k);
    }
    LL sum(int k)
    {
        LL ans = 0;
        while(k > 0)
        {
            ans += c[k];
            k -= lowbit(k);
        }
        return ans;
    }
    void modify(int k, int y)
    {
        while(k <= n)
        {
            c[k] += y;
            k += lowbit(k);
        }
    }
    int main()
    {
        int t;
        scanf("%d", &t);
        while (t--)
        {
            scanf("%d", &n);
            for(int i = 1; i <= n; i++)
            {
                scanf("%d", &data[i].value);
                data[i].id = i;
            }
            sort(data + 1, data + 1 + n, cmp);
            memset(c, 0, sizeof(c));
            LL l, r, ans = 0;
            for(int i = 1; i <= n; i++)
            {
                l = sum(data[i].id); //比 data[i].value小的个数
                r = sum(n) - l;  //总共比data[i].value 小的个数 - 左边比他小的个数 == 右边比他小的个数
                ans += (l * (n - data[i].id - r)) + (r * (data[i].id - 1 - l));
                modify(data[i].id, 1);  //本来一直觉着是修改id + 1的值,不是,就是修改id值,修改id + 1就要sum(id - 1),id - 1可以是0
            }
            printf("%I64d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    Python_数据类型与变量
    啦啦啦
    Java开发环境搭建
    TCP/IP 学习 --- 4(linux网络基础api)
    TCP/IP 学习 --- 3 (流量控制和拥塞控制)
    TCP/IP 学习 --- 2
    TCP/IP 学习记录 -- 1
    多线程
    如何解析xml文件
    Redis
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/5244241.html
Copyright © 2011-2022 走看看