UVa 1428 Ping pong

N <tex2html_verbatim_mark>(3UVa 1428 Ping pongNUVa 1428 Ping pong20000) <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?

 

Input 

The first line of the input contains an integer T <tex2html_verbatim_mark>(1UVa 1428 Ping pongTUVa 1428 Ping pong20) <tex2html_verbatim_mark>, indicating the number of test cases, followed by T <tex2html_verbatim_mark>lines each of which describes a test case.

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 ( 1UVa 1428 Ping pongaiUVa 1428 Ping pong100000 <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.

 

Sample Input 

 

1
3 1 2 3

 

Sample Output 

 

1

 

树状数组问题

考虑第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种比赛。

这样就把问题转化为了求数组Ci和Di的问题:

C[i]可以这样求:

  从左到右扫描所有的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]。

D[i]的求法与C[i]同理,写代码的时候只要让for偱环倒过来进行即可

注意最终求和时要用long long保存,用int会溢出(此处WA两次…)

 

UVa 1428 Ping pong
 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++]

UVa 1428 Ping pong

上一篇:VMWare Workstation 10中安装Mac OS 10.8.5


下一篇:Spider Studio 新版本 (码年吉祥版) - 浏览器视图 / 脚本库上线!