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

 

Sample Output
1
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <list>
13 #include <iomanip>
14 #include <cstdlib>
15 #include <sstream>
16 using namespace std;
17 typedef long long LL;
18 const int INF=0x5fffffff;
19 const double EXP=1e-6;
20 const int MS=20005;
21 int c[MS];
22 int n;
23 
24 struct node
25 {
26     int rank;
27     int id;
28     bool operator <(const node &a)const
29     {    //   rank  unique
30         return rank>a.rank||(rank==a.rank&&id<a.id);
31     }
32 }nodes[MS];
33 
34 int lowbit(int x)
35 {
36     return x&(-x);
37 }
38 
39 void updata(int x)
40 {
41     while(x<=n)
42     {
43         c[x]+=1;
44         x+=lowbit(x);
45     }
46 }
47 
48 int getsum(int x)
49 {
50     int ret=0;
51     while(x>0)
52     {
53         ret+=c[x];
54         x-=lowbit(x);
55     }
56     return ret;
57 }
58 
59 int main()
60 {
61     int T;
62     scanf("%d",&T);
63     while(T--)
64     {
65         scanf("%d",&n);
66         memset(c,0,sizeof(c));
67         for(int i=1;i<=n;i++)
68         {
69             scanf("%d",&nodes[i].rank);
70             nodes[i].id=i;
71         }
72         sort(nodes+1,nodes+n+1);
73         LL ans=0;   // 一定要  LL 不然WA
74         for(int i=1;i<=n;i++)
75         {
76             int id=nodes[i].id;
77             int x=getsum(id);   // 值比它大,坐标比它小的数量
78             int y=i-1-x;        // 值比它大的数总共有i-1,-x就是
79                               //  值它大,坐标比它大
80             ans+=x*(n-id-y)+y*(id-1-x);
81             updata(id);
82         }
83         printf("%lld\n",ans);
84     }
85     return 0;
86 }

 

 

 

posted @ 2014-11-15 16:37  daydaycode  阅读(271)  评论(0编辑  收藏  举报