HDU 1394 minimum inversion number (line segment tree)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1394

The line segment tree calculates the reverse order number. First, the reverse order number (n logn) of the initial array is obtained, and then O (1) releases other Reverse orders. Because the input is 0 -- n-1, the build starts from 0.

Code:

# Include <cstdlib>
# Include <cctype>
# Include <cstring>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Include <vector>
# Include <string>
# Include <iostream>
# Include <sstream>
# Include <set>
# Include <queue>
# Include <stack>
# Include <fstream>
# Include <iomanip>
# Include <bitset>
# Include <list>
# Include <ctime>
Using namespace STD;

# Define set (ARR, what) memset (ARR, what, sizeof (ARR ))
# Define ff (I, A) for (I = 0; I <A; I ++)
# Define SD (a) scanf ("% d", &)
# Define SSD (a, B) scanf ("% d", & A, & B)
# Define SF (a) scanf ("% lf", &)
# Define SS (a) scanf ("% s",)
# Define SLD (a) scanf ("% LLD", &)
# Define PF (a) printf ("% d \ n",)
# Define PPF (a, B) printf ("% d \ n", a, B)
# Define SZ (ARR) (INT) A. Size ()
# Define swap (A, B) A = a xor B; B = a xor B; A = a xor B;
# Define read freopen ("in.txt", "r", stdin)
# Define write freopen ("out.txt", "W", stdout)
# Define max 1 <30
# Define ESP 1e-5
# Define lson L, M, RT <1 | 1
# Define rson m + 1, R, (RT <1) + 2
Template <class T> inline t sqr (t a) {return a * ;}
Template <class T> inline void Amin (T & A, t B) {if (a =-1 | A> B) A = B ;}
Template <class T> inline void amax (T & A, t B) {if (a <B) A = B ;}
Template <class T> inline T min (t a, t B) {return A> B? B: ;}
Template <class T> inline T max (t a, t B) {return A> B? A: B ;}
Const int maxn = 5010;
Int sum [maxn <2];
Int data [maxn];
Void build (int l, int R, int RT ){
Sum [RT] = 0;
If (L = r) return;
Int M = (L + r)> 1;
Build (lson );
Build (rson );
}
Void Update (int x, int L, int R, int RT ){
Sum [RT] ++;
If (L = r) return;
Int M = (L + r)> 1;
If (x <= m) Update (x, lson );
Else Update (x, rson );
}
Int query (int l, int R, int L, int R, int RT ){
If (L <= L & R <= r) return sum [RT];
Int M = (L + r)> 1;
Int ret = 0;
If (L <= m) RET + = query (L, R, lson );
If (r> m) RET + = query (L, R, rson );
Return ret;
}
Int main (){
Int N, I, j, INI, ans;
While (~ SD (N )){
Build (0, n-1, 0 );
INI = 0;
Ff (I, n ){
SD (data [I]);
INI + = query (data [I], n, 0, n-1, 0 );
Update (data [I], 0, n-1, 0 );
}
Ans = ini;
Ff (I, n ){
INI + = N-data [I]-data [I]-1;
Ans = min (INI, ANS );
}
PF (ANS );
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.