Ping pong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2616 Accepted Submission(s): 975
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?
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).
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
题意:有n个选手,给定他们的能力,并且输入顺序就是他们所在的位置,2个选手要比赛的话要找一个位置在他们之间,能力在他们之间的裁判,问能有多少场不同的比赛
题解:选一个人做裁判,他在左边比他能力低的数目乘以他右边比他能力高的数目加起来,再加上在他左边能力比他高的数目乘以他右边能力比他低的数目,全部人的加起来就是比赛总场数。人数计算可以利用树状数组,做一次从左往右插入,并计算出每一个点在左边能力比他低的数目,再用i-1-这个值就是在左边能力比他高的数目,类似算出右边的。该题也可以用线段树做,不过本人是用树状数组做的
#include<stdio.h>
#include<string.h>
int a[100005],c[100005],n;
int leftmin[100005],leftmax[100005],rightmin[100005],rightmax[100005];
int low_bit(int x)
{
return x&(-x);
}
void updata(int x,int y)
{
while(x<=100000)
{
c[x]+=y;
x+=low_bit(x);
}
}
int sum(int x)
{
int temp=0;
while(x>0)
{
temp+=c[x];
x-=low_bit(x);
}
return temp;
}
int main()
{
int t,i;
__int64 s;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d",a+i);
memset(c,0,sizeof(c));
for(i=1;i<=n;i++)
{
updata(a[i],1);
leftmin[i]=sum(a[i]-1);
leftmax[i]=i-1-leftmin[i];
}
memset(c,0,sizeof(c));
for(i=n;i>=1;i--)
{
updata(a[i],1);
rightmin[i]=sum(a[i]-1);
rightmax[i]=n-i-rightmin[i];
}
for(s=0,i=1;i<=n;i++)
{
s=s+leftmin[i]*rightmax[i];
s=s+leftmax[i]*rightmin[i];
}
printf("%I64d\n",s);
}
return 0;
}