HDU5497 Inversion

题解:

将数组分为3部分,左边的A,中间删除B,右边的C,

然后中间删除部分往右移动,左边A增加一个元素,右边C删除一个元素

左边增加一个元素对A,C的影响,右边增加一个元素对A,C的影响想想就清楚了~

然后树状数组就行了~,也是有2种书写方式。

但是这题神TM的卡时间,别直接memset

代码:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define LL long long
#define CLR(x) memset(x,0,sizeof x)
#define MC(x,y) memcpy(x,y,sizeof(x))
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=100050;
const int mod=1e9+7;
const int INF=1e9;

LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}

int n,m,a[maxn];
LL c[maxn],b[maxn];

int lowbit(int x){return x&-x;}

void add(int x,int v,LL* c){
    while(x){
        c[x]+=v;
        x-=lowbit(x);
    }
}

LL sum(int x,LL* c){
    LL cnt=0;
    while(x<=n){
        cnt+=c[x];
        x+=lowbit(x);
    }
    return cnt;
}

int main(){
    int T;
    T=read();
    while(T--){
        n=read();m=read();
        for(int i=1;i<=n;i++) a[i]=read();
        memset(b,0,(n+3)*sizeof(LL));
        memset(c,0,(n+3)*sizeof(LL));
        LL ans=0;
        for(int i=m+1;i<=n;i++){
            ans+=sum(a[i]+1,c);
            add(a[i],1,c);
        }
        LL Min=ans;
        for(int i=1;i<=n-m;i++){
            int l=i,r=i+m;
            LL tmp=ans;
            tmp+=sum(a[l]+1,b);
            add(a[r],-1,c);
            tmp+=n-m-i-sum(a[l],c);
            tmp-=sum(a[r]+1,b);
            tmp-=n-m-i-sum(a[r],c);
            add(a[l],1,b);
            Min=min(Min,tmp);
            ans=tmp;
        }
        printf("%lld\n",Min);
    }
    return 0;
}
时间: 2024-10-11 06:17:56

HDU5497 Inversion的相关文章

hdu-5497 Inversion(滑动窗口+树状数组)

题目链接: Inversion Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1087    Accepted Submission(s): 323 Problem Description You have a sequence {a1,a2,...,an} and you can delete a contiguous subsequ

Minimum Inversion Number 【线段数】

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

hdu 1394 Minimum Inversion Number(这道题改日我要用线段树再做一次哟~)

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

HDU1394 Minimum Inversion Number 线段树+数学

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

HDOJ 1394 Minimum Inversion Number 求循环串的最小逆序数(暴力&amp;&amp;线段树)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14879    Accepted Submission(s): 9082 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu(1394)——Minimum Inversion Number

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

hdu 1394 Minimum Inversion Number 逆序数/树状数组

Minimum Inversion Number Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1394 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

CSUOJ 1555 Inversion Sequence

1555: Inversion Sequence Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 107  Solved: 34 Description For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence which are prior to j and greater to j at the same time. The seq

hdu 1394 Minimum Inversion Number

题目链接:hdu 1394 Minimum Inversion Number 该题是求最小逆序对的扩展.可以使用树状数组来实现.对于$n$个数的序列$A$,其第$i$个数($i\in [0,n)$)的逆序数$r_i$可以表示为它的角标$i$减去在它之前且不大于它的数的个数.例如对序列A = {1,3,5,9,0,8,5,7,4,2}中的数,A[8] = 4.其逆序数$r_8 = 8 - 3 = 5$,第二个3表示三个在它前面且比它小的数:{1,3,0}.从而我们可以得到第$i$个数的逆序数公式: