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

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

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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
   
   
10 1 3 6 9 0 8 5 7 4 2

 


Sample Output
   
   
16

 


Author
CHEN, Gaoli
 


Source
 


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

代码:

#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.net/116730.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月16日 下午5:00
下一篇 2022年1月16日 下午6:00


相关推荐

  • Android Application Thread CPU GC Operatiing and OOM Question 0603-随手笔记

    Android Application Thread CPU GC Operatiing and OOM Question 0603-随手笔记

    2022年1月13日
    53
  • POJ-2499 Binary Tree

    POJ-2499 Binary Tree

    2021年8月22日
    55
  • 虚拟存储技术「建议收藏」

    虚拟存储技术「建议收藏」一.实现内存扩充的技术:(1)覆盖技术:在程序运行中,在不同时刻把同一个存储区分配给不同程序段和数据段,实现存储区共享。适用于连续存储(单一连续区分配,分区)如图BDG共享一个存储区(三个进程不同时发生),CEFH同理(2)交换技术(对换技术):1.定义:将内存中某进程的的程序和数据(全部或部分)写入外存的交换区,从而腾出内存空间给其他进程使用。2.相关涉及知识

    2026年4月14日
    2
  • 六万字最全总结Java数据库编程MyBatis(+收藏)

    六万字最全总结Java数据库编程MyBatis(+收藏)前言今天复习 Java 数据库编程 二话不说 MyBatis 手把手保姆级教程献上 再也不用担心学不会 资料 链接 https pan baidu com s 1FIDi 9QiTuhb4x7pk 提取码 kevf1 MyBatis 入门文章目录前言 1 MyBatis 入门 1 1 概述 1 2 下载 1 3 与 JDBC 对比 1 4 入门案例 搭建环境 1 4 1 构建项目 1 4 2 数据库和表 User1 5 入门案例 查询所有 1 5 1JavaBean User1 5 2 编写 Dao Us

    2026年3月16日
    2
  • 如何检查并清除挖矿程序

    如何检查并清除挖矿程序1 检查 cpu 使用率根据 cpu 使用率曲线确定 2 11 日可能被注入挖矿程序 根据 top 确定挖矿程序进程 kdevtmpfsi2 确定挖矿进程源程序位置 find namekdevtmpf 查看安装时间 对比 cpu 突然拔升时间 3 检查 psadm2 用户的合法性 4 检查 root 或者 psadm2 用户下是否有定时挖矿的复制文件任务 crontab lcrontab r 删除定时任务 5 杀死挖矿进程 pkillkdevtmp 删

    2026年3月17日
    1
  • 浅谈Android指纹识别技术[通俗易懂]

    浅谈Android指纹识别技术[通俗易懂]浅谈Android指纹识别技术当今时代,随着移动智能手机的普及,指纹解锁早已是手机不可或缺的一个功能。除了现在比较新款的iPhone或者部分手机采用了FaceID之外,人们几乎天天都会用到指纹解锁技术。但你知道指纹解锁技术背后的原理吗?原理指纹识别的前提是对指纹的采集,所以我们首先就应该了解第一步:指纹采集。第一步:指纹采集指纹采集主要分为两种方式:滑动式采集和按压式采集滑动式采集是将手指在传感器上滑过,从而使手机获得手指指纹图像。滑动式采集具有成本相对偏低,而且可以采集大面积图像的优势。但这

    2022年8月10日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号