Ping Pong,Beijing 2008,LA4329 树状数组 好题

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

 

 

 

题意:一条大街上住着n个乒乓球爱好者,经常组织比赛。每个人有一个不同的技能值ai,每场比赛需要3个人:2名选手,1名裁判,他们有一个奇怪的规定,即裁判必须住在2名选手的中间,并且技能值也在2名选手之间,问一共能组成多少场比赛。

 

(题意照搬LRJ的训练指南)

 

设ls[i]表示第i个人左边的人中,技能值小于他的人数

设rs[i]表示第i个人右边的人中,技能值小于他的人数、

 

则ans=所有ls[i]*(n-i-rs[i])+(i-1-ls[i])*rs[i]的和。

 

所以问题就是求出ls[i]和rs[i]了。

 

朴素做法:

i从1到n进行一次循环,对于每一个人,再来一次循环,记录1~i-1中有多少个小于a[i]的人。

 

优化:再开一个数组c[i]表示从开始循环到目前,技能值为i的人的个数。

则:对于每一个人,ls[i]=c[1]+...+c[a[i]-1].

那怎么快速地求出区间[1,a[i]-1]的和呢?

 

树状数组的作用:

1.插点问段

2.插段问点

 

其中1作用:

更改数组中某一个位置的值

快速求出某一个区间的和

 

所以用插点问段进行优化,就得到了nlogn的复杂度了。

 

还有,最后要long long

 

Ping Pong,Beijing 2008,LA4329 树状数组 好题
 1 #include<cstdio>

 2 #include<cstring>

 3 #include<algorithm>

 4 

 5 using namespace std;

 6 

 7 const int maxn=20000+10;

 8 

 9 int ls[maxn];          //第i个人左边技能值小于a[i]的人数

10 int rs[maxn];          //第i个人右边技能值小于a[i]的人数

11 int a[maxn];           //第i个人的技能值

12 int c[100000+5];       //开始循环到目前,技能值为i的人数有c[i]个,注意数组大小不同

13 

14 int lowbit(int x)

15 {

16     return x&(-x);

17 }

18 

19 int sum(int x)

20 {

21     int ret=0;

22     while(x>0)

23     {

24         ret+=c[x];

25         x-=lowbit(x);

26     }

27     return ret;

28 }

29 

30 void add(int x,int p)

31 {

32     while(x<=100000)

33     {

34         c[x]+=p;

35         x+=lowbit(x);

36     }

37 }

38 

39 int main()

40 {

41     int test;

42     scanf("%d",&test);

43     while(test--)

44     {

45         int n;

46         scanf("%d",&n);

47         for(int i=1;i<=n;i++)

48             scanf("%d",&a[i]);

49 

50         memset(c,0,sizeof(c));

51         for(int i=1;i<=n;i++)

52         {

53             ls[i]=sum(a[i]);

54             add(a[i],1);

55         }

56         memset(c,0,sizeof(c));

57         for(int i=n;i>0;i--)

58         {

59             rs[i]=sum(a[i]);

60             add(a[i],1);

61         }

62         long long ans=0;

63         for(int i=1;i<=n;i++)

64         {

65             ans+=((long long)ls[i]*(n-i-rs[i])+(long long)(i-1-ls[i])*(rs[i]));

66         }

67         printf("%lld\n",ans);

68     }

69     return 0;

70 }
View Code

 

你可能感兴趣的:(ping)