题目链接:

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. java中的代码块执行顺序

    /* 代码块:在Java中,使用{}括起来的代码被称为代码块. 根据其位置和声明的不同,可以分为 局部代码块:局部位置,用于限定变量的生命周期. 构造代码块:在类中的成员位置,用{}括起来的代码.每次 ...

  2. (旧)子数涵数·UI设计——扁平化设计

    一.基本资料 1.由来 扁平化设计这个概念,是由Google(谷歌)在2008年提出的:它的首个实践者是microsoft(微软),microsoft在2012年发行了win8系统,这个系统的外观主题 ...

  3. Crash日志符号化

    1.符号化crash日志需要3样东西: 1).crash日志本身(如:example.crash),从Xcode的organizer导出或者来自Itunes Connect. 2).crash日志所对 ...

  4. jekyll

    bundle show minima查看安装路径 bundle exec github-pages versions 建立一个类似于master的分支,与master是完全独立 git checkou ...

  5. android 事件

    package com.example.yanlei.my2; import android.app.Activity; import android.content.Context; import ...

  6. android 回调函数一:基本概念

    1.概念 客户程序C调用服务程序S中的某个函数A,然后S又在某个时候反过来调用C中的某个函数B,对于C来说,这个B便叫做回调函数. 一般说来,C不会自己调用B,C提供B的目的就是让S来调用它,而且是C ...

  7. JSP内置对象--request对象

    本文主要介绍JSP中的request对象 request对象的主要方法: setAttribute(String name,Object):设置名字为name的request的参数值 getAttri ...

  8. NSInvocation

    NSInvocation 基本简介 NSInvocation是一个静态描绘的OC消息,也就是说,它是一个动作,这个动作可以变成一个对象.NSInvocation对象在对象和对象之间和应用程序和应用程序 ...

  9. ReactiveCocoa之UI篇

    前言: 上一篇讲ReactiveCocoa是函数响应式编程,并将多种事件响应的方式统一起来,使得不同的事件响应方式高度统一.同时也讲了ReactiveCocoa框架里面常见的几个概念.接下来基于那几个 ...

  10. nodejs socket

    server.js var net = require('net'); var clientList = []; var HOST = '127.0.0.1'; var PORT = 6969; va ...