简体   繁体   English

spoj的时间限制超出错误。我如何改善解决方案?

[英]Time limit exceeded error on spoj.. How can i improve my solution?

We are given an array a[1..N] . 给我们一个数组a[1..N] For each element a[i] in the array, we note down the sum of all the elements which are smaller and occur before the present element. 对于数组中的每个元素a[i] ,我们记下所有较小的元素的总和,这些元素出现在当前元素之前。 I need to calculate the total sum for every element in the array. 我需要计算数组中每个元素的总和。

Constraints: 限制条件:

1<=N<=10^5 1 <= N <= 10 ^ 5

All elements will be between 0 and 10^6. 所有元素将在0到10 ^ 6之间。

Here is the link to my question: http://www.spoj.com/problems/DCEPC206/ . 这是我的问题的链接: http : //www.spoj.com/problems/DCEPC206/ I'm using the approach shown below, but I'm getting TIME LIMIT EXCEEDED error on SPOJ. 我正在使用下面显示的方法,但是在SPOJ上收到了TIME LIMIT EXCEEDED错误。 How can I improve my solution? 如何改善解决方案?

include 

int main()

{

long n,a[100000],i,j,sum;

printf("enter the number of elements");

  scanf("%ld",&n);

printf("enter the elements of the array");

for(i=0;i<n;i++)

      scanf("%ld",&a[i]);

sum=0;


for(i=1;i<n;i++)

     for(j=i-1;j>=0;j--)

         if(a[i]>a[j])

              sum+=a[j];

printf("\n%ld",sum);

return 0;

}

Yours is a trivial implementation which takes time of the order of O(n^2) but for the solution to get accepted you have to use Divide and Conquer as you do in Merge Sort which takes only O(NLogN) time compared to simpler sorting algorithms like Bubble Sort,etc. 您的实现很简单,需要花费O(n ^ 2)的时间,但是要获得解决方案的接受,您必须像在合并排序中那样使用分而治之,与简单排序相比,它只需要O(NLogN)时间冒泡排序等算法。

For this you simply can change the Mergesort implementation a little bit simply by adding 2-3 lines of code in it. 为此,您只需在其中添加2-3行代码即可稍微更改Mergesort实现。 For understanding this better go through question of counting inversions in an array.( http://www.geeksforgeeks.org/counting-inversions/ ) Then you will realize you simply have to consider pairs opposite in nature to inversion and add all the smaller elements of all such pairs . 为了更好地理解这一点,我们将遍历对数组中的反转进行计数的问题。( http://www.geeksforgeeks.org/counting-inversions/ )然后,您将认识到,您只需要考虑与反转相反的对,然后将所有较小的对相加即可。所有这些对的元素。 For example - in an array 1,4,2,5 consider 4,2 is inversion but we have to consider pairs like 2,5 and 1,2 to get the solution. 例如,在数组1,4,2,5中,认为4,2是反演,但我们必须考虑像2,5和1,2这样的对才能获得解。 In every such pair keep adding the left no. 在每一个这样的对中,继续添加左编号。 ( Think hard about how it is doing our job !! ) (认真思考它是如何完成我们的工作的!!)

For your reference go thoroughly through this merge sort code in which i have small changes to get a correct accepted solution.(sum variable stores the resultant value) 供您参考,请仔细阅读此合并排序代码,在其中我进行了一些小的更改以获得正确的可接受解决方案。(sum变量存储了结果值)

#include <stdio.h>
#include <stdlib.h>
long long int sum;
void merge(long long int c[],long long int arr[],long long int start,long long int middle,long long int end)
{
    long long int i=0,j=start,k=middle;
    while((j<middle)&&(k<end))
    {
        if(arr[j]<arr[k])
        {
            sum=sum+((end-k)*arr[j]);
            c[i]=arr[j];
            i++;j++;
        }
        else
        {
            c[i]=arr[k];
            i++;k++;
        }
    }
    while(j<middle)
    {
        c[i]=arr[j];
        i++;
        j++;
    }
    while(k<end)
    {
        c[i]=arr[k];
        i++;
        k++;
    }

}


void msort(long long int arr[],long long int start,long long int end)
{
    long long int middle=(start+end)/2;
    if((end-start)==1)
    {   return ;
    }
    msort(arr,start,middle);
    msort(arr,middle,end);
    long long int *c;
    c=(long long int*)malloc(sizeof(long long int)*(end-start));
    merge(c,arr,start,middle,end);
    long long int i,j=0;
    for(i=start;i<end;i++)
    {
        arr[i]=c[j];
        j++;
    }
}

void swap (long long int x[],long long int m,long long int n)
{
  long long int t= x[m];
  x[m]=x[n];
  x[n]=t;
}

int main()
{
    int t,i;
    long long int n,*arr,j;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        scanf("%lld",&n);
        arr = ( long long int * ) malloc ( sizeof(long long int) * n + 10);
        for(j=0;j<n;j++)
        {
            scanf("%lld",&arr[j]);

        }

        sum=0;  
        msort(arr,0,n);
//      for(j=0;j<n;j++)
//          printf("%lld ",arr[j]);
        printf("%lld\n",sum);
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM