Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4000
For an arrangement of 1 -- N, calculate the number of all I <j <K and A [I] <A [k] <A [J...
Analysis: I don't know how to do it during the competition. I always want to simply enumerate I, j, or K to get the result. I don't know how to change it... In fact, you can change the direction of thinking, for each position to consider the number of The number of his greater than the number, then the small number followed by two large numbers x (x-1)/2, then, you must subtract all the numbers of I <j <K and A [I] <A [J] <A [k] that constitute the end. Both of these operations can be solved, then the problem is converted to scanning the array again...
Code:
# Include <stdio. h ># include <iostream> using namespace STD; const int n = 100001; int A, N, F [N] ;__ int64 ans, TMP, tmp1; void insert (int I) {for (; I <= N; I + = I & (-I) f [I] + = 1 ;} int query (int I) {int TMP = 0; For (; I> 0; I-= I & (-I) TMP + = f [I]; return TMP;} int main () {int I, Cas, cas1 = 1; scanf ("% d", & CAS); While (CAS --) {scanf ("% d", & N ); ans = 0; for (I = 1; I <= N; I ++) f [I] = 0; for (I = 1; I <= N; I ++) {scanf ("% d", & A); insert (a); TMP = query (A-1); tmp1 = (n-A)-(i-1-tmp ); ans-= TMP * tmp1; If (tmp1> = 2) ans + = tmp1 * (tmp1-1)/2;} printf ("case # % d: % i64d \ n ", cas1 ++, ANS % 100000007);} return 0 ;}