poj3928 la4329 pingpong
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).
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
题意:见白书p197 ,注意没有两个人的rank是一样的
思路:利用树状数组,树状数组教程(盗链)http://www.cnblogs.com/zhangshu/archive/2011/08/16/2141396.html
考虑第i个人当裁判的情形。假设i左边有ci个比ai小,那么就有(i-1)-ci个比ai大;
同理:假设右边有di个比ai小,那么就有(n-i)-di个比ai大。所以当i当裁判时,有ci(n-i-di)+(i-ci-1)di种比赛。
建立一个大小<=max(ai)的树状数组,全赋0。从左到右扫描数轴,扫描到第i个人时,add(a[i],1)。我们可以发现,此时在ci的值即为sum(a[i]-1)。因为以后这个值会发生变化,所以我们用一个数组把它存起来。全部扫完一遍之后,我们又可以发现,di的值为sum(a[i]-1)-ci。求解结束。
1 /* 2 * Author: Joshua 3 * Created Time: 2014年07月13日 星期日 14时09分45秒 4 * File Name: poj3928.cpp 5 */ 6 #include<cstdio> 7 #include<cstring> 8 #include<algorithm> 9 using namespace std; 10 11 #define maxv 100005 12 #define maxn 20005 13 typedef long long LL; 14 15 int c[maxv],n,vv; 16 int lowBit(int x) 17 { 18 return x&(-x); 19 } 20 21 void add(int x,int v) 22 { 23 while (x<=vv) 24 { 25 c[x]+=v; 26 x+=lowBit(x); 27 } 28 } 29 30 int sum(int x) 31 { 32 int ret=0; 33 while (x>0) 34 { 35 ret+=c[x]; 36 x-=lowBit(x); 37 } 38 return ret; 39 } 40 41 void solve() 42 { 43 int a[maxn],temp[maxv]; 44 LL ans=0; 45 memset(c,0,sizeof(c)); 46 scanf("%d",&n); 47 vv=0; 48 for (int i=1;i<=n;++i) 49 { 50 scanf("%d",&a[i]); 51 vv=max(vv,a[i]); 52 } 53 for (int i=1;i<=n;++i) 54 { 55 add(a[i],1); 56 temp[i]=sum(a[i]-1); 57 } 58 for (int i=1;i<=n;++i) 59 { 60 int ts=sum(a[i]-1); 61 ans+=temp[i]*(n-i-(ts-temp[i])); 62 ans+=(i-1-temp[i])*(ts-temp[i]); 63 } 64 printf("%lld\n",ans); 65 } 66 67 68 int main() 69 { 70 int T; 71 scanf("%d",&T); 72 while (T) 73 { 74 solve(); 75 T--; 76 } 77 return 0; 78 }