HDU 2492 BIT/逆序数/排列组合

Ping pong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1143    Accepted Submission(s): 387

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
lrj蓝书上BIT的课后习题,挺不错的。
题目读起来有点略拗口,意思就是有N的人,每个人有唯一的一个技能值,规定想要组一场比赛的话需要三个人,且其中一个人做裁判,裁判的技能值必须位于剩余二人之间且裁判的位置也必须位于二者之间,问有多少不同的比赛阵容,输入时按照位置从左至右输入所有人的技能值。
我们把每个人考虑做裁判的方案数,然后累加起来,假设ai做裁判,如果我们知道ai前面分数小于它的人数ci和其后面分数小于它的人数di,
则ai做裁判有 ( ci*(N-i-di) + di*(i-1-ci) )方案数,所以问题转化为求ci和di。
观察得知分数最大10w并不是很大,考虑将分数作为线段树的区间,保存的是区间分数的总人数,然后一边更新一边记录ci,di可以清空树以后再逆序插入更新。
区间和!BIT!简单高效!
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 int sumv[100000+15];
 5 int c[20005],d[20005],a[20005];
 6 int lowbit(int x){return x&-x;}
 7 int sum(int x)
 8 {
 9   int ret=0;
10   while(x){
11     ret+=sumv[x];
12     x-=lowbit(x);
13   }
14   return ret;
15 }
16 void add(int x,int d)
17 {
18     while(x<=100000){
19         sumv[x]+=d;
20         x+=lowbit(x);
21     }
22 }
23 int main()
24 {
25     int N,t,m,i,j,k;
26     scanf("%d",&t);
27     while(t--){memset(sumv,0,sizeof(sumv));
28     LL ans=0;
29         scanf("%d",&N);
30         for(i=1;i<=N;++i){
31             scanf("%d",&a[i]);
32             add(a[i],1);
33             c[i]=sum(a[i]-1);
34         }memset(sumv,0,sizeof(sumv));
35         for(i=N;i>=1;--i){
36            add(a[i],1);
37            d[i]=sum(a[i]-1);
38         }
39         for(i=1;i<=N;++i){
40             ans+=(LL)c[i]*(N-i-d[i])+d[i]*(i-1-c[i]);
41         }
42         printf("%lld
",ans);
43     }
44     return 0;
45 }
原文地址:https://www.cnblogs.com/zzqc/p/7267065.html