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 ...
随机推荐
- java 16-8 泛型高级之通配符
泛型高级(通配符) ?:任意类型,如果没有明确,那么就是Object以及任意的Java类了 ? extends E:向下限定,E及其子类 ? super E:向上限定,E极其父类 import jav ...
- Mybaits学习总结2
http://www.cnblogs.com/xdp-gacl/p/4262895.html 继续参考这篇文章写Mybaits学习总结 上一章,我修改了编码,统一为UTF8之后,便没有编码错误 < ...
- hadoop面试100道收集(带答案)
1.列出安装Hadoop流程步骤 a) 创建hadoop账号 b) 更改ip c) 安装Java 更改/etc/profile 配置环境变量 d) 修改host文件域名 e) 安装ssh 配置无密码登 ...
- smarty模板继承
- Saltstack-进阶篇
查看minion端的文件内容 [root@linux-node2 ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 1 ...
- cocoaPod相关问题
cocoap简介: 1. 简介 CocoaPods是一个负责管理iOS项目中第三方开源代码的工具,其源码在Github上开源.使用CocoaPods可以节省设置和更新第三方开源库的时间并提高工作效率. ...
- VFS分析(一)挂载(持续更新)
基础知识在<深入linux内核架构>第8章,自行脑补. 看下几个关键的过程: do_add_mount里有重要函数lock_mount, lock_mount函数的输入是struct pa ...
- BPM到底能做什么?K2为你解读
和平镇,镇如其名,几百年来一直很和平,夜不闭户路不拾遗.可是这一年来,镇上金光寺的和尚却开始不断离奇死亡…… 衙门里新调来的李捕头正好负责这个案子,经过了几个月的不眠不休,现场侦查和缜密推理之后,一切 ...
- ab压测&htop工具
1,apache bech 参考: http://www.nginx.cn/110.html http://www.ha97.com/4617.html ===================== y ...
- How to install 64-bit Google Chrome 28+ on 64-bit RHEL/CentOS 6 or 7
How to install 64-bit Google Chrome 28+ on 64-bit RHEL/CentOS 6 or 7 The problem Google developers s ...