简体   繁体   English

谁能解释我如何克服 Java 中的 TLE 错误。 我在 3 个输入中超过了 Time Limit Exceeded,这些输入将 100000 个测试用例作为输入

[英]Can anyone explain me how to overcome TLE error in Java. I'm getting Time Limit Exceeded in 3 inputs which are taking 100000 test cases as input

Can anyone explain me how to overcome TLE error in Java.谁能解释我如何克服 Java 中的 TLE 错误。

So, I implemented a seating arrangement code in which first line of input take test cases and second line of input takes seat number.因此,我实现了一个座位安排代码,其中第一行输入用于测试用例,第二行输入用于座位号。 In output I've to display the facing seat number of my input and seat type WS, AS or MS.在输出中,我必须显示我输入的面向座位号和座位类型 WS、AS 或 MS。

For example:例如:

2 // test cases
18 // seat number
40 // seat number
19 WS // Output
45 AS // Output

Here is my code which I implemented :这是我实现的代码:

import java.io.*;

class Solution9
{
    public static void main(String arg[])throws Exception
    {
        int tc;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    tc=Integer.parseInt(br.readLine());
    int sno,q,r;
    String type,s="";
    for(int i=0;i<tc;i++)
    {
        sno=Integer.parseInt(br.readLine());
        sno--;
        q=sno/12;
        r=sno%12;
        int csno;
        csno=11-r;
        csno+=q*12;
        r%=6;
        if(r==0 || r==5)
            type="WS";
        else if(r==1 || r==4)
            type="MS";
        else
            type="AS";
        s=s+ (csno+1) + " " + type + "\n" ;
    }
        System.out.println(s);    
    }
}

If you run your program under control of a profiler tool, you'll most probably notice a huge amount of time spent in StringBuilder activities around your line如果您在分析器工具的控制下运行您的程序,您很可能会注意到您的生产线周围的StringBuilder活动花费了大量时间

s=s+ (csno+1) + " " + type + "\n" ;

Repeatedly adding something to a String is inefficient, as every time it does the following:String重复添加某些内容是低效的,因为每次它都会执行以下操作:

  • Copy the existing string s into a StringBuilder (takes time proportional to the ever-increasing length of the string),将现有的字符串s复制到StringBuilder (花费的时间与字符串不断增加的长度成正比),
  • append the other components to the StringBuilder ,将其他组件附加到StringBuilder
  • create a new String from the StringBuilder contents (again takes time proportional to the ever-increasing length of the contents).StringBuilder内容创建一个新String (同样需要时间与内容的不断增加的长度成正比)。

As you aren't really interested in the intermediate Strings, but only in the final result, replace由于您对中间字符串并不真正感兴趣,而只对最终结果感兴趣,因此请替换

String s="";

with

StringBuilder s=new StringBuilder();

and

s=s+ (csno+1) + " " + type + "\n" ;

with

s.append(csno+1).append(" ").append(type).append("\n");

This should give a substantial performance boost.这应该会显着提高性能。

As String s in Java are immutable, "changing" anything about a String means to somehow create a new String (and probably forget about the old one).由于 Java 中的String是不可变的,因此“更改”有关String任何内容都意味着以某种方式创建一个新的String (并且可能忘记旧的)。 So it's better to use a StringBuilder until you're ready for the final result, because StringBuilder s allow changing their contents without repeatedly creating new ones.因此最好在您准备好获得最终结果之前使用StringBuilder ,因为StringBuilder允许更改其内容而无需重复创建新内容。

I doubt your problem is the performance of your code.我怀疑您的问题是代码的性能。 It could be improved but not by an order of magnitude or anything.它可以改进,但不能提高一个数量级或任何东西。 I suspect the problem is that your input file is improperly formed, where it is saying that it has more lines than it actually has.我怀疑问题在于您的输入文件格式不正确,它说它的行数比实际行数多。 Since your first line says how many lines to expect and you are streaming from System.in rather than using File io, it will wait until it sees the lines it expects.由于您的第一行说明了预期的行数,并且您是从 System.in 流式传输而不是使用 File io,因此它将一直等到它看到预期的行。 System.in will not produce an EOF exception the way a FileReader would, it'll just wait. System.in 不会像 FileReader 那样产生 EOF 异常,它只会等待。

Edit: Oh!编辑:哦! Wait!等待! I take it back.我把它收回。 You're collecting the entire output into a single string and not outputting any of it until your entire input is processed.您将整个输出收集到一个字符串中,并且在处理整个输入之前不输出任何输出。 Think how huge that string is getting!想想那个字符串变得多么巨大! You're probably running out of memory.您可能内存不足。

Also, Ralf Kleberhoff is completely correct that you're making a new string (copying the whole string in order to add to it) for every row.此外,Ralf Kleberhoff 是完全正确的,您正在为每一行创建一个新字符串(复制整个字符串以添加到它)。 Consider how much is being copied as you're doing the last couple hundred of your 100,000 rows.考虑在您执行 100,000 行中的最后几百行时复制了多少。

You really have to be outputting as you go along.你真的必须在进行时输出。 This makes it awkward to test manually from standard in, but you should get over that.这使得从标准输入手动测试变得很尴尬,但您应该克服这一点。 Test from test files.从测试文件进行测试。

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

相关问题 如何克服此 Time Limit Exceeded 错误? - How can I overcome this Time Limit Exceeded error? 如何删除此 java 代码的 Time Limit Exceeded(TLE) 错误 - How to remove Time Limit Exceeded(TLE) error for this java code 为什么我在使用 ArrayList 时出现 Time Limit Exceeded (TLE) 错误<string[]>而不是 int[][]?</string[]> - Why am I getting Time Limit Exceeded (TLE) error when using ArrayList<String[]> instead of int[][]? 谁能解释我如何在java中读取文件。 -1意味着什么? 在我想要阅读的文件中没有任何数字.. confused :( - Can anyone explain me how to read a file in java. what does -1 means? there no any numbers in the file i want to read.. confused :( 我是android Java的初学者。 谁能解释代码 - I am beginner in android Java. Can anyone explain the code 遇到找不到或无法加载主类错误的情况,有人可以帮助我修复该错误吗? 我是Java新手 - Getting a could not find or load main class error, can anyone help me fix it? I'm new to java Java中获取时间限制超出错误 - Getting Time limit exceeded error in java 阵列旋转 TLE(超出时间限制) - Array rotation TLE (Time Limit Exceeded) 超出圆形阵列旋转时间限制(TLE) - Circular Array Rotation Time Limit Exceeded(TLE) 有人可以解释为什么我在Java中遇到此错误吗? - Can someone explain why I'm getting this error in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM