首页 > 代码库 > hdu 2492(树状数组)

hdu 2492(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492

Ping pong

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


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
2008 Asia Regional Beijing
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  2485 2491 2490 2489 2488 
题意:有n个参赛选手位于一条直线上,每个选手有自己的一个技能值,每两个选手之间进行一场比赛需要到一个裁判家里去,并且裁判只能位于这两个选手的中间,问最后最多能有多少场比赛;

思路:再分析一下问题,相当于如果一个选手作为裁判,那么只要求出他左边有多少个比他技能值大的选手,求出右边有多少个比他技能值小的选手个数相乘,再加上,左边有多少个比他技能值小的选手,右边有多少个比他技能值大的选手相乘;

所以可以考虑用树状数组来求;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
const int N=20000+1000;
using namespace std;

struct node
{
 int value,id;
}a[N];
bool cmp(node a,node b)
{
  return a.value<b.value;
}

int b[N],n,c[N];
int lmin[N],lmax[N],rmin[N],rmax[N];

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

void updata(int x,int d)
{
 while(x<=n)
 {
  c[x]+=d;
  x+=lowbit(x);
 }
}

int getsum(int x)
{
 int ans=0;
 while(x>0)
 {
  ans+=c[x];
  x-=lowbit(x);
 }
 return ans;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
     scanf("%d",&n);
     for(int i=1;i<=n;i++)
     {
       scanf("%d",&a[i].value);
       a[i].id=i;
     }
     sort(a+1,a+1+n,cmp);
     for(int i=1;i<=n;i++)b[a[i].id]=i;
     memset(c,0,sizeof(c));
     for(int i=1;i<=n;i++)
     {
       lmin[i]=getsum(b[i]);
       lmax[i]=i-1-lmin[i];
       updata(b[i],1);
     }
     memset(c,0,sizeof(c));
     for(int i=n,j=1;i>=1;i--,j++)
     {
       rmin[i]=getsum(b[i]);
       rmax[i]=j-1-rmin[i];
       updata(b[i],1);
     }
     long long cnt=0;
     for(int i=1;i<=n;i++)
     {
      long long temp=lmin[i]*rmax[i]+lmax[i]*rmin[i];
      cnt+=temp;
     }
     printf("%I64d\n",cnt);
    }
    return 0;
}


hdu 2492(树状数组)