• poj 3928 Ping pong


    Ping pong
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4260   Accepted: 1407

    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

    首先简要说明题意,给定一个不存在重复数的序列,请问其中,有多少种可能,挑三个数出来,按位置排列,满足单增或者单减

    首先我们先考虑,要求出每个数与它前面的数构成的顺序对数a,逆序对数b;与它后面的数构成的顺序对数c,逆序对数d

    ans=a*c+b*d;

    因为前面的一个顺序对与后面的一个顺序对 重合一个数,三个数,刚好满足题目的条件。

    这里再解释一下如何求a,b,c,d:

    先把所有数排序并且记录位置。

    然后我们只需要将数从大到小,每个数所在的位置添加1,然后对于这个数的b和c只需要分别统计前缀和与后缀和即可

    反向从小到大可以求出a,d

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=20005;
    struct A
    {
        int z,id;
        bool operator<(const A a)const
        {
            return z<a.z;
        }
    }a[N];
    int c[N],jl[N][2],n;
    void add(int x)
    {
        for(;x<=n;x+=x&-x)
            ++c[x];
    }
    int askk(int x)
    {
        int re=0;
        for(;x;x-=x&-x)
            re+=c[x];
        return re;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            long long ans=0;
            scanf("%d",&n);
            for(int i=1;i<=n;++i)
                scanf("%d",&a[a[i].id=i].z);
            sort(a+1,a+n+1);
            memset(c,0,sizeof(c));
            for(int i=1;i<=n;++i)
            {
                add(a[i].id);
                jl[i][0]=askk(a[i].id-1);
                jl[i][1]=askk(n)-askk(a[i].id);
            }
            memset(c,0,sizeof(c));
            for(int i=n;i;--i)
            {
                add(a[i].id);
                ans+=(long long)jl[i][1]*askk(a[i].id-1);
                ans+=(long long)jl[i][0]*(askk(n)-askk(a[i].id));
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    常用模板
    pascal 的字符串操作
    war2 洛谷模拟赛day2 t3 状压
    状压搜索 洛谷T47092 作业
    Milking Order
    洛谷九月月赛T1 思考
    C数列下标 牛客OI赛制测试赛2
    钓鱼 洛谷p1717
    机房人民大团结(DP)
    Spark的Straggler深入学习(2):思考Block和Partition的划分问题——以论文为参考
  • 原文地址:https://www.cnblogs.com/bzmd/p/9379670.html
Copyright © 2020-2023  润新知