HDU 1394 Minimum Inversion Number (线段树,暴力)

Description

The inversion number of a given number sequence a1, a2, ..., an 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 will obtain another sequence. There are 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 are 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 two 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 on a single line.

Sample Input

10

1 3 6 9 0 8 5 7 4 2

Sample Output

16

这题就是求逆序数题,给你一个数列,找出它的逆序数,然后通过以上变换分别求出变换后的逆序数,输出最小值就是答案了。

假设ans是初始逆序数,那么他的下一个数列的逆序数ans1=ans+n-2*a[0]-1;因为把a[0]移到后面逆序数就要减少a[0],也要加上n-a[0]-1;如果这看懂了,这题就不难了,数据不大,求逆序数用线段树,暴力什么都能解决。

暴力:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int n,a[100000],i,j,ans1,ans2;
    while (~scanf("%d",&n))
    {
        ans1=0;
        for (i=0;i<n;i++) scanf("%d",&a[i]);
        for (i=1;i<n;i++)
        for (j=0;j<i;j++)
        if (a[i]<a[j]) ans1++;
        ans2=ans1;
        for (i=0;i<n;i++)
        {
            ans1=ans1+n-2*a[i]-1;
            ans2=min(ans1,ans2);
        }
        printf("%d\n",ans2);
    }
    return 0;
}

  

线段树

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int s[50000];
struct p
{
    int x,y,su;
};p tree[1000000];
void build(int l,int r,int p)
{
    tree[p].x=l;
    tree[p].y=r;
    tree[p].su=0;
    if (l==r) return ;
    int m=(l+r)/2;
    build(l,m,2*p);
    build(m+1,r,2*p+1);
    return ;
}
int find(int l,int r,int p)
{
    if (tree[p].x==l&&tree[p].y==r) return tree[p].su;
    int m=(tree[p].x+tree[p].y)/2;
    if (l>m) return find(l,r,2*p+1);
    if (m>=r) return find(l,r,2*p);
    return find(l,m,2*p)+find(m+1,r,2*p+1);
}
void un(int pos,int i,int p)
{
    tree[p].su+=i;
    if (tree[p].x==tree[p].y) return ;
    int m=(tree[p].x+tree[p].y)/2;
    if (pos<=m) un(pos,i,2*p);
    else un(pos,i,2*p+1);
}
int main()
{
    int n,i,ans,mn;
    while (~scanf("%d",&n))
    {
        build(0,n-1,1);
        ans=0;
        for (i=0;i<n;i++) scanf("%d",&s[i]);
        for (i=0;i<n;i++)
        {
            ans+=find(s[i],n-1,1);
            un(s[i],1,1);
        }
        mn=ans;
        for (i=0;i<n;i++)
        {
            ans=ans+n-2*s[i]-1;
            mn=min(mn,ans);

        }
        printf("%d\n",mn);
    }
    return 0;
}

  

时间: 2024-07-29 03:38:18

HDU 1394 Minimum Inversion Number (线段树,暴力)的相关文章

HDU 1394 Minimum Inversion Number.(线段树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 ~~~~ 早起一发线段树,开心又快乐.这题暴力也能水过,同时线段树的效率也就体现的尤为明显了,看了大牛的博客,说是还可以用树状数组,点树和合并序列写,现在还不懂,留着以后在写吧. ~~~~ 大致题意:给定一个数字序列,同时由此可以得到n个序列, 要求从n个序列中找到逆序数最小的序列,输出最小逆序数. 首先介绍下逆序数的概念: 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面

hdu 1394 Minimum Inversion Number 线段树 点更新

// hdu 1394 Minimum Inversion Number 线段树 点更新 // // 典型线段树的单点更新 // // 对于求逆序数,刚开始还真的是很年轻啊,裸的按照冒泡排序 // 求出最初始的逆序数,然后按照公式递推,结果就呵呵了 // // 发现大牛都是用线段树和树状数组之类的做的,而自己又在学 // 线段树,所以就敲了线段树. // // 线段树的节点保存一段区间( L,R )内0,1...n一共出现了多少个. // 因为每个数是0,1,2...n-1且没有重复的数字. /

HDU 1394 Minimum Inversion Number (线段树,单点更新)

C - Minimum Inversion Number Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1394 Appoint description: System Crawler (2015-08-17) Description The inversio

Hdu 1394 Minimum Inversion Number(线段树或树状数组)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11981    Accepted Submission(s): 7321 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu - 1394 Minimum Inversion Number(线段树水题)

http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum就是当前数的逆序数. 这样查询完之后得到初始数列的逆序数,要求得所有序列的最小逆序数,还需要循环一次. 设初始序列abcde中逆序数为k,小于a的个数是t-1那么大于a的个数就是n-t,当把a左移一位,原来比a大的都变成了a的逆序对,即逆序数增加了n-t,但是原来比a小的数都变成了顺序, 因此逆序数

hdu 1394 Minimum Inversion Number 线段树

Problem Description The inversion number of a given number sequence a1, a2, ..., an 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

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列: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)求这些序列中,逆序数最少的

HDU 1394 Minimum Inversion Number(树状数组||线段树)

题目链接:点击打开链接 对于求逆序数的问题, 通常用线段树或者树状数组来维护, 树状数组代码短,好写, 还是尽量写树状数组吧. 首先求出原始排列的逆序数,  那么对于每一次操作, 因为都是将当前排列的第一个数拿到最后一个位置, 所以答案就增加了所有比他大的数字个数,减小了所有比他小的数字个数. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #

hdu 1394 Minimum Inversion Number(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个0 — n-1的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最多逆序对的个数 求出原始序列的逆序对的数目,然后进行n-1次将第一个元素放到最后一个的操作,每次操作后可以用O(1)复杂度求得新序列的逆序对数目 此题的关键点在于求出原始序列逆序对的数目,可以使用树状数组, 线段树, 归并等方法. 下面是树状数组的解法 #include <iostream> #i