首页 > 代码库 > Ping pong

Ping pong

Ping pong

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


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
13 1 2 3
 

 

Sample Output
1
 
 1 #include <stdio.h>   2 #include <string.h>   3 #include <math.h>   4 #include <iostream>   5 #include <algorithm>   6 using namespace std;   7 typedef long long LL;   8 const int maxn = 100005;   9 const int Mod = 1000000007;  10 int n;  11 int c[maxn<<2];  12 struct Node  13 {  14     int id,k;  15 }node[maxn];  16 bool cmp( Node a,Node b )  17 {  18     return a.k < b.k;  19 }  20 int lowbit( int x )    21 {    22     return x&(-x);    23 }    24 void add( int x )    25 {    26     while( x <= n )    27     {    28         c[x] += 1;    29         x += lowbit(x);    30     }    31 }    32   33 int sum( int x )    34 {    35     int ans = 0;    36     while( x >= 1 )    37     {    38         ans += c[x];    39         x -= lowbit(x);    40     }    41     return ans;    42 }   43 int main()  44 {  45 #ifndef ONLINE_JUDGE       46     freopen("data.txt","r",stdin);       47 #endif    48     int cas;  49     scanf("%d",&cas);  50     while( cas -- )  51     {  52         LL ans = 0,ld,rd;  53         scanf("%d",&n);  54         memset( c,0,sizeof(c) );  55         for( int i = 1; i <= n; i ++ )  56         {  57             scanf("%d",&node[i].k);    58             node[i].id = i;  59         }  60         sort( node+1,node+n+1,cmp );  61         for( int i = 1; i <= n; i ++ )  62         {  63             ld = sum( node[i].id );     //能力小于i且下标下与i的个数  64             rd = i-1 - ld;              //能力小于i但下标大于i的个数  65             ans += ( ld*( n-node[i].id - rd ) ) + rd*( node[i].id - ld - 1 );  66             add( node[i].id );  67         }  68         printf("%I64d\n",ans);  69     }  70     return 0;  71 }

 

Ping pong