把下面C语言代码改成java代码
#include<iostream>#include<cstring>usingnamespacestd;constintmaxn=100000+5;longlonga[...
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=100000+5;
long long a[maxn],t[maxn];
long long ans=0;
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;
t[k++]=a[tm++];
}
}
while(tx<=m) t[k++]=a[tx++];
while(tm<=y) t[k++]=a[tm++];
for(int i=0;i<k;i++){
a[x++]=t[i];
}
}
void mergesort(int x,int y){
if(x==y) return ;
else {
int mid=(x+y)/2;
int xx=x,yy=y;
mergesort(xx,mid);
mergesort(mid+1,yy);
mymerge(xx,mid,yy);
//cout<<xx<<" "<<mid<<" "<<yy<<endl;
}
}
int main(){
int n,k;
while(cin>>n>>k){
memset(a,0,sizeof(a));
memset(t,0,sizeof(t));
for(int i=0;i<n;i++){
cin>>a[i];
}
ans=0;
mergesort(0,n-1);
if(k>ans) cout<<"0"<<endl;
else
cout<<ans-k<<endl;
}
return 0;
} 展开
#include <cstring>
using namespace std;
const int maxn=100000+5;
long long a[maxn],t[maxn];
long long ans=0;
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;
t[k++]=a[tm++];
}
}
while(tx<=m) t[k++]=a[tx++];
while(tm<=y) t[k++]=a[tm++];
for(int i=0;i<k;i++){
a[x++]=t[i];
}
}
void mergesort(int x,int y){
if(x==y) return ;
else {
int mid=(x+y)/2;
int xx=x,yy=y;
mergesort(xx,mid);
mergesort(mid+1,yy);
mymerge(xx,mid,yy);
//cout<<xx<<" "<<mid<<" "<<yy<<endl;
}
}
int main(){
int n,k;
while(cin>>n>>k){
memset(a,0,sizeof(a));
memset(t,0,sizeof(t));
for(int i=0;i<n;i++){
cin>>a[i];
}
ans=0;
mergesort(0,n-1);
if(k>ans) cout<<"0"<<endl;
else
cout<<ans-k<<endl;
}
return 0;
} 展开
展开全部
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;i<k;i++)
a[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;i<n;i++) {
a[i]=input.nextLong();
}
ans=0;
mergesort(0, n-1);
if(k>ans)
System.out.println("0");
else
System.out.println((ans-k)+"");
}
}
}
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;i<k;i++)
a[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;i<n;i++) {
a[i]=input.nextLong();
}
ans=0;
mergesort(0, n-1);
if(k>ans)
System.out.println("0");
else
System.out.println((ans-k)+"");
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询