题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492
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?
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).
For each test case, output a single line contains an integer, the total number of different games.
1
3 1 2 3
1
话不多说 直接上代码
解释都在代码中标注了
1 #include<stdio.h> 2 #include<string.h> 3 #define size 100100 4 int n,c[size],x1[size],x2[size],y1[size],y2[size],a[size]; 5 6 int Lowbit(int k) 7 { 8 return (k&-k); 9 } 10 void update(int pos,int num) 11 { 12 while(pos<size)//重要 是size 而不是<=n 13 { 14 c[pos]+=num; 15 pos+=Lowbit(pos); 16 } 17 } 18 int sum(int pos) 19 { 20 int s=0; 21 while(pos>0) 22 { 23 s+=c[pos]; 24 pos-=Lowbit(pos); 25 } 26 return s; 27 } 28 29 30 31 int main() 32 { 33 int i,cas; 34 scanf("%d",&cas); 35 while(cas--) 36 { 37 memset(c,0,sizeof(c)); 38 scanf("%d",&n); 39 for(i=1;i<=n;i++) 40 { 41 scanf("%d",&a[i]); 42 int k1; 43 k1=sum(a[i]); 44 x1[i]=k1; //输入的i个数中 有k1个比a[i]小 45 x2[i]=i-1-k1; //输入的i个数中 有k1个比a[i]大 46 update(a[i],1); 47 } 48 memset(c,0,sizeof(c)); 49 int j=1;//代表现在输入的数的个数 50 for(i=n;i>=1;i--,j++) 51 { 52 int k1; 53 k1=sum(a[i]); 54 y1[i]=k1;//输入a[i]后输入的那些数中有多少个比a[i]小的 55 y2[i]=j-1-k1; //输入a[i]后输入的那些数中有多少个比a[i]大的 56 update(a[i],1); 57 } 58 __int64 ans=0; 59 for(i=1;i<=n;i++) 60 { 61 // printf("x1[%d]=%d x2[%d]=%d y1[%d]=%d y2[%d]=%d\n",i,x1[i],i,x2[i],i,y1[i],i,y2[i]); 62 ans+=x1[i]*y2[i]+x2[i]*y1[i]; 63 // ans+=x1[i]*x2[i]; 64 } 65 printf("%I64d\n",ans); 66 } 67 68 }