HDU 2492 Ping pong(树状数组)

来源:互联网 发布:网络设备监控软件orion 编辑:程序博客网 时间:2024/04/29 07:39

Ping pong

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

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 Input13 1 2 3Sample Output1Source2008 Asia Regional Beijing

题意:有t组数据,每行数据的第一个数 n 表示有n个人,每个位置上的数据代表选手的技能值,现在要三个人组队,按照位置的顺序,三个人中间的人是裁判,两边的选手,裁判的技能值要跟位置一样位于两位选手之间。
解题思路:以自己为裁判,然后找左边有多少人比较小,再找右边有多少人比自己大,然后两个数相乘就是以他为裁判的比赛场数,当然还有相反的,找右边比自己小的,左边比自己大的,再相乘相加。这样子就变成了:和找逆序数、顺序数差不多了,找左边(右边)比自己大(小)有多少个数,用树状数组最好最快!所以两个for循环就全部找到,然后相乘相加。

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;const int ma=100005;int x[ma],xx[ma],y[ma],yy[ma],a[ma],b[ma],s[ma];int lo(int x){    return x&(-x);}int sum(int e){    int sum=0;    while(e>0)    {        sum+=b[e];        e-=lo(e);    }    return sum;}void update(int pos,int num){    while(pos<=ma)    {        b[pos]+=num;        pos+=lo(pos);    }}int main(){    int t,n,flag;    long long int summ;    scanf("%d",&t);    while(t--)    {        summ=0;        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        scanf("%d",&n);        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        for(int i=1;i<=n;i++)//求左边比该点技能值大的数的个数,小的数的个数         {            x[i]=sum(a[i]);//输入的i个数中 有 sum(a[i]) 个比a[i]小              xx[i]=i-1-x[i];//输入的i个数中 有 i-1-sum(a[i]) 个比a[i]大              update(a[i],1);        }        memset(b,0,sizeof(b));        int j=1;//代表现在输入的数的个数          for(int i=n;i>=1;i--,j++)//求右边比该点技能值大的数的个数,小的数的个数          {            y[i]=sum(a[i]);//输入a[i]后 输入的那些数中有 sum(a[i]) 个比a[i]小的              yy[i]=j-1-y[i];//输入a[i]后输入的那些数中有 j-1-sum(a[i] 个比a[i]大的             update(a[i],1);        }        for(int i=1;i<=n;i++)            summ+=xx[i]*y[i]+x[i]*yy[i];        printf("%lld\n",summ);    }    return 0;}
原创粉丝点击