求大佬帮忙看看,全是tle

P1908 逆序对

GREATchen @ 2023-11-03 17:05:07

#include<iostream>

using namespace std;

void inverse_order(int a[],int l,int r)
{   int sum=0;
    if(l>=r)return ;

 int mid=(l+r)/2;
 inverse_order(a,l,mid),inverse_order(a,mid+1,r);
 int i=l, j=mid+1, temp[100000],k=0;

 while(i<=mid&&j<=r)
 {
     while(a[i]<a[j])temp[k++]=a[i++];
     while(a[i]>a[j])
     {   sum+=mid+1-i;
         temp[k++]=a[j++];
     };
 }
    while(i<mid)temp[k++]=a[i++];
    while(j<r)temp[k++]=a[j++];

    for(int i=l;i<r;i++)
    a[i]=temp[i];

 }

by 7The_shy @ 2023-11-03 19:09:38

数组开小了,开500010


by Second_Gardenia @ 2023-11-03 19:47:02

@hxj666666 哇哇哇,hxj大佬耶


by 7The_shy @ 2023-11-03 20:18:36

@caizilang 哇哇哇,省一大佬耶


by GREATchen @ 2023-11-03 20:28:00

#include<iostream>

using namespace std;
 long long sum ;

void inverse_order(int a[],int l,int r)
{   
    if(l>=r)return ;

 int mid=(l+r)/2;
 inverse_order(a,l,mid),inverse_order(a,mid+1,r);
 int i=l, j=mid+1, temp[500010],k=0;

 while(i<=mid&&j<=r)
 {
     if(a[i]<=a[j])temp[k++]=a[i++];
     else
     {   
         temp[k++]=a[j++];
         sum+=mid+1-i;
     };
 }
    while(i<mid)temp[k++]=a[i++];
    while(j<r)temp[k++]=a[j++];

    for(int i=l;i<r;i++)
    a[i]=temp[i];

 }

int main()
{
    int n,a[500010];
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>a[i];

    inverse_order(a,0,n-1);

    cout<<sum;

    return 0;
}

大佬求带,有结果但是报错是warning@caizilang@hxj666666


by xiaoshumiao @ 2023-11-08 22:04:29

@GREATchen 建议把temp定义成全局数组。


|