Beijing 2008 / UVa 1428 / POJ 3928 / HDU 2492 Ping pong (树状数组)

来源:互联网 发布:节假日堵车数据 编辑:程序博客网 时间:2024/06/07 05:46
Ping pong

http://poj.org/problem?id=3928;http://acm.hdu.edu.cn/showproblem.php?pid=2492;http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=501&page=show_problem&problem=4174

Time Limit: 1000MS
Memory Limit: 65536K

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


题意抽象:
已知数列{aN}(3<=N<=20000),从左往右取三个不同的数(可以不相邻),求使这三个数排成升序或降序的取法数。

解法:
复杂度:

思路:用树状数组维护每个能力值的个数
1. 对ai(1<i<N),求在其左边或右边且小于其的数的个数ll[i]和rl[i]。
const int MAXN = 20002;int a[MAXN], ll[MAXN], rl[MAXN];//leftlower,rightlowerscanf("%d", &a[1]);update(a[1], 1);for (i = 2; i < n; i++){    scanf("%d", &a[i]);    update(a[i], 1);    ll[i] = sum(a[i] - 1);}scanf("%d", &a[n]);//注意这里要清空树memset(rank, 0, sizeof(rank));update(a[n], 1);for (i = n - 1; i > 1; i--){    update(a[i], 1);    rl[i] = sum(a[i] - 1);}

2. 注意到在数列{aN}是递增或递减数列的情形时,取法数达到最大,为(N-2)(N-1)N/6,数据最大约10^15,所以这里用long long。
计算Σ(i=2,n-1) ll[i] * (n - i - rl[i]) + (i - 1 - ll[i]) * rl[i],搞定。
long long ans = 0;for (i = 2 ; i < n; i++){    ans += ll[i] * (n - i - rl[i]) + (i - 1 - ll[i]) * rl[i];}

完整代码:

/*UVa: 0.269s*//*POJ: 235ms,792KB*//*HDU: 109ms,904KB*/#include<cstdio>#include<cstring>const int MAXN = 100002;const int MAXN2 = 20002;int rk[MAXN], a[MAXN2], ll[MAXN2], rl[MAXN2];void update(int a, int val){while (a <= MAXN){rk[a] += val;a += -a & a;}}int sum(int a){int sum = 0;while (a > 0){sum += rk[a];a -= -a & a;}return sum;}int main(){int i, t, n;long long ans;scanf("%d", &t);while (t--){scanf("%d", &n);memset(rk, 0, sizeof(rk));scanf("%d", &a[1]);update(a[1], 1);for (i = 2; i < n; i++){scanf("%d", &a[i]);update(a[i], 1);ll[i] = sum(a[i] - 1);}scanf("%d", &a[n]);memset(rk, 0, sizeof(rk)); ///注意这里要清空update(a[n], 1);for (i = n - 1; i > 1; i--){update(a[i], 1);rl[i] = sum(a[i] - 1);}ans = 0;for (i = 2 ; i < n; i++)ans += ll[i] * (n - i - rl[i]) + (i - 1 - ll[i]) * rl[i];printf("%lld\n", ans);}return 0;}