hdu1394 Minimum Inversion Number

本文介绍了一种利用线段树求解特定序列中最小逆序数的算法。该算法适用于给定序列通过循环移位产生的所有可能序列,并找出这些序列中逆序数最小的一个。文章提供了完整的代码实现及解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
就先求第一个顺序的逆序数 。
用线段树求逆序数的方法就是下标代表那个值 0,1代表有没有出现 这样每次查询后面的值之和就是当前值对应的逆序数啦
但是要注意因为从0开始所以建树要从0开始
求玩第一个顺序别的也就好求了
现在把A1从队首挪到队尾 当A1在队首时 后面得数有多少个比它小呢?a1个 挪到队尾A1的逆序数是多少?n-a1-1
#include<bits/stdc++.h>
using namespace std;
const int maxn=5005;
struct node
{
   int l,r,v;
}tree[maxn];
int a[maxn];
void build(int i,int l,int r)
{
    tree[i].l=l;
    tree[i].r=r;
    if(l==r)
    {
        tree[i].v=0;
        return ;
    }
    int m=(l+r)/2;
    build(i*2,l,m);
    build(i*2+1,m+1,r);
    tree[i].v=tree[i*2].v+tree[i*2+1].v;
}
void update(int i,int x,int v)
{
    if(tree[i].l==tree[i].r)
    {
        tree[i].v=1;
        //cout<<tree[i].l<<endl;
        return ;
    }
    int m=(tree[i].l+tree[i].r)/2;
    if(x>m)update(i*2+1,x,v);
    else update(i*2,x,v);
    tree[i].v=tree[i*2].v+tree[i*2+1].v;
}
int query(int i,int x,int y)
{
    int sum=0;
   // cout<<x<<" "<<tree[i].l<<endl;
    if(x<=tree[i].l&&tree[i].r<=y)
    {
        //cout<<"nsdkfjsd"<<endl;
        //cout<<tree[i].l<<" "<<tree[i].r<<endl;
        sum+=tree[i].v;
        return sum;
    }
    int m=(tree[i].l+tree[i].r)/2;
    if(x<=m)sum+=query(i*2,x,y);
    if(y>m)sum+=query(i*2+1,x,y);
    return sum;
}
int main()
{
    int n;
    while(cin>>n)
    {
        build(1,0,n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            sum+=query(1,a[i],n);
            //cout<<i<<" "<<sum<<endl;
            update(1,a[i],1);
        }
        int mmin=sum;
        for(int i=1;i<=n;i++)
        {
            sum-=a[i];
            sum+=n-a[i]-1;
            mmin=min(sum,mmin);
        }
        printf("%d\n",mmin);
    }
    return 0;
}
 


内容概要:本文介绍了奕斯伟科技集团基于RISC-V架构开发的EAM2011芯片及其应用研究。EAM2011是一款高性能实时控制芯片,支持160MHz主频和AI算法,符合汽车电子AEC-Q100 Grade 2和ASIL-B安全标准。文章详细描述了芯片的关键特性、配套软件开发套件(SDK)和集成开发环境(IDE),以及基于该芯片的ESWINEBP3901开发板的硬件资源和接口配置。文中提供了详细的代码示例,涵盖时钟配置、GPIO控制、ADC采样、CAN通信、PWM输出及RTOS任务创建等功能实现。此外,还介绍了硬件申领流程、技术资料获取渠道及开发建议,帮助开发者高效启动基于EAM2011芯片的开发工作。 适合人群:具备嵌入式系统开发经验的研发人员,特别是对RISC-V架构感兴趣的工程师和技术爱好者。 使用场景及目标:①了解EAM2011芯片的特性和应用场景,如智能汽车、智能家居和工业控制;②掌握基于EAM2011芯片的开发板和芯片的硬件资源和接口配置;③学习如何实现基本的外设驱动,如GPIO、ADC、CAN、PWM等;④通过RTOS任务创建示例,理解多任务处理和实时系统的实现。 其他说明:开发者可以根据实际需求扩展这些基础功能。建议优先掌握《EAM2011参考手册》中的关键外设寄存器配置方法,这对底层驱动开发至关重要。同时,注意硬件申领的时效性和替代方案,确保开发工作的顺利进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值