码迷,mamicode.com
首页 > 编程语言 > 详细

Inversion_树状数组

时间:2016-11-26 23:16:30      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:space   题意   owb   return   string   树状数组   ora   color   msu   

Problem Description
You have a sequence {a1,a2,...,an} and you can delete a contiguous subsequence of length m. So what is the minimum number of inversions after the deletion.
 

 

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integers n,m(1n105,1m<n) - the length of the seuqence. The second line contains n integers a1,a2,...,an(1ain).

The sum of n in the test cases will not exceed 2×106.
 

 

Output
For each test case, output the minimum number of inversions.
 

 

Sample Input
2 3 1 1 2 3 4 2 4 1 3 2
 

 

Sample Output
0 1

 

 

【题意】给出n个数,删除其中一个长度为m的一个连续序列,求最后最小逆序数

【思路】这题精华很多,有待吸收~~

每一次移动,显然会往这个序列中删除一个数,增加一个数

1.加入一个数:多了它后面所有比它小的数,多了它前面所有比它大的数

2.删除一个数:少了它后面所有比它小的数,少了它前面所有比它大的数

用两个树状数组动态维护删除的序列前面和后面部分。

直接memset会TLE,需要限制一下清空的范围。(大神让我学了一招!!!)

参考:http://blog.csdn.net/weizhuwyzc000/article/details/49745569

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=100000+10;
int n,m,a[N];
long long b[N*4],c[N*4];
int lowbit(int x)
{
    return x&(-x);
}
long long query(long long *d,int x)
{
    int res=0;
    while(x)
    {
        res+=d[x];
        x-=lowbit(x);
    }
    return res;
}
void update(long long *d,long long x,long long v )
{
    while(x<=n)
    {
        d[x]+=v;
        x+=lowbit(x);
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int cnt=0,ans=inf;
        scanf("%d%d",&n,&m);
        memset(b,0,(n+3)*sizeof(long long));
        memset(c,0,(n+3)*sizeof(long long));
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1+m;i<=n;i++)
        {
            cnt+=i-m-1-query(b,a[i]);
            update(b,a[i],1);
        }
        ans=cnt;
        for(int i=m+1;i<=n;i++)
        {
            cnt+=query(b,a[i-m]-1);
            cnt+=query(c,n)-query(c,a[i-m]);
            update(c,a[i-m],1);
            cnt-=query(b,a[i]-1);
            cnt-=query(c,n)-query(c,a[i]);
            update(b,a[i],-1);
            ans=min(ans,cnt);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

Inversion_树状数组

标签:space   题意   owb   return   string   树状数组   ora   color   msu   

原文地址:http://www.cnblogs.com/iwantstrong/p/6105295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!