zoukankan      html  css  js  c++  java
  • POJ 3928 & HDU 2492 Ping pong(树阵评价倒数)

    主题链接:

    PKU:http://poj.org/problem?id=3928

    HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492


    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


    PS:
    分别求每个数的左右两边比它大的个数和小的个数!最后再交叉相乘就可以!

    代码例如以下:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int maxn=100017;
    int n;
    int a[maxn], c[maxn];
    int leftMax[maxn], leftMin[maxn];
    int rightMax[maxn], rightMin[maxn];
    typedef __int64 LL;
    
    int Lowbit(int x) //2^k
    {
        return x&(-x);
    }
    
    void update(int i, int x)//i点增量为x
    {
        while(i <= maxn)//注意此处
        {
            c[i] += x;
            i += Lowbit(i);
        }
    }
    int sum(int x)//区间求和 [1,x]
    {
        int sum=0;
        while(x>0)
        {
            sum+=c[x];
            x-=Lowbit(x);
        }
        return sum;
    }
    
    int main()
    {
        int t;
        int n;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(int i = 1; i <= n; i++)
            {
                scanf("%d",&a[i]);
            }
            memset(c,0,sizeof(c));
            for(int i = 1; i <= n; i++)
            {
                leftMin[i] = sum(a[i]);//计算左边小的个数
                leftMax[i] = i-leftMin[i]-1;//计算左边大的个数
                update(a[i],1);
            }
            memset(c,0,sizeof(c));//再次清零
            for(int i = n,j = 1; i >= 1; i--,j++)
            {
                rightMin[i] = sum(a[i]);
                rightMax[i] = j-rightMin[i]-1;
                update(a[i],1);
            }
            LL ans = 0;
            for(int i = 1; i <= n; i++)
            {
                ans+=leftMax[i]*rightMin[i] + leftMin[i]*rightMax[i];//交叉相乘取和
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
    



    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    ios自动布局
    Android真机调试流程
    Window phone应用中的多触点手势解读以及toolKit.dll和Microsoft.Phone.dll 冲突问题
    软件质量可视化与软件测试
    软件测试中不可忽视的 Warning
    软件测试作业1 -- 关于c++项目中类相互调用的问题与解决
    软件测试 总结
    系统测试,集成测试,单元测试的联系与区别
    白盒测试
    关于UI测试的一些实例操作
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4707824.html
Copyright © 2011-2022 走看看