3_14 @ 2024-07-17 10:14:11
Code:
#include<bits/stdc++.h>
#define str to_string
using namespace std;
using ll=long long;
const int MAX=5e5+1;
int a[MAX],b[MAX],cnt=0;
void merge_sort(int l,int r){
cnt++;
if(l==r)return;
int mid=l+r>>1;
merge_sort(l,mid);
merge_sort(mid+1,r);
int i=l,j=mid+1,k=l;
while(i<=mid&&j<=r)
if(a[i]<a[j])b[k++]=a[i++];
else b[k++]=a[j++];
while(i<=mid)b[k++]=a[i++];
while(j<=r)b[k++]=a[j++];
for(int i=l;i<=r;i++)a[i]=b[i];
}
int n;
int main(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
merge_sort(1,n);
cout<<cnt<<'\n';
return 0;
}
by Melo_DDD @ 2024-07-17 10:18:21
@3_14 你的 cnt 为什么要放在那里啊,建议思考一下逆序对的原理
by 3_14 @ 2024-07-17 10:19:15
@shin_chan_jiang
by Melo_DDD @ 2024-07-17 10:22:16
@3_14
else b[k++]=a[j++];
by caoshuchen @ 2024-07-17 10:31:45
by caoshuchen @ 2024-07-17 10:32:49
@3_14 我赌你的
by 3_14 @ 2024-07-17 10:37:53
@caoshuchen 主要是才学完归并
by caoshuchen @ 2024-07-17 10:40:23
@3_14 问题不大,当初我也差不多。我现在就正在学点分治。