hdu5497 Inversion 树状数组 待补完!!!

Inversion

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1174    Accepted Submission(s): 351


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
 

Source
 
半年前的问题一直困然至今。。
题意:一个数组,你可以从中删除连续的k个数,求一个最优的方案使得操作后的数组内逆序数对最少。

学长讲过这个,但是暑假的我还是在无限wa。。。枚举删除连续k个数的起点,每次
啊啊啊回头在写。。。。
待补完
/*
 ━━━━━┒
 ┓┏┓┏┓┃μ'sic foever!!
 ┛┗┛┗┛┃\○/
 ┓┏┓┏┓┃ /
 ┛┗┛┗┛┃ノ)
 ┓┏┓┏┓┃
 ┛┗┛┗┛┃
 ┓┏┓┏┓┃
 ┛┗┛┗┛┃
 ┓┏┓┏┓┃
 ┛┗┛┗┛┃
 ┓┏┓┏┓┃
 ┃┃┃┃┃┃
 ┻┻┻┻┻┻
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <stack>
#include <vector>
using namespace std;
const int maxn=1e5;
int save[maxn+5];
int before[maxn+5];
int after[maxn+5];
int n;
int lowbit(int x){
    return x&(-x);
}
void update(int x,int val,bool type){
    if(type){
        while(x<=maxn){
            before[x] += val;
            x += lowbit(x);
        }
    }else{
        while(x<=maxn){
            after[x] += val;
            x += lowbit(x);
        }
    }
}
int getsum(int x,bool type){
    int sum=0;
    if(type){
        while(x){
            sum += before[x];
            x -=lowbit(x);
        }
    }else{
        while(x){
            sum += after[x];
            x -=lowbit(x);
        }
    }
    return sum;
}
int main(){
    int t;
    scanf("%d",&t);
    int m,i;
    while(t--){
        memset(before, 0, sizeof(before));
        memset(after, 0, sizeof(after));
        scanf("%d%d",&n,&m);
        long long ans=0;
        for(i=1;i<=n;i++){
            scanf("%d",&save[i]);
        }
        for(i=m+1;i<=n;i++){
            ans += i-m-1-getsum(save[i],false);
            update(save[i], 1, false);
        }
        long long now=ans;
        for(i=1;i<=n-m;i++){
            now -= getsum(save[i+m]-1,false);
            now -= getsum(save[n], true) - getsum(save[i+m], true);
            update(save[i+m], -1, false);
            now += getsum(save[i]-1, false);
            now += getsum(save[n], true) - getsum(save[i],true);
            update(save[i], 1, true);
            ans = min(ans,now);
        }
        printf("%lld\n",ans);
    }
    return 0;
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值