Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Description

You have a sequence \{a_1,a_2,...,a_n\} 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 (1 \le n \le 10^5, 1 \le m < n) - the length of the seuqence. The second line contains n integersa_1,a_2,...,a_n (1 \le a_i \le n).

The sum of n in the test cases will not exceed 2 \times 10^6.

 

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

BestCoder Round #58 (div.2)

滑动区间扫描。用树状数组来维护逆序对的个数(数组存储数n是否已经出现,通过计算数组前缀和来求逆序对,思路和比这篇早些写的POJ2182 Lost Cows类似)

维护L([i])树状数组表示i左边比a[i]的数的数量,R([i])树状数组表示i右边比a[i]小的数的数量。窗口从左滑到右面,不断更新L和R,计算答案并更新

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int n,m;
struct tree{
int a[mxn];
inline lowbit(int x){return x&-x;}
void init(){memset(a,,sizeof a);}
void add(int x,int num){
while(x<=n){
a[x]+=num;
x+=lowbit(x);
}
return;
}
int sum(int x){
int res=;
while(x){
res+=a[x];
x-=lowbit(x);
}
return res;
}
};
int a[mxn];
tree Le,Ri;
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
int i,j;
for(i=;i<=n;i++){
scanf("%d",&a[i]);
}
Le.init();
Ri.init();
long long ans;
long long tmp=;
//求原有逆序对数(最左边滑动窗口内的不算)
for(i=n;i>m;i--){
Ri.add(a[i],);
tmp+=Ri.sum(a[i]-);
}
ans=tmp;
//finish
for(i=;i<=n-m;i++){
Ri.add(a[i+m],-);//窗口滑动,窗口最右面的数被删除
tmp=tmp+Ri.sum(a[i]-)-Ri.sum(a[i+m]-);
tmp=tmp+Le.sum(n+-(a[i]+))-Le.sum(n+-(a[i+m]+));
Le.add(n+-a[i],);
ans=min(ans,tmp);
}
printf("%lld\n",ans);
}
return ;
}

HDU 5497 Inversion的更多相关文章

  1. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  2. HDU 6098 - Inversion | 2017 Multi-University Training Contest 6

    /* HDU 6098 - Inversion [ 贪心,数论 ] | 2017 Multi-University Training Contest 6 题意: 求出所有B[i] = max(A[j] ...

  3. HDU 1394Minimum Inversion Number 数状数组 逆序对数量和

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  4. HDU 4911 Inversion

    http://acm.hdu.edu.cn/showproblem.php?pid=4911   归并排序求逆对数. Inversion Time Limit: 2000/1000 MS (Java/ ...

  5. hdu 4911 Inversion(找到的倒数)

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 Inversion Time Limit: 2000/1000 MS (Java/Others) ...

  6. HDU 4911 Inversion (逆序数 归并排序)

    Inversion 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/A Description bobo has a sequen ...

  7. hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场

    Inversion                                                                             Time Limit: 20 ...

  8. HDU 6098 Inversion

    Inversion 思路:从大到小排序后,每次找到第一个下标不整出i的输出. 代码: #include<bits/stdc++.h> using namespace std; #defin ...

  9. HDU 1394Minimum Inversion Number

    The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...

随机推荐

  1. C++中的运算符重载

    首先思考以下几个问题: 1.什么是运算符重载? 2.为什么要重载运算符,它有什么用? 3.可以重载哪些运算符? 4.重载运算符有哪些规则? 一.基本概念 我们在程序中使用各种操作符,比如加(+).赋值 ...

  2. javascript获取当前的时间戳

    JavaScript 获取当前时间戳:第一种方法: var timestamp = Date.parse(new Date()); 结果:1280977330000第二种方法: var timesta ...

  3. usb驱动开发3之先看core

    上节中看到usb目录中有一个core目录,凡是认识这个core单词的人都会想要先看看它是什么,对不?用LDD3中一幅图,来表述usb core所处地位. usb core负责实现一些核心的功能,为别的 ...

  4. ESLint 检查代码质量

    利用 ESLint 检查代码质量 其实很早的时候就想尝试 ESLint 了,但是很多次都是玩了一下就觉得这东西巨复杂,一执行检查就是满屏的error,简直是不堪入目,遂放弃.直到某天终于下定决心深入看 ...

  5. mvc4中的 webapi 的使用方式

    目录 一:简单介绍什么是Web api 二:怎么定义的 Post Get Put 和 Delete 三:简单使用,直接从前台传递一个类到后台接收 四:其他获取值的方式 一:简单介绍什么是Web api ...

  6. python 3 安装 scrapy 并运行成功

    今天,python 3 安装 scrapy, 并运行成功.特此纪念! 我的环境:windows 10(64位) + python 3.5.2(64位) 其中几个要点说明一下: 1.有几个依赖库需要事先 ...

  7. MMDrawerController第三方库的使用(根据导航item+滚动条progressView实现的手势滑动切换视图的)

    https://github.com/mutualmobile/MMDrawerController MMDrawerControlleris边抽屉导航容器视图控制器用来支持越来越多的应用程序利用抽屉 ...

  8. 地图坐标转换 -- 火星坐标与GPS坐标

    第一次处理地理位置的数据的人,没什么经验,往往掉入很多坑浪费不少时间.我也是刚刚从坑里爬出来.这篇博文主要是把入门GPS轨迹分析的经验总结一下,以方便大家少走些弯路. (1)可视化 GPS 路径 刚拿 ...

  9. 使用yuicompressor-maven-plugin压缩js及css文件

    本文介绍通过使用yuicompressor-maven-plugin插件实现js及css代码的自动压缩,方便集成到持续集成环境中,如jenkins. 一.配置yuicompressor-maven-p ...

  10. html 空格-有趣的试验

    首先,先给大家看一组demo <input /> <input type="submit" /> 展示效果: 为什么会出现空格呢?input不是行内元素吗? ...