题目链接:

Inversion

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1087    Accepted Submission(s): 323

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
 
题意:
有一个序列,然后你可以删除一个长度为mm的连续子序列. 问如何删除才能使逆序对最少.

思路:

也是套路,逆序对可以用树状数组求得,连续的可以使用滑动窗口,跟尺取法差不多啦;开了两个树状数组一个记录左边界之前的数,一个记录右边界后面的数,然后更新逆序对的数目,取最小就好了;

AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
int sum1[maxn],sum2[maxn],n,m,a[maxn];
int lowbit(int x){return x&(-x);}
inline void update1(int x,int num)
{
while(x<=n)
{
sum1[x]+=num;
x+=lowbit(x);
}
}
inline int query1(int x)
{
int s=0;
while(x)
{
s+=sum1[x];
x-=lowbit(x);
}
return s;
}
inline void update2(int x,int num)
{
while(x<=n)
{
sum2[x]+=num;
x+=lowbit(x);
}
}
inline int query2(int x)
{
int s=0;
while(x)
{
s+=sum2[x];
x-=lowbit(x);
}
return s;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)sum1[i]=sum2[i]=0;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
LL su=0;
for(int i=n;i>0;i--)
{
su=su+query2(a[i]-1);
update2(a[i],1);
}
LL ans=su,temp=su;int l,r=1;
for(l=1;l<=n-m+1;l++)
{
while(r-l<m&&r<=n)
{
update2(a[r],-1);
temp=temp-query2(a[r]-1);
temp=temp-(l-1-query1(a[r]));
r++;
}
ans=min(ans,temp);
temp=temp+(l-1-query1(a[l]));
temp=temp+query2(a[l]-1);
update1(a[l],1);
}
printf("%lld\n",ans);
}
return 0;
}

  

 

hdu-5497 Inversion(滑动窗口+树状数组)的更多相关文章

  1. hdu 4911 Inversion and poj2299 [树状数组+离散化]

    题目 题意:  给你一串数字,然后给你最多进行k次交换(只能交换相邻的)问交换后的最小逆序对个数是多少. 给你一个序列,每次只能交换相邻的位置,把他交换成一个递增序列所需要的最少步数 等于 整个序列的 ...

  2. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  3. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  4. hdu 1394 Minimum Inversion Number(树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个0 — n-1的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...

  5. HDU 1394 Minimum Inversion Number (树状数组)

    题目链接 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the numb ...

  6. hdu 1394 Minimum Inversion Number (树状数组求逆序对)

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

  7. hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)

    题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...

  8. HDU 1394:Minimum Inversion Number(树状数组,线段树)[水]

    题意:有0~n-1这n个数,以一定的排列.这个排列可以循环,就是可以把第一个拿到最后,然后形成新的排列.问这些排列中的逆序对最小值. 思路: 最后的循环,拿走一个之后,新的逆序对数 newsum = ...

  9. HDU 5862 Counting Intersections (树状数组)

    Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...

随机推荐

  1. c语言笔试题(带答案)

    填空: 1,short int a[10]={123, 456, 789}; sizeof(a)= 对于64位机来说,指针为8字节表示.其中 sizeof是一运算符,返回编译器为其分配的数组空间大小, ...

  2. [moka同学笔记]yii2.0的下拉菜单与bootstrap下拉菜单

    1.yii2下拉菜单 <li class="dropdown"><a href="#" class="dropdown-toggle ...

  3. css导航栏

    几个导航栏也算对学过知识的回顾,总有新的收获,下面是学习过程中敲的代码: <!DOCTYPE HTML> <html> <head> <title>&l ...

  4. Underscore学习笔记1

    项目用了很久underscore.每次都是临时查手册,没有系统的研究过,最近有空正好看看 github地址:https://github.com/lily1010/underscore_learn 一 ...

  5. 2015年第12本(英文第8本):the Old Man and the Sea 老人与海

    书名:The Old Man and The Sea(老人与海) 作者:Ernest Hemingway 单词数:2.7万 不重复单词数:2600 首万词不重复单词数:1500 蓝思值:940 阅读时 ...

  6. iOS多线程邂逅

    .线程之间的通信 //有一个特别耗时的操作,比如说网络请求,开启子线程去请求网络,我们一般是要在主线程更新UI,如何从子线程跳转到主线程? #import "ViewController.h ...

  7. 推些C语言与算法书籍

    c语言系统学习与进阶: 1. C primer plus C primer plus 作为一本被人推崇备至的 c 入门经典,C primer plus 绝非浪得虚名.应该 算得上 C 教材里最好的入门 ...

  8. tomcat下部署润乾报表

    因为项目需要,需要在项目中配置润乾报表,之前一直是用的jboss服务器,此处调整为tomcat时出错,然后各种找错,找答案,最后终于好了,然后总结一下. 首先在apache-tomcat-6.0.43 ...

  9. TreeSize工具介绍

    TreeSize Professional 工具是一个功能强大且灵活方便的硬盘空间管理工具,能在 Windows 8/7/Vista/XP 或 Windows Server 2012年/2008年/2 ...

  10. Asp.Net远程调试

    1.在本地找到VS安装目录下的 Visual Studio Tools 文件夹  并进入Remote Debugger Folder文件夹 2.根据服务器的操作系统是32位还是64位,选择下面的文件夹 ...