samedi 5 octobre 2019

how to remove TLE error from this code . what is causing TLE error in the program

my programing is giving desired output but i am facing TLE error . i don't know which part is causing TLE error and i want to know how i can i make is free from TLE error.

i have used merge sort in place of insertion which i was using earlier but still no effect on TLE error. the given contarint limits of program are 1≤t≤10 1≤n≤10^5 1≤A[i]≤10^6 for each valid i

#include<algorithm>
#include<vector>

using namespace std;
#include<stdio.h>         
void Merge(int *a, int low, int high, int mid)
{
    // We have low to mid and mid+1 to high already sorted.
    int i, j, k, temp[high-low+1];
    i = low;
    k = 0;
    j = mid + 1;

    // Merge the two parts into temp[].
    while (i <= mid && j <= high)
    {
        if (a[i] < a[j])
        {
            temp[k] = a[i];
            k++;
            i++;
        }
        else
        {
            temp[k] = a[j];
            k++;
            j++;
        }
    }

    // Insert all the remaining values from i to mid into temp[].
    while (i <= mid)
    {
        temp[k] = a[i];
        k++;
        i++;
    }

    // Insert all the remaining values from j to high into temp[].
    while (j <= high)
    {
        temp[k] = a[j];
        k++;
        j++;
    }


    // Assign sorted data stored in temp[] to a[].
    for (i = low; i <= high; i++)
    {
        a[i] = temp[i-low];
    }
}

// A function to split array into two parts.
void MergeSort(int *a, int low, int high)
{
    int mid;
    if (low < high)
    {
        mid=(low+high)/2;
        // Split the data into two half.
        MergeSort(a, low, mid);
        MergeSort(a, mid+1, high);

        // Merge them to get sorted output.
        Merge(a, low, high, mid);
    }
}


int main()
{
    int t,i,j,k,max[100011],T,n[10],A[100011],c[100011];
    cin>>t;

    for(i=0;i<t;++i)
    { 
        cin>>n[i];
        T=n[i];



        for(j=0;j<n[i];++j)
        {
            cin>>A[j];
        }



        for(j=0;j<n[i];++j)
        { 
          c[j]=0;

            for(k=0;k<j;++k)
            { 
              if(A[j]!=0)
              {
                if(A[k]%A[j]==0)
                {c[j]++;

                }
              }

            }
        }

         MergeSort(c,0,T-1);
         max[i]=c[T-1];





    }


    for(i=0;i<t;++i)
    {
        cout<<max[i]<<"\n";
    }
    return 0;
}```

Aucun commentaire:

Enregistrer un commentaire