题目来源(洛谷P1908 逆序对)
题目链接:
求逆序对的数目
https://www.luogu.com.cn/problem/P1908
逆序对数目的求解方法,一种是基于冒泡排序,另一种是基于归并排序。
基于冒泡排序的算法:(O(n^2))
#include<cstdio>
int num[500001];
int tot;
unsigned long long sum=0;
inline int read(){
int res=0;
char c=getchar();
while(c>='0' && c<='9'){
res=(res<<1)+(res<<3)+(c^48);
c=getchar();
}
return res;
}
inline void write(unsigned long long num){
if(num!=0){
write(num/10);
putchar((num%10)^48);
}
}
int main(){
bool check;
tot=read();
for(int i=1;i<=tot;i++){
num[i]=read();
}
for(int i=1;i<=tot;i++){
check

本文探讨了如何求解C++中的逆序对问题,分别介绍了基于冒泡排序和归并排序的两种算法。冒泡排序的复杂度为O(n^2),而归并排序的复杂度优化到了O(nlogn)。详细讲述了每种方法的实现思路和步骤。
最低0.47元/天 解锁文章
1799

被折叠的 条评论
为什么被折叠?



