import java.util.Scanner;
public class Main{
public static int n;
public static person[] Child;
public static long result=0;
static class person{
public int hight;
public long count;
public person(int hight){
this.hight=hight;
this.count=0;
}
}
public void mergeSort(person[] A){
if(A.length>1){
person[] leftA=getHalf(A,0);
person[] rightA=getHalf(A,1);
mergeSort(leftA);
mergeSort(rightB);
Merge(A,leftA,rightA);
}
}
public person[] getHalf(person[] A,int judge){
int len=A.length;
person[] half;
if(judge==0){
half=new person[len/2];
for(int i=0;i<half.length;i++)
half[i]=A[i];
}else{
half=new person[len-len/2];
for(int i=0;i<half.length;i++)
half[i]=A[len/2+i];
}
return half;
}
public void Merge(person[] A,person[] leftA,person[] rightA){
int i=0,j=0;
int lenL=leftA.length,lenR=rightA.length;
while(i<lenL&&j<lenR){
if(leftA[i].hight>right[j].length){
rightA[j].count+=(lenL-i);
j++;
}else{
i++;
}
}
i=lenL-1;
j=lenR-1;
while(i>=0&&j>=0){
if(leftA[i].high>right[j].high){
leftA[i].count+=(j+1);
i--;
}else{
j--;
}
}
i=0;
j=0;
int t=0;
while(i<lenL&&j<lenR){
if(leftA[i].hight<rightA[j].hight)
A[t++]=leftA[i++];
else
A[t++]=rightA[j++];
}
while(i<lenL)
A[t++]=leftA[i++];
while(j<lenR)
A[t++]=rightA[j++];
}
public static void main(String[] args){
Main test=new Main();
Scanner in=new Scanner(System.in);
n=in.nextInt();
Child=new person[n];
for(int i=0;i<n;i++){
int hight=in.nextInt();
Child[i]=new person(hight);
}
test.mergeSort(Child);
for(int i=0;i<n;i++){
long count=Child[i].count;
result+=count*(count+1)/2;
}
System.out.println(result);
}
}
试题 历届试题 小朋友排队
最新推荐文章于 2022-03-06 21:32:34 发布