现在的位置: 首页 > 综合 > 正文

HDU 2492 Ping pong 树状数组

2018年01月20日 ⁄ 综合 ⁄ 共 2199字 ⁄ 字号 评论关闭

Ping pong

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

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

/*
HDU 2492 
枚举裁判,这样对于任意一个裁判而言,不同的比赛场数等于左边小于裁判的人数乘以右边大于裁判的人数,
再加上右边小于裁判的人数乘以左边大于裁判的人数。
*/
#include<iostream>
#include<stdio.h>
using namespace std;
#define N 100001
int kk[N],map[N],minl[N],minr[N],maxl[N],maxr[N];
int n;

void add(int x)
{
    while(x<=N)
    {
        kk[x]++;
        x=x+(x&-x);
    }
}

int query(int x)
{
    int sum=0;
    while(x>0)
    {
        sum+=kk[x];
        x=x-(x&-x);
    }
    return sum;
}

int main()
{
    int t,i,j;
    
    //freopen("test.txt","r",stdin);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(map,0,sizeof(map));
        for(i=1;i<=n;i++)
            scanf("%d",&map[i]);
            
        memset(kk,0,sizeof(kk));
        for(i=1;i<=n;i++)
        {
            add(map[i]);
            minl[i]=query(map[i]-1);//因为这里先增加左边的,先定位map[i]-1,再统计的就是左边比自己小的, 
            maxl[i]=i-minl[i]-1;
        }
            
        memset(kk,0,sizeof(kk));
        for(i=n,j=1;i>=1;i--,j++)
        {
            add(map[i]);
            minr[i]=query(map[i]-1);//因为这里先增加右边的,左边没有,
                                    //统计的就是右边比自己这个数map[i]-1还小的数 
            maxr[i]=j-minr[i]-1;
        }
        
        long long ans=0;
        for(i=1;i<=n;i++)
            ans+=minl[i]*maxr[i]+maxl[i]*minr[i];
        printf("%I64d\n",ans);
        
        
        
    }
    
    
    return 0;
} 

抱歉!评论已关闭.