题目链接:

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. pbfunc外部函数扩展应用-在Powerbuilder中进行Http的GET、POST操作

    利用PBFunc扩展函数进行Http的操作时,需要对n_pbfunc_http的以下几个函数进行参数设置: of_set_URL(...)//要进行GET或POST的url,必须 of_set_Con ...

  2. angularjs post

    /** * POST 1 * $http.post('http://localhost:8001/quickstart/task/create', { newTask: newTask }) */ / ...

  3. Android 手机卫士11--窗体弹出PopupWindow

    protected void showPopupWindow(View view) { View popupView = View.inflate(this, R.layout.popupwindow ...

  4. SwipeRefreshLayout下拉刷新

    1.SwipeRefreshLayout是Google在support v4 19.1版本的library更新的一个下拉刷新组件,实现刷新效果更方便. 弊端:只有下拉 //设置刷新控件圈圈的颜色 sw ...

  5. 公司mysql数据库设计与优化培训ppt

    cnblogs无法上传附件. http://pan.baidu.com/s/1kVGqMn9

  6. Smtp邮件发送系统公用代码整理—总结

    1.前言 a.在软件开发中,我们经常能够遇到给用户或者客户推送邮件,推送邮件也分为很多方式,比如:推送一句话,推送一个网页等等.那么在系统开发中我们一般在什么情况下会使用邮件发送呢?下面我简单总结了一 ...

  7. CSS3选择器(二)--表单

    :enabled 选择可用状态的表单元素 :disabled 选择不可用状态的表单元素 :checked 复选框.单选框选中状态的选项 ::selection 用来匹配突出显示的文本(用鼠标选择文本时 ...

  8. 数据库表-DD01L DD02L DD03L-保存数据表和域的消息

    DD01L 定义域 DD02L SAP-表,记录每张数据库表和自定义表的表相关信息 DD03L 定义字段,记录数据表和自定义表中每个字段相关信息

  9. SharePoint2013 - 移动文档

    In SharePoint 2010, the easiest way to transfer documents from one library to another involved using ...

  10. 正确匹配URL的正则表达式

    网上流传着多种匹配URL的正则表达式版本,但我经过试验,最好用的还是从stackoverflow上查到的: (https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_| ...