hdu 1394 Minimum Inversion Number

简介:

hdu 1394 的传送门

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 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

题目大意:
给你一个n个数的排列,然后可以将第一个元素放到最后一个,求这n个数列的最小的逆序数Min;
解题思路:

它的逆序列个数是N个,如果把t[i]放到t[N]后面,逆序列个数会减少t[i]个,相应会增加N-(t[i]+1)个
如果暴力的话,估计会超时,所以就用线段数搞一下,,注意线段树只是一个工具,!!

上代码:

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=5005;
int num[maxn];
struct
{
    int l, r, num;
} tree[4*maxn];

void build(int root, int l, int r)
{
    tree[root].l=l;
    tree[root].r=r;
    tree[root].num=0;
    if(l == r)
        return;
    int mid=(l + r)>>1;
    build(root<<1, l, mid);
    build(root<<1|1, mid+1, r);
}

void update(int root, int pos)
{
    if(tree[root].l == tree[root].r)
    {
        tree[root].num++;
        return;
    }
    int mid=(tree[root].l+tree[root].r)>>1;
    if(pos<=mid)
        update(root<<1, pos);
    else
        update(root<<1|1, pos);
    tree[root].num=tree[root<<1].num+tree[root<<1|1].num;
}

int query(int root, int L, int R)
{
    if(L <= tree[root].l && R >= tree[root].r)
        return tree[root].num;
    int mid=(tree[root].l+tree[root].r)/2,ret=0;
    if(L <= mid)
        ret += query(2*root, L, R);
    if(R>mid)
        ret+=query(2*root+1, L, R);
    return ret;
}
int main()
{

    int m;
    while(~scanf("%d",&m))
    {
        int sum=0;
        build(1, 0, m);
        for(int i=1; i<=m; i++)
        {
            scanf("%d",&num[i]);
            update(1, num[i]);
            sum+=query(1, num[i]+1, m);
        }
        int ans=sum;
        for(int i=1; i<m; i++)
        {
            sum+=(m-1-num[i])-num[i];
            if(sum<ans)
                ans=sum;
        }
        printf("%d\n",ans);
    }
    return 0;
}
目录
相关文章
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
|
人工智能 Java
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
139 0
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
121 0
HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
121 0
HDU 1005 Number Sequence
Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 115124    Accepted Su...
805 0
|
人工智能 Java BI
KMP - HDU 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11606    Accepted Submission...
821 0
|
人工智能 Java Windows
枚举 + 进制转换 --- hdu 4937 Lucky Number
Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 294    Accepted Submission(s):...
838 0
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
104 1
|
6月前
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】