HDU 2492 Ping pong

Ping pong

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


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
 

 

Recommend
gaojie
 
//树状数组+二分查找、也可用2次树状数组
//思路、先排序,然后用树状数组求前面比他大的和比他小的、二分查找求总的比他大和总的比他小的

#include <iostream>
#include <stdio.h>
#include <string.h>
#define Y 100003
#include <algorithm>
using namespace std;
int c[Y];
int low[Y];
int a[20003],b[20003];
void updata(int i)
{
    for(;i<=100000;i+=low[i])
       c[i]+=1;
}
int rs(int i)
{
    int sum;
    for(sum=0;i>0;i-=low[i])
       sum+=c[i];
    return sum;
}
int n;
int bf(int x)
{
    int l=1,r=n,m;
    while(l<=r)
    {
        m=(l+r)>>1;
        if(b[m]>x) r=m-1;
        else if(b[m]<x) l=m+1;
             else return m;
    }
}
int main()
{
    int i,k;
    for(i=1;i<=100000;i++) low[i]=i&-i;
    int T;
    __int64 sum;
    int lmin,rmin,lmax,rmax;
    scanf("%d",&T);
    while(T--)
    {
        sum=0;
       scanf("%d",&n);
       for(i=1;i<=n;i++)
        scanf("%d",&a[i]),b[i]=a[i];
       sort(b+1,b+n+1);
      memset(c,0,sizeof(c));//我把sizeof(c)写成sizeof(0),坑爹呀、、、
       for(i=1;i<=n;i++)
       {
          k=bf(a[i]);
          lmin=rs(a[i]);
          lmax=i-1-lmin;
          rmin=k-1-lmin;
          rmax=n-k-lmax;
          sum+=lmin*rmax+lmax*rmin;
          updata(a[i]);
       }
       printf("%I64d\n",sum);
    }
    return 0;
}

你可能感兴趣的:(ping)