Weird problem while coding in JAVA (USANBOLT)

I coded this question’s solution in JAVA: Usain Bolt vs Tiger,
but getting WA, although the logic is the same as other accepted submissions, just the difference that I could figure out is that I am using the ‘Reader Class’ for Faster I/O,

Code is:

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

//coded by leucocephalus, this is JAVA Code, so don't mind the length of it :)
class Codechef
{
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }


        public StringBuffer readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }

            StringBuffer stringBuffer = new StringBuffer(new String(buf,0,cnt));
            return stringBuffer;
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }

    public static void main(String args[]) throws IOException {
        Reader rd = new Reader() ;
        int T=rd.nextInt() ;
       while (T-->0){
           long f=rd.nextInt(),d2b=rd.nextInt(),ta=rd.nextInt(),bs=rd.nextInt();

           if(Math.sqrt(2*(f+d2b)/ta)<=(f/bs)) System.out.println("Tiger");
           else System.out.println("Bolt");

       }
}

}

Kindly tell me why this is happening and what’s the fix for this,
Thanks in advance :slight_smile:

Hi, i don’t know java ,but i think you need to use double or float and check if sqrt(2*(f+d2b)/ta) {with some floating point precision}<=(f/bs).
actually , it was said in the acm regional contest problem discussion that most of them used a integer to store and check the distances, due to precision errors most of them couldn’t solve the problem in first try .
Hope this helped : )

1 Like

Just do something like: (float)sqrt(2*(f+d2b)/ta) -> this should work.

2 Likes

Thanks @executoner5440 , you got it right…:smile:

Correct implementation was:

 if(Math.sqrt((double) 2* (double)(f+d2b)/ta) <= ((double) f/bs)) System.out.println("Tiger");
            else System.out.println("Bolt");

Was this asked in ICPC? (as you gave the instance of people who stored in integer…)

1 Like

Yep, this was asked in recent icpc amrita regional which i attended as a school team :slight_smile:
Editorials for all problems from that contest were discussed after the contest and those editorial slides are posted on mirror contest page.
You can check them out.