UVA 1428-ping Pong
Topic links
Test instructions: Given some people, from left to right, each person has a skill value, now to hold the game, must meet the position from left to right 3 people. and the skill value from small to large or from big to small, ask a few forms of organization
Idea: Use a tree-like array to process the number of the left side of each position smaller than it and the number of small on the right. Then the left and right large can also be calculated, then the game played for the left small * right big + left big * right small.
Code:
#include <cstdio> #include <cstring>const int N = 100005;int T, N, A[n];long long bit[n], lx[n], rx[n];inline i NT lowbit (int x) { return x& (-X);} void Add (int x, Long long Val) {while (x < N) {Bit[x] + = val;x + lowbit (x); }} Long long Query (int x) { long long ans = 0; while (x > 0) {ans + = bit[x];x-= lowbit (x); } return ans;} int main () { scanf ("%d", &t); while (t--) {scanf ("%d", &n), memset (bit, 0, sizeof (bit)), for (int i = 0; i < n; i++) { scanf ("%d", &a[i]); C10/>lx[i] = Query (A[i]); Add (A[i], 1);} memset (bit, 0, sizeof (bit)), for (int i = n-1; I >= 0; i--) { Rx[i] = Query (A[i]); Add (A[i], 1);} Long Long ans = 0;for (int i = 0; i < n; i++) ans + lx[i] * (N-i-1-rx[i]) + (I-lx[i]) * Rx[i];p rintf ("%ll D\n ", ans); } return 0;}
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
UVA 1428-ping Pong (tree-like array)