一半TLE,膜拜各路大佬

P1908 逆序对

KBFqiufeng @ 2024-09-07 22:32:45


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,a[500010],num=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
            if(a[i]>a[j])num++;
        }
    }
    printf("%d",num);
    return 0;
}

by meifan666 @ 2024-09-07 22:35:45

@KBFqiufeng n*n的时间复杂度肯定超时,学学归并排序


by KBFqiufeng @ 2024-09-07 22:38:19

@meifan666 好的,谢谢大佬


|