把下面C语言代码改成java代码

2024-11-23 05:54:05
推荐回答(1个)
回答(1):

import java.util.Arrays;
import java.util.Scanner;
public class A {
static final int maxn=100000+5;
static long[] a=new long[maxn],t=new long[maxn];
static long ans=0;

static void mymerge(int x,int m,int y) {
int tx=x,tm=m+1;
int k=0;
while(tx<=m&& tm<=y) {
if(a[tx]<=a[tm])
t[k++]=a[tx++];
else {
ans+=m-tx+1;
a[k++]=a[tm++];
}
}

while(tx<=m) t[k++]=a[tx++];
while(tm<=y) t[k++]=a[tm++];
for(int i=0;ia[x++]=t[i];
}

static void mergesort(int x,int y) {
if(x==y)return ;

int mid=(x+y)/2;
int xx=x,yy=y;
mergesort(xx,mid);
mergesort(mid+1,yy);
mymerge(xx,mid,yy);
//System.out.println(xx+" "+mid+" "+yy);
}

public static void main(String[] arg) {
int n,k;

Scanner input=new Scanner(System.in);

while(input.hasNext()) {
n=input.nextInt();
k=input.nextInt();

Arrays.fill(a, 0);
Arrays.fill(t, 0);
for(int i=0;ia[i]=input.nextLong();
}
ans=0;
mergesort(0, n-1);
if(k>ans)
System.out.println("0");
else
System.out.println((ans-k)+"");
}
}

}