新手!!50分求助!!

P1908 逆序对

朝雨辰河 @ 2019-07-31 20:30:30

include<bits/stdc++.h>

using namespace std; int a[1000002],b[1000002],ans;
void cmp(int x,int y) { int p,i,j,k; if(x>=y) return ; p=(x+y)/2;
cmp(x,p);
cmp(p+1,y);
i=x;
j=p+1;
k=x;
while(i<=p&&j<=y)
if(a[i]<=a[j])
b[k++]=a[i++]; else b[k++]=a[j++],ans+=p-i+1; while(i<=p)
b[k++]=a[i++]; while(j<=y) b[k++]=a[j++]; for(i=x;i<=y;i++)
a[i]=b[i];
} int main() { int n,i; cin>>n; for(i=1;i<=n;i++) cin>>a[i]; cmp(1,n); cout<<ans; }


by 樱花飞舞 @ 2019-07-31 20:34:19

你不会用Markdown吗?

这张图片我发了快十次了


by 朝雨辰河 @ 2019-07-31 20:36:00

#include<bits/stdc++.h>
using namespace std;
int a[1000002],b[1000002],ans;  
void cmp(int x,int y)
{
int p,i,j,k;
if(x>=y) return ;
p=(x+y)/2;          
cmp(x,p);   
cmp(p+1,y);     
i=x;                
j=p+1;              
k=x;                
while(i<=p&&j<=y)   
    if(a[i]<=a[j])  
        b[k++]=a[i++];
    else b[k++]=a[j++],ans+=p-i+1;
while(i<=p)         
    b[k++]=a[i++];
while(j<=y)
    b[k++]=a[j++];
for(i=x;i<=y;i++)   
    a[i]=b[i];  
}
int main()
{
int n,i;
cin>>n;
for(i=1;i<=n;i++)
    cin>>a[i];
cmp(1,n);
cout<<ans;
}

by Alioth_ @ 2019-07-31 20:43:43

你不会缩进吗?


by 朝雨辰河 @ 2019-07-31 20:50:12

OK啦


by gx123 @ 2019-08-14 17:59:45

改为long long吧


|