简体   繁体   English

如何解决“超出时间限制”错误?

[英]How to solve "Time Limit Exceeded" error?

I am recently practicing on codeforces.com, I solved a problem there:我最近在 codeforces.com 上练习,我在那里解决了一个问题:

Kristina has two arrays a and b, each containing n non-negative integers. Kristina 有两个数组 a 和 b,每个数组包含 n 个非负整数。 She can perform the following operation on array a any number of times:她可以对数组执行任意次数的以下操作:

apply a decrement to each non-zero element of the array, that is, replace the value of each element ai such that ai>0 with the value ai−1 (1≤i≤n).对数组的每个非零元素应用减量,即用值 ai-1 (1≤i≤n) 替换每个元素 ai 的值,使得 ai>0。 If ai was 0, its value does not change.如果 ai 为 0,则其值不变。 Determine whether Kristina can get an array b from an array a in some number of operations (probably zero).确定 Kristina 是否可以通过一些操作(可能为零)从数组 a 中获取数组 b。 In other words, can she make ai=bi after some number of operations for each 1≤i≤n?换句话说,对于每个 1≤i≤n,她可以在一些操作之后使 ai=bi 吗?

For example, let n=4, a=[3,5,4,1] and b=[1,3,2,0].例如,设 n=4,a=[3,5,4,1] 和 b=[1,3,2,0]。 In this case, she can apply the operation twice:在这种情况下,她可以应用两次操作:

after the first application of the operation she gets a=[2,4,3,0];在第一次应用操作后,她得到 a=[2,4,3,0]; after the second use of the operation she gets a=[1,3,2,0].在第二次使用该操作后,她得到 a=[1,3,2,0]。 Thus, in two operations, she can get an array b from an array a.因此,在两次操作中,她可以从数组 a 中得到数组 b。

Input The first line of the input contains an integer t (1≤t≤104) —the number of test cases in the test.输入 输入的第一行包含一个整数 t (1≤t≤104) — 测试中的测试用例数。

The descriptions of the test cases follow.测试用例的描述如下。

The first line of each test case contains a single integer n (1≤n≤5⋅104).每个测试用例的第一行包含一个整数 n (1≤n≤5⋅104)。

The second line of each test case contains exactly n non-negative integers a1,a2,…,an (0≤ai≤109).每个测试用例的第二行正好包含 n 个非负整数 a1,a2,…,an (0≤ai≤109)。

The third line of each test case contains exactly n non-negative integers b1,b2,…,bn (0≤bi≤109).每个测试用例的第三行正好包含 n 个非负整数 b1,b2,…,bn (0≤bi≤109)。

It is guaranteed that the sum of n values over all test cases in the test does not exceed 2⋅105.保证测试中所有测试用例的n个值之和不超过2⋅105。

Output For each test case, output on a separate line:输出对于每个测试用例,在单独的行上输出:

YES, if by doing some number of operations it is possible to get an array b from an array a;是的,如果通过执行一些操作可以从数组 a 中获取数组 b; NO otherwise.否,否则。 You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).您可以在任何情况下输出 YES 和 NO(例如,字符串 yEs、yes、Yes 和 YES 将被识别为肯定响应)。

here is my solution这是我的解决方案

#include<stdio.h>
#include<stdlib.h>

void array_decrement(int arr[],int length)   
{
    for(int i=0;i<length;i++)
    {       
        if(arr[i]>0)
        {
            arr[i]--;       //decrements all element by one, if element is 0 then it won't be changed
        }
    }
}

int array_cmp(int arr1[],int arr2[],int length)
{
    for(int i=0;i<length;i++)
    {
        if(arr1[i]<arr2[i])     //if any element will less than element of 2nd array, then it will return -1 
        {
            return -1;
        }
        if(arr1[i]>arr2[i])     //if any element will more than element of 2nd array, then it will return 0
        {
            return 0;
        }
    }
    return 1;                   // if all elements are same then it will return 1
}

