码迷,mamicode.com
首页 > 其他好文 > 详细

HDU2492 Ping pong

时间:2014-05-07 08:33:20      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:杭电   树状数组   

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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个人要进行乒乓球比赛每一个人都一个技术值,每个人出现的次序就是
他们住的位置,现在要求进行一场比赛,三个人,裁判的技术值在两个人的
中间,位置也在两个人的中间,问一共可以进行这种比赛多少次。
分析:
采用树状数组,先从左到右计算左边大的个数,左边小的个数,再从右到
做计算右边大和小的个数,然后交叉相乘取和就可以了
代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
using  namespace std;
const int MAX = 100005;
int a[MAX] , cnt[MAX] , leftMax[MAX] ;
int leftMin[MAX] ,rightMax[MAX] , rightMin[MAX];

int lowbit(int x)
{
    return x&(-x);
}

void update(int x , int y)
{
    while(x <= MAX)
    {
        cnt[x] += y;
        x += lowbit(x);
    }
}

int sum(int x)
{
    int sum = 0;
    while(x)
    {
        sum += cnt[x];
        x -= lowbit(x);
    }
    return sum;
}

int main()
{
    int t,i,j;
    scanf("%d" , &t);
    while(t--)
    {
        int n;
        scanf("%d" , &n);
        for( i = 1 ; i <= n ; i++)
            scanf("%d" , &a[i]);

        memset(cnt , 0 , sizeof(cnt));//清零
        for( i = 1 ; i <= n ; i++)
        {
            update(a[i] , 1);
			leftMin[i] = sum(a[i]-1);//计算左边小的个数
            leftMax[i] = i - leftMin[i] -1;//计算左边大的个数
		//	leftMax[i] = sum(MAX) - sum(skill[i]);//计算左边大的个数
        }
		memset(cnt , 0 , sizeof(cnt));//注意此处要再次清零
        for( i = n,j = 1 ; i >= 1 ;j++, i--)
        {
            update(a[i] , 1);
			rightMin[i] = sum(a[i]-1);//计算右边小的个数
            rightMax[i] = j - rightMin[i]-1;//计算右边大的个数
		//	rightMax[i] = sum(MAX) - sum(skill[i]);//计算左右大的个数
        }
        __int64 sum = 0;
        for( i = 1 ; i <= n ; i++)
        {
            sum += leftMax[i]*rightMin[i] + leftMin[i]*rightMax[i];//交叉相乘取和
        }
        printf("%I64d\n" , sum);
    }
    return 0;
}


HDU2492 Ping pong,布布扣,bubuko.com

HDU2492 Ping pong

标签:杭电   树状数组   

原文地址:http://blog.csdn.net/u012860063/article/details/25079445

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!