题目链接:

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. CentOS 7.2 搭建 Openvpn 服务器

    本文将以目前最新的openvpn-2.3.13.tar.gz为例来介绍如何在Linux系统中安装.配置及使用OpenVPN. 在这里,我们选用了一台预装CentOS 7.2 64位系统的计算机作为Op ...

  2. svn 大全

    环境:Win7 32 bit SVN简介:程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是一 ...

  3. [C#]多线程下载

    发现电脑里以前编写的下载程序... 做个记录,那时做的挺匆忙的,没用委托,通过公开出窗体来修改下载进度,做的比较乱... ==!! 程序具体功能(流程): 1.检测系统托盘图标内的进程名是否符合要求 ...

  4. android 不一样的学习记录

    http://blog.csdn.net/innost/article/details/48228651 ( 深入理解Android 之 Gradle) 介绍:这篇文章篇幅较长,需要有时间并足够有耐心 ...

  5. 原生js实现tab选项卡

    1.html部分 <body>        <div id="tab">            <div class="tab_menu& ...

  6. rails provide与content_for的区别

    页面渲染时:provide先执行,但找到一个provide之后就不再查找 content_for 顺序执行,在哪个位置,就等之前的渲染完后才执行.但是要等到所有的content被查找完后一块返回,也就 ...

  7. 滴滴DSRC抢楼大赛,十一快车券飞起来

    十一马上就要到了 不管你想要去哪里 总少不了滴滴打车 DSRC为你准备了30份快车券 现在开始参与抢楼,就有机会得到 活动时间: 本帖发布之时起-9月30日12:00 参与方式: 关注微信公众号“滴滴 ...

  8. Force.com微信开发系列(五)自定义菜单进阶及语音识别

    在上文里我们介绍了如何通过Force.com平台里为微信账号添加自定义菜单,本文里我们将进一步介绍如何查询菜单以及删除菜单的相关知识,最后会介绍微信平台如何进行语音识别的相关技术. 查询菜单 与创建菜 ...

  9. 迭代器iterator(三):Listlterator遍历arraylist,并用逆序输出结果

    迭代器(iterator) 是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址.迭代器修改了常规指针的接口,所谓迭代器是一种概 念上的抽象:那些行为上像迭 ...

  10. MX4连接后adb无法识别解决方法

    1. 使用android SDK目录中的 android SDK目录\tools 下的android脚本,命令行中执行 android update adb,成功运行后,会在用户名录下产生,C:\Us ...