首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >HDU 1394 Minimum Inversion Number (数据结构-段树)

HDU 1394 Minimum Inversion Number (数据结构-段树)

作者头像
全栈程序员站长
发布2022-07-06 19:54:58
发布2022-07-06 19:54:58
19800
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是全栈君

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9514 Accepted Submission(s): 5860

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

代码语言:javascript
代码运行次数:0
运行
复制
    10
1 3 6 9 0 8 5 7 4 2

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
    16

Author

CHEN, Gaoli

Source

ZOJ Monthly, January 2003

Recommend

Ignatius.L

题目大意:

求逆序数。也就是给你一个序列,每次求逆序数,然再把第一个数放到这个序列的末尾,构成新的序列。问你这n个序列的最小的逆序数。

解题思路:

1、对于每一个序列。其原来的逆序数记为 pre , 假设当前把该序列 第一个数 a[0] 移动到尾部,那么新序列的逆序数为 pre-a[i]+(n-a[i]-1)

由于序列中比a[i]大的数有 n-a[i]-1 个。比a[i]小的有 a[i]个。

因此仅仅需求出第一个序列的逆序数,依次能够递推出这n个序列的逆序数,求出最小的就可以

2、求第一个序列的逆序数的方法

(1)暴力算法,据说不会超时

(2)线段树,建 [0,n]这段树。对于数据 a[i] ,先查询 (a[i]+1,n) 这段的值也就是比a[i]大的数的个数也就是 逆序数,然后插入 (a[i],a[i]) 值为1

代码:

代码语言:javascript
代码运行次数:0
运行
复制
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=5100;

struct tree{
    int l,r,sum;
}a[maxn*4];

int data[maxn],n,m;

void build(int l,int r,int k){
    a[k].l=l;
    a[k].r=r;
    a[k].sum=0;
    if(l<r){
        int mid=(l+r)/2;
        build(l,mid,2*k);
        build(mid+1,r,2*k+1);
    }
}

void insert(int l,int r,int k,int c){
    if(l<=a[k].l && a[k].r<=r){
        a[k].sum+=c;
    }else{
        int mid=(a[k].l+a[k].r)/2;
        if(r<=mid) insert(l,r,2*k,c);
        else if(l>=mid+1) insert(l,r,2*k+1,c);
        else{
            insert(l,mid,2*k,c);
            insert(mid+1,r,2*k+1,c);
        }
        a[k].sum=a[2*k].sum+a[2*k+1].sum;
    }
}

int query(int l,int r,int k){
    if(l<=a[k].l  && a[k].r<=r){
        return a[k].sum;
    }else{
        int mid=(a[k].l+a[k].r)/2;
        if(r<=mid) return query(l,r,2*k);
        else if(l>=mid+1) return query(l,r,2*k+1);
        else{
            return query(l,mid,2*k) + query(mid+1,r,2*k+1) ;
        }
    }
}

void solve(){
    int ans=0;
    build(1,n,1);
    for(int i=1;i<=n;i++){
        ans+=query(data[i]+1,n,1);
        insert(data[i]+1,data[i]+1,1,1);
        //cout<<data[i]<<" "<<ans<<endl;
    }
    int tmp=ans;
    for(int i=1;i<=n;i++){
        tmp-=data[i];
        tmp+=n-data[i]-1;
        if(tmp<ans) ans=tmp;
    }
    cout<<ans<<endl;
}

int main(){
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++) scanf("%d",&data[i]);
        solve();
    }
    return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116730.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Minimum Inversion Number
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档