Minimum Inversion Number HDU - 1394(权值线段树)

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

额,不就是线段树代替了树状数组了么。。。。不过正常来讲似乎都是用分治来做吧。。。为了学习所谓的权值线段树。。。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxx 5005
using namespace std;
int c[maxx<<2];
void update(int st,int l,int r,int x)
{
    if(l==r&&l==x)
    {
        c[st]++;
        return;
    }
    int mid=(l+r)>>1;
    if(x<=mid)update(st<<1,l,mid,x);
    else update(st<<1|1,mid+1,r,x);
    c[st]=c[st<<1]+c[st<<1|1];
}
int query(int st,int l,int r,int L,int R)
{
    if(L<=l&&r<=R)
        return c[st];
    int mid=(l+r)>>1;
    int ans=0;
    if(L<=mid)ans+=query(st<<1,l,mid,L,R);
    if(R>mid)ans+=query(st<<1|1,mid+1,r,L,R);
    return ans;
}
int n;
int a[5005];
int main()
{
    while(scanf("%d",&n)==1)
    {
        int now=0;
        int x;
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            a[i]=x+1;
            now+=query(1,1,n,a[i]+1,n);
            update(1,1,n,a[i]);
        }
        int ans=now;
        for(int i=1;i<=n;i++)
        {
            now-=a[i]-1;
            now+=n-a[i];
            ans=min(ans,now);
        }
        cout<<ans<<endl;
    }
    return 0;
}
内容概要:文章详细介绍了电梯门禁(梯控)系统的硬件安装与接线要点。首先强调了梯控板与楼层按键对接的重要性,包括遵循一一对应原则以避免错层、越层问题,允许空层存在以适应实际需求。接着阐述了不同接线方式(COM、NO、NC端口的不同组合)对用户限的影响,如单层限用户刷卡直达指定楼层,多层限用户在特定接线方式下的操作限制。硬件安装方面,强调了无源干触点设计原则以确保电气隔离,防止系统间干扰,以及读卡器接入时的规范要求。文章还介绍了梯控系统的技术原理,如身份验证机制(二维码/IC卡/人脸识别)、消防联动功能(紧急情况下释放所有楼层限),并指出该系统适用于小区、写字楼等场景,支持机器人乘梯SDK扩展。最后,根据不同场景需求提出了适用的接线方式选择,如严格管控场景下选择4.3接线以实现精准限控制,限制多层用户手动选层场景下选择4.1接线并配合软件限设置。; 适合人群:从事电梯安装维护的技术人员、楼宇自动化工程师及相关领域的管理人员。; 使用场景及目标:①指导技术人员正确安装和接线梯控系统,确保系统安全稳定运行;②帮助管理人员了解不同接线方式对用户限的影响,以便根据实际需求选择合适的配置方案;③提升楼宇安全管理和服务质量,特别是在小区、写字楼等场所的应用。; 其他说明:梯控系统的正确安装和接线不仅关系到系统的正常运作,更直接影响到用户的安全和使用体验。因此,在实际操作中务必严格按照规范执行,同时关注最新的技术发展和应用场景变化,以确保系统始终处于最佳状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值