hdu2429Ping pong

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
 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>

using namespace std;

int an[51000];
int s[110000];
const int N = 109999;
int lb(int x)
{
    return x&(-x);
}
void add(int i)
{
    if(i>N) return;
    s[i]++;
    add(i+lb(i));
}
int get(int i)
{
    if(i == 0)return 0;
    return s[i]+get(i-lb(i));
}
int taa[51000],tab[51000],tba[51000],tbb[51000];

int main()
{
    int z;
    cin>>z;
    while(z--)
    {
        int n,i,j,k;
        cin>>n;
        memset(s,0,sizeof(s));
        memset(taa,0,sizeof(taa));
        memset(tab,0,sizeof(tab));
        for(i = 1;i<=n;i++)
            scanf("%d",&an[i]);
        for(i = 1;i<=n;i++)
        {
            taa[i] = get(an[i]-1);
            tab[i] = i-1-get(an[i]);
            add(an[i]);
        }
        memset(s,0,sizeof(s));
        memset(tba,0,sizeof(tba));
        memset(tbb,0,sizeof(tbb));
        for(i = n;i>=1;i--)
        {
            tba[i] = get(an[i]-1);
            tbb[i] = n-i-get(an[i]);
            add(an[i]);
        }
        long long ans = 0;
        for(i = 1;i<=n;i++)
        {
            ans += taa[i]*tbb[i]+tab[i]*tba[i];
            ans += (i-1-taa[i]-tab[i])*(n-i)+(taa[i]+tab[i])*(n-i-tba[i]-tbb[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

posted on 2015-06-09 21:01  wos1239  阅读(112)  评论(0编辑  收藏  举报

导航