Evaluation Portal
Question:
T-group data, N numbers, 3 are selected in order, and the sum of all the number pairs that meet the monotonic increasing or monotonic decreasing conditions is obtained.
Sample Input
1 3 1 2 3
Sample output
1
Ideas:
This is an advanced method of reverse order, because it is not just a reverse order.
We can first find all the reverse order pairs based on the conventional practice, but let's think about who is this reverse order?
After you create a table, you will find that this is the reverse order of the prefixes of all numbers. If we calculate the reverse order of all suffixes, isn't multiplication the correct number of all monotonically decreasing numbers?
The same is true for the order.
Note:
Don't open long to see our ancestors, ten years of OI empty
Code
# Include <stdio. h> # include <algorithm> # include <string. h> # define lowbit (x) X &-x using namespace STD; const int mxn = 20010; struct node {int Val, ID;} A [mxn]; bool operator <(const node & X, const node & Y) {return X. val <Y. val;} int N, F [mxn] [2], C [mxn]; void add (int x) {While (x <= N) {c [x] ++; x + = lowbit (x) ;}} int ask (int x) {int Re = 0; while (X) {re + = C [X]; X-= lowbit (x);} return re;} int main () {int t; scanf ("% d ", & T); While (t --) {long ans = 0; scanf ("% d", & N); For (INT I = 1; I <= N; ++ I) scanf ("% d", & A [A [I]. id = I]. val); sort (a + 1, A + 1 + n); memset (C, 0, sizeof (c); For (INT I = N; I; -- I) {Add (A [I]. ID); F [I] [0] = Ask (A [I]. ID-1); // forward/backward f [I] [1] = Ask (N)-ask (A [I]. ID); // backward peer} memset (C, 0, sizeof (c); For (INT I = 1; I <= N; ++ I) {Add (A [I]. ID); ans + = f [I] [1] * Ask (A [I]. ID-1); // backward x forward X ans + = f [I] [0] * (ASK (N)-ask (A [I]. ID); // forward/backward * backward} printf ("% LLD \ n", ANS);} return 0 ;}
Ping pong (poj 3928)