UVa 1428 Ping pong

N <tex2html_verbatim_mark>(3$ \le$N$ \le$20000) <tex2html_verbatim_mark>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?html

 

Input 

The first line of the input contains an integer T <tex2html_verbatim_mark>(1$ \le$T$ \le$20) <tex2html_verbatim_mark>, indicating the number of test cases, followed by T <tex2html_verbatim_mark>lines each of which describes a test case.ios

Every test case consists of N + 1 <tex2html_verbatim_mark>integers. The first integer is N <tex2html_verbatim_mark>, the number of players. Then N <tex2html_verbatim_mark>distinct integers a1a2...aN <tex2html_verbatim_mark>follow, indicating the skill rank of each player, in the order of west to east ( 1$ \le$ai$ \le$100000 <tex2html_verbatim_mark>, i = 1...N <tex2html_verbatim_mark>).數組

 

Output 

For each test case, output a single line contains an integer, the total number of different games.ide

 

Sample Input 

 

1
3 1 2 3

 

Sample Output 

 

1

 

樹狀數組問題this

考慮第i我的當裁判時,假設a1到a(i-1)中有Ci我的比ai小,那麼就有(i-1)-Ci個比ai大;同理,假設a(i+1)到an中有Di個比ai小,那麼就有(n-i)-Di個比ai大。這樣就能夠算出,i 當裁判時有Ci*(n-i-Di)+(i-Ci-1)*Di種比賽。
spa

這樣就把問題轉化爲了求數組Ci和Di的問題:code

C[i]能夠這樣求:htm

  從左到右掃描全部的ai,令x[j]表示目前爲止已經考慮的全部ai中,是否存在一個ai=j(x[j]=0表示不存在,x[j]=1表示存在),則Ci就是前綴和x[1]+x[2]+…+x[ai-1]。初始全部x[i]=0,在計算Ci時,須要先令x[ai]=1,即執行數狀數組的加操做,而後求前綴和。這樣便可求出數組C[i]。blog

D[i]的求法與C[i]同理,寫代碼的時候只要讓for偱環倒過來進行便可input

注意最終求和時要用long long保存,用int會溢出(此處WA兩次…)

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 int n;
 8 int a[20050],temp[100050],c[20050],d[20050];
 9 
10 int lowbit(int x)
11 {
12     return x&(-x);
13 }
14 
15 int sum(int x)
16 {
17     int ret=0;
18 
19     while(x>0)
20     {
21         ret+=temp[x];
22         x-=lowbit(x);
23     }
24 
25     return ret;
26 }
27 
28 void add(int x,int t)
29 {
30     while(x<=100000)
31     {
32         temp[x]+=t;
33         x+=lowbit(x);
34     }
35 }
36 
37 int main()
38 {
39     int kase;
40 
41     scanf("%d",&kase);
42 
43     while(kase--)
44     {
45         memset(c,0,sizeof(c));
46         memset(d,0,sizeof(d));
47 
48         scanf("%d",&n);
49 
50         for(int i=1;i<=n;i++)
51             scanf("%d",&a[i]);
52 
53         memset(temp,0,sizeof(temp));
54         for(int i=1;i<=n;i++)
55         {
56             add(a[i],1);
57             c[i]=sum(a[i]-1);
58         }
59 
60         memset(temp,0,sizeof(temp));
61         for(int i=n;i>=1;i--)
62         {
63             add(a[i],1);
64             d[i]=sum(a[i]-1);
65         }
66 
67         long long ans=0;
68         for(int i=2;i<n;i++)
69             ans+=c[i]*(n-i-d[i])+d[i]*(i-1-c[i]);
70 
71         printf("%lld\n",ans);
72     }
73 
74     return 0;
75 }
[C++]
相關文章
相關標籤/搜索