int main()
{
    int t,n,cmp;
    scanf("%d",&t);
    for(int i=0;i<t;i++)  // t = number of testcases
    {
        scanf("%d",&n);
        int arr1[n]; 
        int arr2[n];

        for(int p=0;p<n;p++)
        {
            scanf("%d",&arr1[p]);
        }
        for(int p=0;p<n;p++)
        {
            scanf("%d",&arr2[p]);
        }


        // logical part
        while (i>=0)     //loop runs for infinite times, but it has a set of conditions which will break loop in finite time
        {
            if(array_cmp(arr1,arr2,n)==1)          // if all elements are same print "YES"
            {
                printf("YES\n");
                break;

            }else if(array_cmp(arr1,arr2,n)==0)    //if any element can be decremented then it will decrement
            {
                array_decrement(arr1,n);

            }else if(array_cmp(arr1,arr2,n)==-1)    // if any element gets to less than 2nd elemnet during process of decrement then print "NO"
            {
                printf("NO\n");
                break;
            }
        }     
    }
    return 0;
}

My Problem : It shows " Time limit Exceeded " when I went to submit this code, can anyone help me to solve this error?, actually I've faced this error First time.我的问题:当我提交此代码时,它显示“超出时间限制”,有人可以帮我解决这个错误吗?实际上我第一次遇到这个错误。

I am recently practicing on codeforces.com... It shows "Time limit Exceeded" when I went to submit this code.我最近在 codeforces.com 上练习...当我提交此代码时,它显示“超出时间限制”。

As paddy says :正如稻谷所说

These types of coding challenges are designed to test time complexity of the algorithms.这些类型的编码挑战旨在测试算法的时间复杂度。 When you get a runtime limit exceeded, it's usually because your algorithm is not the best way to solve the problem .当您超出运行时限制时,通常是因为您的算法不是解决问题的最佳方法

Your algorithm compares the arrays two times in a loop which actually performs all the possible decrements.您的算法在一个循环中两次比较数组,该循环实际上执行所有可能的递减。

Given N , the number of elements in each array (up to 5*10 4 , apparently), and K , the number of possible steps necessary to transform an array into the other (note that 0 <= a[i], b[i] <= 10 9 ), the time complexity of the posted algorithm is O( N * K ).给定N ,每个数组中的元素数(显然最多 5*10 4 )和K ,将一个数组转换为另一个数组所需的可能步骤数(注意 0 <= a[i], b[ i] <= 10 9 ),posted算法的时间复杂度为O( N * K )。


Consider the following points:考虑以下几点:

  • The array a can be transformed into b (applying the rules of the problem statement) if there exits a non-negative value k (the number of decrements) such that, for every i :如果存在非负值k (递减的数量),则数组a可以转换为b (应用问题陈述的规则),对于每个i

    • If b[i] == 0 , then k >= a[i] .如果b[i] == 0 ,则k >= a[i]
    • Otherwise:否则:
      • If b[i] <= a[i] , then k == a[i] - b[i] .如果b[i] <= a[i] ,则k == a[i] - b[i]
      • Othrwise, k doesn't exist and the transformation cannot be done.否则, k不存在,无法进行转换。
  • We don't need to actually perform the transformation, we only have to compare the elements of the two arrays (not even all them, in some cases) to find out if such a k value exists.我们不需要实际执行转换,我们只需要比较两个数组的元素(在某些情况下甚至不是全部)来找出是否存在这样的k值。 In other words, this algorithm has an O( N ) time complexity.换句话说,该算法的时间复杂度为 O( N )。

When you are submitting your solution codeforces perform a lot of test cases on it.当您提交解决方案时,codeforces 会在其上执行大量测试用例。 The error says that it takes too much time so your solution is not optimal for runtime.该错误表明它需要太多时间,因此您的解决方案对于运行时不是最佳的。

After the first look at the code I can give an advise that dynamic allocation is too expensive, you must try preallocation.先看代码后我可以给个建议,动态分配太贵了,你必须尝试预分配。

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

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