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(1≤n≤105,1≤m<n) - the length of the seuqence. The second line contains n integers a1,a2,...,an(1≤ai≤n).

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

【题意】给出n个数,删除其中一个长度为m的一个连续序列,求最后最小逆序数

【思路】这题精华很多,有待吸收~~

每一次移动,显然会往这个序列中删除一个数,增加一个数

1.加入一个数:多了它后面所有比它小的数,多了它前面所有比它大的数

2.删除一个数:少了它后面所有比它小的数,少了它前面所有比它大的数

用两个树状数组动态维护删除的序列前面和后面部分。

直接memset会TLE,需要限制一下清空的范围。(大神让我学了一招!!!)

参考:http://blog.csdn.net/weizhuwyzc000/article/details/49745569

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=+;
int n,m,a[N];
long long b[N*],c[N*];
int lowbit(int x)
{
return x&(-x);
}
long long query(long long *d,int x)
{
int res=;
while(x)
{
res+=d[x];
x-=lowbit(x);
}
return res;
}
void update(long long *d,long long x,long long v )
{
while(x<=n)
{
d[x]+=v;
x+=lowbit(x);
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int cnt=,ans=inf;
scanf("%d%d",&n,&m);
memset(b,,(n+)*sizeof(long long));
memset(c,,(n+)*sizeof(long long));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=+m;i<=n;i++)
{
cnt+=i-m--query(b,a[i]);
update(b,a[i],);
}
ans=cnt;
for(int i=m+;i<=n;i++)
{
cnt+=query(b,a[i-m]-);
cnt+=query(c,n)-query(c,a[i-m]);
update(c,a[i-m],);
cnt-=query(b,a[i]-);
cnt-=query(c,n)-query(c,a[i]);
update(b,a[i],-);
ans=min(ans,cnt);
}
printf("%lld\n",ans);
}
return ;
}

Inversion_树状数组***的更多相关文章

  1. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  2. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  3. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  5. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

  6. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  7. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  8. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

  9. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

随机推荐

  1. 【转】【翻译】对响应式SVG的再思考

    来源: http://www.w3ctech.com/topic/1555 原文地址:http://www.smashingmagazine.com/2014/03/rethinking-respon ...

  2. 网页 css 样式 初始化

    body, div, ul, ol, dl, dt, dd, li, dl, h1, h2, h3, h4 {margin:0;padding:0;font-style:normal;font:12p ...

  3. WebService开发

    一.什么是WebService: 简单通俗来说,就是企业之间.网站之间通过Internet来访问并使用在线服务,一些数据,由于安全性问题,不能提供数据库给其他单位使用,这时候可以使   用WebSer ...

  4. blade and soul races guide

    Race Four races are available for those who wish to choose the path of martial arts: the careful Gon ...

  5. MergeRecord_C++中map的使用

    //============================================================================ // Name : TTTest.cpp ...

  6. php_html转译符号

    1.双引号 /" 或者 " 2.单引号 ' > 4. & &

  7. toolsqa-IMPORTANCE

    http://toolsqa.com/cucumber/cucumber-options/

  8. sfliter__except_handler4

    sfliter源码在vs08中编译 出现 错误error LNK2019: unresolved external symbol __except_handler4 referenced in fun ...

  9. python学习09——字典(3)

    今天写了一道python字典题目,用了上次字典(2)中的方法,代码如下: json = {', 'IP':'10.0.0.1'} def find_value(themap, word): if wo ...

  10. asp.net 5 中应用程序根目录及物理文件根目录的获取方式 此文已过期,不再适应rc1以后的版本

    之前看了asp.net5,小弟就试着用了用,做了个小网站练习一下,有一个小模块需要上传图片到wwwroot下的images文件夹,但是aspnet5 发生了翻天复地变化,之前获取网站根目录的的方法不再 ...