Minimum Inversion numberTime
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 1394Appoint Description:System Crawler (2015-03-30)
Description
The inversion number of a given number sequence A1, A2, ..., is the number of pairs (AI, AJ) that satisfy I < J and Ai > AJ.
For a given sequence of numbers a1, A2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we'll Obtain another sequence. There is totally n such sequences as the following:
A1, A2, ..., An-1, an (where m = 0-the initial seqence)
A2, A3, ..., an, A1 (where m = 1)
A3, A4, ..., an, A1, A2 (where m = 2)
...
An, A1, A2, ..., an-1 (where m = n-1)
You is asked to write a program to find the minimum inversion number out of the above sequences.
Input
The input consists of a number of test cases. Each case consists of the lines:the first line contains a positive integer n (n <= 5000); The next line contains a permutation of the n integers from 0 to n-1.
Output
For each case, output the minimum inversion number in a single line.
Sample Input
101 3 6 9 0 8 5 7 4 2
Sample Output
16 Problem-Solving ideas: 1. The number of reverse order of the initial sequence 2. According to the initial sequence number, the reverse number of the next sequence is handed out 1. When traversing to the current sequence element, the query from this element to the n-1 interval of the reverse and, that is, the element is greater than the element has appeared several (inverse reverse, but the result The node and its parent node are then updated. Traversing to the end, you can find out the reverse order of the sequence. 2. Since it is starting from 0 consecutive numbers for reverse order, so, there is a character from the head of the sequence to take a, the equivalent of the original sequence to reduce A; put in the tail, the equivalent sequence of the reverse order to increase n-1-a;
#include <stdio.h> #include <algorithm> #include <string.h> #define MID (L+r)/2#define Lson Rt*2,l,mid #define Rson rt*2+1,mid+1,rconst int maxn=5500;int num[maxn*4];void pushup (int rt) {num[rt]=num[rt*2]+num[rt*2+1];} void build (int rt,int l,int R) {num[rt]=0; if (l==r) return; Build (Lson); Build (Rson);} int query (int rt,int l,int r,int l_ran,int R_ran) {if (L_ran<=l&&r<=r_ran) {return NUM[RT]; } int ret=0; if (l_ran<=mid) {ret+=query (Lson,l_ran,r_ran); } if (R_ran>mid) {ret+=query (Rson,l_ran,r_ran); } return ret;} void update (int rt,int l,int r,int pos) {if (l==r) {num[rt]++; return; } if (Pos<=mid) {update (LSON,POS); } if (Pos>mid) {update (RSON,POS); } pushup (RT);} int main () {int n; while (scanf ("%d", &n)!=eof) {build (1,0,n-1); int sum=0; int a[5050]; for (int i=0;i<n;i++) {scanf ("%d", &a[I]); int Tmp=query (1,0,n-1,a[i],n-1); sum+=tmp; Update (1,0,n-1,a[i]); } int ans=sum; for (int i=0;i<n;i++) {sum=sum+n-2*a[i]-1; if (ans>sum) {ans=sum; }} printf ("%d\n", ans); } return 0;}
HDU 1394--minimum Inversion number —————— "segment tree single point increment, Interval sum"