时间限制超过了hackerearth [英] Time limit exceeded hackerearth

查看:136
本文介绍了时间限制超过了hackerearth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在执行hackerearth编译器中的代码时遇到超出时间限制的错误,代码正在给出正确的输出但是在提交代码的时间限制超过时,请帮助这段代码,是不是优化了。 />


我的尝试:



 #include < stdio.h中> 

main()
{
long long int n,q,i,x,y,a [1000001],s;

scanf(%lld%lld,& n,& q); //没有数组元素和查询

for(i = 1; i< = n; i ++)
scanf(%lld,& a [i]);

而(q--)
{
x = 0,y = 0,s = 0;

scanf(%lld%lld,& x,& y); //数组的上限和下限

for(i = x; i< = y; i ++)
s = s + a [i];

s = s /(y-x + 1);
printf(%lld \ n,s);
}
}

解决方案

据我所知,您使用的是超时的在线编译器。



虽然我不确定在发生这种情况时可能是编译器正在使用默认值初始化堆栈变量(因为其他编译器至少也使用调试版本花了一些时间。



你在堆栈上有一个非常大的64位整数数组,占用800万字节,接近默认的堆栈大小8大多数编译器使用的MB。



您可以尝试使用 malloc()在堆上分配数组已输入尺寸。这是常用的方法。



如果你想展示你的技能,你也应该避免常见的初学者错误(数组开始索引不为零, main 不返回 int )并检查用户输入的有效性(大小大于零且小于最大值,数组大小和上限之间的界限大于低于)。


这些网站给你带来了挑战。

Quote:

是不是优化了。



优化正是挑战的对象。大多数海峡前沿解决方案永远不是正确的解决方案。

如果要求1笔金额,您的计划是最佳的,但如果要求您提供许多部分金额,则不是。

另有说法,解决方案是一种权衡,你花时间计算一些可以帮助你获得快速答案的东西。



你的挑战是找到怎么做,如果我们给你解决方案,然后你就没有遇到挑战。


我们无法帮助你解决这个问题:这是你的挑战,而不是我们的挑战。

但你需要的第一件事就是do是试图找出它需要多长时间:如果可以;从网站上获取,然后将相同的代码导入到您自己的编译器中,使用相同的数据集,并计时。

当您将其作为基线时,您可以开始研究改进它的方法,并找出大部分时间在哪里。有一些相对简单的简单改进,比如使用指针而不是数组访问 - 但主要的改进可能来自算法更改。我们根本无法帮助你,因为我们不知道问题要求你做什么 - 如果我们确实给你代码以更有效的方式做同样的工作,这将不是一个公平的挑战

I am getting the error of time limit exceeded while executing the code in hackerearth compiler, the code is giving the correct output but while submitting the code the time limit exceeds, please help in this code, is it not optimized.

What I have tried:

#include <stdio.h>

main()
{
    long long int n,q,i,x,y,a[1000001],s;

    scanf("%lld %lld",&n,&q);//no of array elements and querries

    for(i=1;i<=n;i++)
        scanf("%lld",&a[i]);

    while(q--)
    {
        x=0,y=0,s=0;

        scanf("%lld %lld",&x,&y);//upper and lower bound of arrays

        for(i=x;i<=y;i++)
            s = s + a[i];

        s = s/(y-x+1);
        printf("%lld\n",s);
    }
}

解决方案

As far as I understand you are using an online compiler that times out.

While I don't know for sure while that happens it might be that the compiler is initialising stack variables with default values (as other compilers do too at least with debug builds) which took some time.

You have a very large array of 64-bit integers on the stack occupying 8 million bytes which is close to the default stack size of 8 MB used by most compilers.

You can try to allocate the array on the heap using malloc() after the size has been entered. That is the common method.

If you want to show your skills you should also avoid common beginner mistakes (array start index not zero, main does not return an int) and check user inputs for validity (sizes greater than zero and below a max. value, bounds within array size and upper bound larger than lower).


Those sites are giving you challenges.

Quote:

is it not optimized.


Optimizing is exactly the object of the challenge. Most strait forward solution is never the right solution.
Your program is optimum if requested 1 sum, but not if you are requested many partial sums.
Said otherwise, the solution is a trade off, you spend time to compute something that will help you to get fast answers.

Your challenge is to find how to do, if we give you the solution, then you fail the challenge.


We can't help you fix this: it's your challenge, not ours.
But the first thing you need to do is try to find out how long it is taking: if you can;t get that from the site, then import the same code into your own compiler, use the same dataset, and time it.
When you have that as a baseline, you can start working out what improves it, and finding out where the bulk of the time is being taken. There are trivial improvements that are relatively simple, like using pointers instead of array accesses - but the major improvements are likely to come from algorithmic changes. And we can't help you there at all, since we have no idea what the question is asking you to do - plus it wouldn't be a fair challenge if we did give you code for a more efficient way to do the same job.


这篇关于时间限制超过了hackerearth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