HDU 5497 Inversion
Time Limit: 3000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
Input
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
Sample Input
3 1
1 2 3
4 2
4 1 3 2
Sample Output
1
Source
滑动区间扫描。用树状数组来维护逆序对的个数(数组存储数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的更多相关文章
- hdu 5497 Inversion 树状数组 逆序对,单点修改
Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...
- HDU 6098 - Inversion | 2017 Multi-University Training Contest 6
/* HDU 6098 - Inversion [ 贪心,数论 ] | 2017 Multi-University Training Contest 6 题意: 求出所有B[i] = max(A[j] ...
- HDU 1394Minimum Inversion Number 数状数组 逆序对数量和
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU 4911 Inversion
http://acm.hdu.edu.cn/showproblem.php?pid=4911 归并排序求逆对数. Inversion Time Limit: 2000/1000 MS (Java/ ...
- hdu 4911 Inversion(找到的倒数)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 Inversion Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 4911 Inversion (逆序数 归并排序)
Inversion 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/A Description bobo has a sequen ...
- hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场
Inversion Time Limit: 20 ...
- HDU 6098 Inversion
Inversion 思路:从大到小排序后,每次找到第一个下标不整出i的输出. 代码: #include<bits/stdc++.h> using namespace std; #defin ...
- HDU 1394Minimum Inversion Number
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
随机推荐
- unity3D里面的点乘和叉乘
在unity3D里面.两个向量的点乘所得到的是两个向量的余弦值,也就是-1 到1之间,0表示垂直,-1表示相反,1表示相同方向. 两个向量的叉乘所得到的是两个向量所组成的面的垂直向量,分两个方向. 简 ...
- QTP学习一添加默认的注释及调用外部vbs文件
一.添加默认注释 1.新建一个TXT文档,将要添加的注释写在文档中 2.再将文档名改为:ActionTemplate.mst 3.将文件放到QTP安装目录(默认为:C:\Program Files\H ...
- mongoVUE1.5.3 破解方法
MongoVUE是个免费软件,但超过15天后功能受限.可以通过删除以下注册表项来解除限制: [HKEY_CURRENT_USER\Software\Classes\CLSID\{B1159E65-82 ...
- linux运维中的命令梳理(一)
在linux日常运维中,我们平时会用到很多常规的操作命令. 下面对常用命令进行梳理: 命令行日常系快捷键(不分大小写)CTRL + A 移动光标到行首CTRL + E 移动光标到行末CTRL + U ...
- 获取一个字符串Hello world中world首次出现的位置
获取一个字符串Hello world中world首次出现的位置 var str=“Hello world!” document.write(str.indexOf("world") ...
- EasyUI Tree判断节点是否是叶
方法1: $('#domaincatalog').tree('isLeaf', node.target); 返回true或false ,true表示是叶节点, false即不是 方法2:官方文档中: ...
- rem详解及使用方法
好像有一段时间没有写博客了……今天刚好总结一下rem的使用方法 首先,先说一个常识,浏览器的默认字体高都是16px.步入正题-----〉 兼容性: 目前,IE9+,Firefox.Chrome.Saf ...
- Express使用手记:核心入门
入门简介 Express是基于nodejs的web开发框架.优点是易上手.高性能.扩展性强. 易上手:nodejs最初就是为了开发高性能web服务器而被设计出来的,然而相对底层的API会让不少新手望而 ...
- 论Visual Studio和.NET Framework
今天在工作的时候听到一席谈话感觉有点不可思议,微软真的是把开发人员惯的有点傻了,微软流水线式的产品让很多开发者认定了"唯一",这当然也说明了微软的成功,不扯太多题外话,今天只是简单 ...
- Implementing SQL Server Row and Cell Level Security
Problem I have SQL Server databases with top secret, secret and unclassified data. How can we estab ...