Inversion
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1174 Accepted Submission(s): 351
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 .
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
Source
半年前的问题一直困然至今。。
题意:一个数组,你可以从中删除连续的k个数,求一个最优的方案使得操作后的数组内逆序数对最少。
学长讲过这个,但是暑假的我还是在无限wa。。。枚举删除连续k个数的起点,每次
啊啊啊回头在写。。。。
待补完
/*
━━━━━┒
┓┏┓┏┓┃μ'sic foever!!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <stack>
#include <vector>
using namespace std;
const int maxn=1e5;
int save[maxn+5];
int before[maxn+5];
int after[maxn+5];
int n;
int lowbit(int x){
return x&(-x);
}
void update(int x,int val,bool type){
if(type){
while(x<=maxn){
before[x] += val;
x += lowbit(x);
}
}else{
while(x<=maxn){
after[x] += val;
x += lowbit(x);
}
}
}
int getsum(int x,bool type){
int sum=0;
if(type){
while(x){
sum += before[x];
x -=lowbit(x);
}
}else{
while(x){
sum += after[x];
x -=lowbit(x);
}
}
return sum;
}
int main(){
int t;
scanf("%d",&t);
int m,i;
while(t--){
memset(before, 0, sizeof(before));
memset(after, 0, sizeof(after));
scanf("%d%d",&n,&m);
long long ans=0;
for(i=1;i<=n;i++){
scanf("%d",&save[i]);
}
for(i=m+1;i<=n;i++){
ans += i-m-1-getsum(save[i],false);
update(save[i], 1, false);
}
long long now=ans;
for(i=1;i<=n-m;i++){
now -= getsum(save[i+m]-1,false);
now -= getsum(save[n], true) - getsum(save[i+m], true);
update(save[i+m], -1, false);
now += getsum(save[i]-1, false);
now += getsum(save[n], true) - getsum(save[i],true);
update(save[i], 1, true);
ans = min(ans,now);
}
printf("%lld\n",ans);
}
return 0;
}