现在的位置: 首页 > 综合 > 正文

UVALive 4329 Ping pong

2017年11月23日 ⁄ 综合 ⁄ 共 2558字 ⁄ 字号 评论关闭
                                  Ping pong
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]  

Description

Download as PDF

 

N(3$ \le$N$ \le$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$ \le$T$ \le$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 a1a2...aN follow, indicating the skill rank of each player, in the order of west to east ( 1$ \le$ai$ \le$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

Beijing 2008-2009

 

枚举裁判的位置+树状数组

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 const int maxn=100010;
 8 
 9 int tree[maxn+10],n;
10 
11 inline int lowbit(int x) {return x&(-x);}
12 
13 void Init()
14 {
15     memset(tree,0,sizeof(tree));
16 }
17 
18 int Add(int x)
19 {
20     while(x<=maxn)
21     {
22         tree[x]++;
23         x+=lowbit(x);
24     }
25 }
26 
27 int getSum(int x)
28 {
29     int sum=0;
30     while(x>0)
31     {
32         sum+=tree[x];
33         x-=lowbit(x);
34     }
35     return sum;
36 }
37 
38 struct node
39 {
40     int leftbig,leftsmall;
41     int rightbig,rightsmall;
42 }ND[20020];
43 
44 int bilib[20020];
45 
46 int main()
47 {
48     int t;
49     scanf("%d",&t);
50     while(t--)
51     {
52         long long int ans=0;
53         scanf("%d",&n);
54         int x;
55         for(int i=1;i<=n;i++)
56         {
57             scanf("%d",&bilib[i]);
58         }
59         Init();
60         for(int i=1;i<=n;i++)
61         {
62             ND[i].leftsmall=getSum(bilib[i]);
63             Add(bilib[i]);
64             ND[i].leftbig=i-1-ND[i].leftsmall;
65         }
66         Init();
67         for(int i=n;i>=1;i--)
68         {
69             ND[i].rightsmall=getSum(bilib[i]);
70             Add(bilib[i]);
71             ND[i].rightbig=n-i-ND[i].rightsmall;
72         }
73         ///debug/*
74         //for(int i=1;i<=n;i++)
75         //{
76         //    cout<<ND[i].leftsmall<<" , "<<ND[i].leftbig<<endl;
77         //    cout<<ND[i].rightsmall<<" , "<<ND[i].rightbig<<endl;
78         //    cout<<endl;
79         //}
80         ///.....*/
81         for(int i=2;i<=n-1;i++)
82         {
83             ans+=1LL*((ND[i].leftsmall*ND[i].rightbig)+(ND[i].leftbig*ND[i].rightsmall));
84         }
85         cout<<ans<<endl;
86     }
87     return 0;
88 }

 

抱歉!评论已关闭.