HDU (1394)--minimum inversion number

Source: Internet
Author: User
Tags printf

Problem Description the inversion number of a given number sequence A1, A2, ..., the number of pairs (AI, AJ) that s Atisfy 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

10 1 3 6 9 0 8 5 7 4 2
Sample Output
16

I thought about it later, why the question is to use a line tree. The original segment tree is a helper, and he can help us find the interval more quickly, rather than saying it can help us solve the problem completely.

The general meaning of this question is: give you an inverse number, and then each time you can move the first number to the last position, and then ask you this n-1 sequence of each of the number of reverse order, and then ask you all of these sequences in the inverse number of the minimum value is how much.

This problem seems to have four ways, and now I only know 2 kinds, the rest of the later update.

1: The first idea is to find the law, we first found the first sequence of the number of reverse order, credited as sum, and then each time the first move to the last one will reduce the sum of a[i], but will increase N (a[i]+1). If this rule is not understood, you can give a few sets of examples to think about. Then for n times, you can find the smallest number of reverse order.

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MAXN 5555
#define INF 99999999
int A[MAXN];
int main () {
    int n;
    while (~SCANF ("%d", &n)) {
        int min1=inf,num=0;
        for (int i=0;i<n;i++) scanf ("%d", &a[i]);
        for (int i=0;i<n;i++) {for
            (int j=i+1;j<n;j++) {
                if (a[i]>a[j]&&i<j) num++;
            }
        }
        if (min1>num) min1=num;
        The following is the rule of law,
        or//each time the first move to the end, the logarithm of the number of reverse order will reduce a[i], but will increase N (a[i]+1), for a few examples to know the for 
        (int i=0;i<n;i++) {
            num=num-a[i]+n-(a[i]+1);
            if (min1>num) min1=num;
        }
        printf ("%d\n", min1);
    }
}
/*
4
1 3 2 0
*/

The second idea is the line tree,

This is to tell the truth some of the problems are not detailed enough to write, and then I read a few days is the real understanding.

Here to recommend this person to write a blog: http://blog.sina.com.cn/s/blog_691ce2b70101ldmm.html

You can take a look at his ideas, but you don't have to learn how to spell the tree of his line.

The idea is that we look at the number of numbers that are larger and have occurred each time we read the number of n in order.

For example: There are 10 numbers, then we have read into the 3,6,7, and then enter 0 to go to the range of 1~10 to ask the number has already appeared, then those numbers is its reverse number. So in this case it is 3,6,7, so it has 3 reverse-order numbers.

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace
Std
#define MAXN 5555 #define INF 99999999 int A[MAXN]; struct node{int l,r,sum;}
TREE[MAXN*4];
int ans=0;
	void pushup (int v) {int temp=v*2;
Tree[v].sum=tree[temp].sum+tree[temp+1].sum;
	} void Build (int l,int R,int v) {tree[v].l=l;
	Tree[v].r=r;
	tree[v].sum=0;
	if (l==r) return;
	int temp=v*2;
	int mid= (L+R)/2;
	Build (L,mid,temp);
Build (mid+1,r,temp+1);
	} int query (int l,int R,int v) {if (L==TREE[V].L&AMP;&AMP;R==TREE[V].R) {return tree[v].sum;
	} int temp=v*2;
	int mid= (TREE[V].L+TREE[V].R)/2;
	if (r<=mid) return query (L,R,TEMP);
	else if (l>mid) return query (l,r,temp+1);
	else{return Query (l,mid,temp) +query (mid+1,r,temp+1);
		}} void Update (int pos,int v) {if (TREE[V].L==TREE[V].R) {tree[v].sum++;
	return;
	} int mid= (TREE[V].L+TREE[V].R)/2;
	int temp=v*2;
	if (pos<=mid) update (POS,TEMP);
	else update (POS,TEMP+1);		Pushup (v); 
Remember here to pushup ... }
int main () {int n;
		while (~SCANF ("%d", &n)) {memset (a,0,sizeof (a));
		for (int i=0;i<n;i++) scanf ("%d", &a[i]);
		Build (0,n-1,1);
		int sum=0,min1=inf;
			for (int i=0;i<n;i++) {ans=query (a[i],n-1,1);
			Sum+=ans;
		Update (a[i],1);
			} for (int i=0;i<n;i++) {sum+=n-(a[i]+1)-a[i];
		if (sum<min1) min1=sum;
	} printf ("%d\n", min1); }}/* 10 1 3 6 9 0 8 5 7 4 2 4 1 3 2 0 * *
In fact, the following part of the same as the first method is the same, so the line tree maintenance or to find out the sum of the process (that is, the number of reverse order of the original series).

Multi-accumulation, multi-AC, refueling.

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.