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 ...
随机推荐
- 使用appscan实现多站扫描简单自动化
因为appscan在新建扫描任务的时候只能输入一个target,并且没有awvs/nessus那样提供web接口,导致我以前一直以为appscan不能像awvs那样批量建立任务自动扫描. 不过,今天要 ...
- 用mel编写自定义节点的属性编辑器界面
用mel编写自定义节点的属性编辑器界面比较麻烦,而且网上例子又少,下面给出一个范例,说明基本的格式 // 初始化节点时调用 global proc initControl(string $attrNa ...
- xcode的菜单栏功能解析
[Xcode 7.2]Xcode菜单栏之你不知道的那点事 File: New : 可以新建tap,窗口,新文件,playground,workspace,target等等. Add Files to ...
- 微软职位内部推荐-SW Engineer II for Skype
微软近期Open的职位: We are the Skype Beijing team. Skype division drives the communications strategy for Mi ...
- C# 单点登录 MVC
实现sso系统的主要难点: 1:不能直接访问数据库,有安全隐患,而且还容易乱套. 2:多个系统需要进行单点登录,逻辑需要严谨,能支持N多系统.而不只是少数几个系统. 3:代码不能过于复杂,需要简洁,灵 ...
- 深入理解abstract class和interface(转)
原文地址 深入理解abstract class和interface java提高篇(四)-----抽象类与接口
- 修改Matlab 2012b默认工作路径
MATLAB的路径有多种,这里只讲一下启动时设置成MATLAB的用户的默认工作路径. 本人不想去改MATLAB的原来系统文件,而是尽量利用startup.m.这个文件默认在'/home/r/文档/MA ...
- TCP建立连接、断开连接以及正常报文的报头和报位的大小
正常通信报文大小: 以太网的头尾:14字节 IP首部:20字节 TCP首部:20字节 尾部4字节校验 合计:58 三次握手的报文大小: SYN: 70 AYN,ACK:72 ACK: 64 合计:20 ...
- 疯狂位图之——位图实现12GB无重复大整数集排序
<Programming Pearls>(编程珠玑)第一章讲述了如何用位图排序无重复的数据集,整个思想很简洁,今天实践了下. 一.主要思想 位图排序的思想就是在内存中申请一块连续的空间作为 ...
- python实现简易数据库之一——存储和索引建立
最近没事做了一个数据库project,要求实现一个简单的数据库,能满足几个特定的查询,这里主要介绍一下我们的实现过程,代码放在过ithub,可参看这里.都说python的运行速度很慢,但因为时间比较急 ...