`

hdu 2492 ping pong(逆序数与顺序数)

阅读更多

Ping pong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1143    Accepted Submission(s): 387

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

         题目大意:每一个人都有一个实力值,顺序就是他的位置,要求寻找一个对手,然后再寻找一个裁判,要求裁判的实力再他们之间,而且位置要在他们两人的中间,这样可以组成一场比赛。问总共可以组织多少场比赛?
      解题思路:以自己为裁判,然后找左边有多少人比较小,再找右边有多少人比自己大,然后两个数相乘就是以他为裁判的比赛场数,当然还有相反的,找右边比自己小的,左边比自己大的,再相乘相加。这样子就变成了:和找逆序数、顺序数差不多了,找左边(右边)比自己大(小)有多少个数,用树状数组最好最快!所以两个for循环就全部找到,然后相乘相加,搞定!!!!!!!
 我的代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;
const int MAX = 100005;

int a[MAX+1], b[MAX+1];
int c[MAX+1], d[MAX+1], e[MAX+1], f[MAX+1];
int n;

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

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

int sum(int i)
{
    int sum = 0;
    while(i > 0)
    {
        sum += a[i];
        i -= lowbit(i);
    }
    return sum;
}

int main()
{
    int i, t;
    long long ans;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        memset(a, 0, sizeof(a));
        for(i = 1; i <= n; i++)
        {
            scanf("%d", &b[i]);
        }
        ans = 0;
        for(i = 1; i <= n; i++)
        {
            update(b[i], 1);
            c[i] = sum(MAX)-sum(b[i]);  //求左边有多少个数比自己大
            d[i] = sum(b[i]-1);         //求左边有多少个数比自己小
        }
        memset(a, 0, sizeof(a));
        for(i = n; i >= 1; i--)
        {
            update(b[i], 1);
            e[i] = sum(MAX)-sum(b[i]);  //求右边有多少个数比自己大
            f[i] = sum(b[i]-1);         //求右边有多少个数比自己小
        }
        for(i = 1; i <= n; i++)
        {
            ///1.左边比自己大的数*右边比自己小的数
            ///2.左边比自己小的数*右边比自己大的数
            ans += c[i]*f[i] + d[i]*e[i];   //相加就是总和
        }
        printf("%I64d\n", ans);
    }

    return 0;
}
 
1
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics