Question address: Minimum inversion number
Question:
Calculate the logarithm of the reverse order, and the minimum value of the number of the reverse order after the cyclic shift, meaning that the first digit is moved to the last digit at a time, and then calculate the logarithm of the reverse order to find the smallest one.
Solution:
Array.
Line Segment tree code.
Code:
1 # include <algorithm> 2 # include <iostream> 3 # include <sstream> 4 # include <cstdlib> 5 # include <cstring> 6 # include <cstdio> 7 # include <string> 8 # include <bitset> 9 # include <vector> 10 # include <queue> 11 # include <stack> 12 # include <cmath> 13 # include <list> 14/ /# include <map> 15 # include <set> 16 using namespace STD; 17 /************************************** */18 # define ll long long19 # define in T64 _ int6420 # define PI 3.141592721 /******************************* * ******/22 const int INF = 0x7f7f7f7f; 23 const double EPS = 1e-8; 24 const double PIE = ACOs (-1.0); 25 const int d1x [] = {0,-, 1 }; 26 const int d1y [] = {-,}; 27 const int d2x [] = {0,-, 1}; 28 const int d2y [] =, -}; 29 const int FX [] = {-1,-1,-,}; 30 const int FY [] = {-, 1, -,-, 1}; 31 const int dirx [] = {-,-2, 2,-,-}; 32 const int diry [] = {-2,-2,-1,-, 2 }; 33/* vector <int> map [N]; Map [A]. push_back (B); int Len = map [v]. size (); */34 /************************************ ***/35 void openfile () 36 {37 freopen ("data. in "," rb ", stdin); 38 freopen (" data. out "," WB ", stdout); 39} 40 priority_queue <int> qi1; 41 priority_queue <int, vector <int>, greater <int> qi2; 42/*********************** Lili split line, the above is the template section **************** */43 const int M = 5010; 44 int C [m]; 45 int N; 46 int lowbit (int x) 47 {48 return X & (-x ); 49} 50 int sum (int x) 51 {52 int ret = 0; 53 While (x> 0) 54 {55 RET + = C [x]; 56 X-= lowbit (x); 57} 58 return ret; 59} 60 void add (int x, int Val) 61 {62 while (x <= N) 63 {64 C [x] + = val; 65 x + = lowbit (x); 66} 67} 68 int main () 69 {70 int A [m]; 71 while (scanf ("% d", & N )! = EOF) 72 {73 int I, j; 74 int CNT = 0; 75 memset (C, 0, sizeof (c); 76 memset (A, 0, sizeof (a); 77 for (I = 0; I <n; I ++) 78 {79 scanf ("% d", & A [I]); 80 A [I] ++; 81 CNT + = sum (N)-sum (A [I]); 82 add (A [I], 1 ); 83} 84 int min = CNT; 85 for (INT I = 0; I <n; I ++) 86 {87 CNT + = n-A [I]-(A [I]-1); 88 If (CNT <min) 89 min = CNT; 90} 91 printf ("% d \ n", min); 92} 93 return 0; 94}View code
Hdu1394 (minimum inversion number)