Ping Pong
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 2302 |
|
Accepted: 879 |
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
Explanation: A street of n table tennis enthusiasts, often organized games, everyone has a different skill value AI, each game requires three people, a referee, two players, there is a strange rule, the referee must live in the middle of the two players, and skills are between the two,
How many games are organized by function;
Solution: Consider each person when the referee, the front is greater than his, the back less than his, the front is smaller than his, the back is greater than his, multiply and add, and then statistics:
AC Code:
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5 using namespacestd;6 Const intN =20010;7 Const intM =100010;8 intX[m],y[m],ymin[m],ymax[m];9 intA[n],lef[n],right[n],leftm[n];Ten intN; One intLowbit (intx) A { - returnx& (-x); - } the voidInit ()//total number of numbers less than or equal to I in overall statistics, expressed as ymin[i] - { - for(inti =1; i<=m; i++) - { +Ymin[i] = ymin[i-1]+y[i];//n The number of the CPC is less than or equal to I, and then minus 1; -Ymax[i] = N-ymin[i];//the number of n is greater than I; + } A } at voidAddintIintC//Insert a number to calculate the following - { - while(i<=m)//the first commit wrote a n, so WA - { -X[i] = x[i]+C; -i=i+lowbit (i); in } - } to intSolveintc) + { - intsum =0; the while(c>0) * { $sum = sum+X[c];Panax Notoginsengc =Lowbit (c); - } the returnsum; + } A intMain () the { + intT; - Long Longans; $scanf"%d",&T); $ while(t--) - { -Ans =0; thememset (x,0,sizeof(x)); -memset (Y,0,sizeof(y));Wuyiscanf"%d",&n); the for(inti =1; i<=n; i++) - { Wuscanf"%d",&a[i]); -Y[a[i]] =1; About } $ init (); - for(inti =1; i<=n;i++) - { -Add (A[i],1); ALef[i] = Solve (a[i]-1);//the number of the front less than a[i]; +Leftm[i] = i1-lef[i];//the number of the front greater than a[i]; the intMa = ymax[a[i]]-leftm[i];//number of the latter greater than a[i]; - intMB = Ymin[a[i]]-1-Lef[i];//number of the latter less than a[i]; $Ans = ans + lef[i]*ma +leftm[i]*mb;//before big after small, before small after large; the } theprintf"%lld\n", ans); the } the return 0; -}
POJ Ping Pong LA 4329 (number of tree-like arrays)