Corresponding HDU topic: Click to open link
Ping PongTime
limit:MS
Memory Limit:0KB
64bit IO Format:%lld & %llu SubmitStatus
Description
N(3N20000)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 an integer t(1t) , indicating the number of test cases, Followed by T lines 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 each player, in the order of West to east ( 1ai100000, i = 1 ... N ).
Output
For each test case, output a single line contains an integer with the total number of different games.
Sample Input
13 1 2 3
Sample Output
1
Test Instructions: There are n people in a row, each has a different level, two people to fight to find a referee, the referee's rank between them, and the referee will stand between them. Ask how many different races can be made up in total.
Train of thought: for example (level is not necessarily 1~n)
1 3 7 6 9 8 5
If 6 is the referee, then the number of people on the left level is lower than his (order number) A= 2, the number of people on the right level higher than him (order number) B = 2, the number of the left rank higher than he (reverse number) C= 1, the right level is lower than his number (reverse number) d = 1;
So 6 of referees can make up a*b + c*d = 5 different races.
From left to right for once, the number of games that each person can make up the referee is the result.
The solution of the inverse number and the order number can be a tree-like array
Specific implementation code:
#include <cstdio> #include <cstdlib> #include <cmath> #include <map> #include <queue># include<stack> #include <vector> #include <algorithm> #include <cstring> #include <string > #include <iostream>const int maxn=20000+10;using namespace Std;int a[maxn];int Highl[maxn];//highl[i] The number of numbers that represent the left side of the a[i] is larger than the count of int highr[maxn];//highr[i] denotes the number of numbers that are larger than the right of the number of I (A[i]) int Lowl[maxn];//lowl[i] The number of numbers that are smaller than a[i] for the left of the number of I Lowr[maxn];//lowr[i] Indicates the number of numbers that are smaller than the right of the number of a[i] int c[maxn*5];int lowbit (int x) {return x& (-X);} void update (int x, int num) {while (x <= 5*maxn-2) {c[x] + = num;x + = lowbit (x);}} int sum (int x) {int res = 0;while (x > 0) {res + = c[x];x-= lowbit (x);} return res;} int main () {//freopen ("In.txt", "R", stdin), int t;scanf ("%d", &t), while (t--) {int N;memset (c,0,sizeof (c)); scanf ("% D ", &n); for (int i=0; i<n; i++) {scanf ("%d ", a+i), int x = a[i];highl[i] = SUM (5*MAXN)-sum (x); Lowl[i] = SUM (x); updat E (x,1);} Memset (C,0,sizeof (c)); for (int i=n-1; i>=0; i--) {int x = A[i];hiGhr[i] = SUM (5*MAXN)-sum (x); Lowr[i] = SUM (x); update (x,1); Long Long sum = 0;for (int i=0; i<n; i++) {//multiplication principle and addition principle Sum + = (long long) highl[i] * Lowr[i]; Sum + = (long long) lowl[i] * highr[i];} printf ("%lld\n", Sum);} return 0;}
Tree array + reverse order number and sequence number--hdu 2492