problem 100 TLE in java?

Write here if you have problems with your Java source code

Moderator: Board moderators

problem 100 TLE in java?

Postby vinu76jsr » Mon May 26, 2008 10:14 pm

following is my code which returns a TLE I am new and its almost 6 hrs and 16 submissions straight to but couldn't get correct somebody please help

Code: Select all
import java.io.BufferedReader;
import java.util.*;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
    public static void main(String[] args) throws IOException {
        try{
        int x=0;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (x==0) {
            String line = reader.readLine();
            if (line == null) {
                x=1;
            }
            if(x!=1){
               

            StringTokenizer tokenizer = new StringTokenizer(line);
            int num1 = Integer.parseInt(tokenizer.nextToken());
            int num2 = Integer.parseInt(tokenizer.nextToken());
            if(num1>10000||num2>10000/||num1<0||num2<0)break;
            int temp,countmax=1,count,min,max;
            if (num1 < num2) { min=num1; max=num2; } else { min=num2; max=num1; }
            for(int i=min;i<=max;i++)
            {
              count=1;
              temp=i;
              while(i!=1)
              {
                 if((i%2)==1)
                    i=i*3+1;
                 else
                    i=i/2;
                 count++;
              }
              if(countmax<count)
                 countmax=count;             
              i=temp;
             }
             System.out.println(num1+" "+num2+" "+countmax); 
          }           
        }   
    }
}
}
vinu76jsr
New poster
 
Posts: 1
Joined: Mon May 26, 2008 10:08 pm

Re: problem 100 TLE in java?

Postby MAK » Sat May 31, 2008 11:36 pm

Suggestions:
Use Scanner for input
Use longs (the problem statement talks of 32bit ints. Java ints are 32 bits but they are signed so they are effectively 31 bits for positive numbers)
Get rid of this line
if(num1>10000||num2>10000||num1<0||num2<0)break;
Input is guaranteed to be valid, so no need to check. Actually this causes WA because input can be as large as 999999

Everyone gets lots of WA/TLE/CE at first, that is no reason to feel frustrated.

Hope this helps
MAK
New poster
 
Posts: 28
Joined: Sun Oct 23, 2005 3:44 pm
Location: Dhaka, Bangladesh

Re: problem 100 TLE in java?

Postby mf » Sun Jun 01, 2008 11:01 am

MAK wrote:Use Scanner for input

Actually, Scanner is quite a bit slower than BufferedReader + StringTokenizer combo.

Use longs (the problem statement talks of 32bit ints. Java ints are 32 bits but they are signed so they are effectively 31 bits for positive numbers)

It doesn't matter in this problem.


I'd suggest to replace % and / in 'if((i%2)==1) i=i*3+1; else i=i/2;' by bitwise operators:
Code: Select all
if ((i & 1) == 1)
    i = i * 3 + 1;
else
    i >>= 1;
mf
Guru
 
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland


Return to Java

Who is online

Users browsing this forum: No registered users and 1 guest