代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define mod 3
#define len 100010
using namespace std;
struct p
{
int x;
int priority;
};
bool cmp1(p a,p b)
{
return a.x<b.x;
}
bool cmp2(p a,p b)
{
return a.priority<b.priority;
}
p a[len];
p b[len];
int n,k;
int c[len];
int lowbit (int x)
{
return x&(-x);
}
void add(int i,int x)
{
while(i<=n)
{
c[i]+=x;
i+=lowbit(i);
}
}
__int64 query(int i)
{
__int64 ans=0;
while(i>0)
{
ans+=c[i];
i-=lowbit(i);
}
return ans;
}
int main()
{
//int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(c,0,sizeof(c));
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i].x);
a[i].priority=i;
//a[i].x=
}
//add(1,2);
sort(a+1,a+1+n,cmp1);
//a[1].x=1;
for(int i=1;i<=n;++i)
{
b[i].x=a[i].x;
b[i].priority=a[i].priority;
}
b[1].x=1;
for(int i=2;i<=n;++i)
{
//b[i].x=a[i].x;
//b[i].priority=a[i].priority;
if(a[i].x==a[i-1].x)
b[i].x=b[i-1].x;
else
b[i].x=b[i-1].x+1;
}
sort(b+1,b+1+n,cmp2);
__int64 sum=0;
for(int i=1;i<=n;++i)
{
//printf("%d ",a[i].x);
//printf("%I64d ",query(b[i].x));
sum+=i-1-query(b[i].x);
add(b[i].x,1);
//printf("%d ",query(a[i].x));
//printf("%d ",query(n));
}
/*
for(int i=1;i<=n;++i)
{
printf("%d ",c[i]);
}
*/
printf("%I64d\n",max(sum-k,(__int64)0));
}
return 0;
}