Topic Links:
pku:http://poj.org/problem?id=3928
hdu:http://acm.hdu.edu.cn/showproblem.php?pid=2492
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-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 has to walk to the referee's house, and because they is lazy, they want to make their total walking dist Ance no more than the distance between their houses. Of course all players live in different houses and the position of their houses is all different. If the referee or any of the contestants are different, we call both games different. Now are the problem:how many different games can being held in this ping Pong street?
Input
The first line of the input contains a integer T (1<=t<=20), indicating the number of test cases, followed by T line s each of the 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 all 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 with the total number of different games.
Sample Input
1 3 1) 2 3
Sample Output
1
Source
Beijing 2008
PS:The number of the left and right sides of each count is larger than its number and small number! Finally, cross-multiply it!
The code is as follows:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int maxn=100017;int n;int A[MAXN], C[maxn];int LEFTMAX[MAXN], Leftmin[maxn];int RIGHTMAX[MAXN], RIGHTMIN[MAXN ];typedef __int64 ll;int lowbit (int x)//2^k{return x& (-X);} void Update (int i, int x)//i point increment for x{while (I <= MAXN)//Note here {C[i] + = x; i + = Lowbit (i); }}int sum (int x)//interval sum [1,x]{int sum=0; while (x>0) {sum+=c[x]; X-=lowbit (x); } return sum;} int main () {int t; int n; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]); } memset (C,0,sizeof (c)); for (int i = 1; I <= n; i++) {leftmin[i] = SUM (a[i]);//Calculate left small number leftmax[i] = i-leftmin[i]- 1;//calculates the number of the left large update (a[i],1); } memset (C,0,sizeof (c));//Clear 0 for (int i = N,J = 1; I >= 1; I--, J + +) {Rightmin[i] = SUM (a[i]); Rightmax[i] = j-rightmin[i]-1; Update (a[i],1); } LL ans = 0; for (int i = 1; I <= n; i++) {Ans+=leftmax[i]*rightmin[i] + leftmin[i]*rightmax[i];//cross multiply Fetch and} printf ("%i64d\n", ans); } return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
POJ 3928 & HDU 2492 Ping Pong (countdown to Tree array evaluation)