Obviously, each exchange can be reduced by 1.
So find the logarithm of the reverse order, and then-= K...
_ (: Too many rows )_
# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <set> # include <map> # include <iostream> # include <algorithm> using namespace STD; # define n 100005 # define ll long longll C [n + 100000], maxn; inline ll lowbit (ll x) {return X & (-x );} void change (Ll I, ll X) // The I point increment is X {While (I <= maxn) {C [I] + = X; I + = lowbit (I) ;}} ll sum (ll x) {// sum of the range [1, x] ll ans = 0; For (ll I = X; i> = 1; I-= lowbit (I) ans + = C [I]; return ans;} ll A [n], n, k; set <ll> S; set <ll >:: iterator P; Map <LL, ll> MP; int main () {ll I; while (CIN> N> K) {S. clear (); MP. clear (); for (I = 1; I <= N; I ++) scanf ("% i64d", & A [I]), S. insert (a [I]); maxn = N + 100; for (P = S. begin (), I = 2; p! = S. end (); P ++, I ++) {MP [* p] = I ;}for (I = 1; I <= N; I ++) A [I] = MP [A [I]; memset (C, 0, sizeof C); LL ans = 0; for (I = N; I> = 1; I --) {ans + = sum (A [I]-1); Change (A [I], 1);} ANS-= K; cout <max (0ll, ans) <Endl;} return 0 ;}