Submission #117077

# Submission time Handle Problem Language Result Execution time Memory
117077 2019-06-14T16:04:57 Z emilem Board (CEOI13_board) Java 11
50 / 100
200 ms 185624 KB
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

class board
{
	static final BigInteger BIGTWO = BigInteger.valueOf(2);
    static BigInteger min(BigInteger a, BigInteger b)
    {
        return a.compareTo(b) < 0 ? a : b;
    }
	static int Depth(String aString)
	{
	    char [] a = aString.toCharArray();
		int depth = 0;
		for (char i: a)
			if (i == 'U')
				--depth;
			else if (i == '1' || i == '2')
				++depth;
		return depth;
	}
	static BigInteger Coord(String aString)
	{
	    char [] a = aString.toCharArray();
		BigInteger coord = BigInteger.ZERO;
		for (char i: a)
			switch(i)
			{
			case '1':
				coord = coord.multiply(BIGTWO); break;
			case '2':
				coord = coord.multiply(BIGTWO);
				coord = coord.add(BigInteger.ONE); break;
			case 'L':
				coord = coord.subtract(BigInteger.ONE); break;
			case 'R':
				coord = coord.add(BigInteger.ONE); break;
			case 'U':
				coord = coord.divide(BIGTWO);
			}
		return coord;
	}
    public static void main(String[] args)
	{
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
		String path = in.nextString();
		int aDepth = Depth(path);
		BigInteger aCoord = Coord(path);
		path = in.nextString();
		int bDepth = Depth(path);
		BigInteger bCoord = Coord(path);
		if (aDepth > bDepth)
		{
		    int temp1 = aDepth; aDepth = bDepth; bDepth = temp1;
			BigInteger temp2 = aCoord; aCoord = bCoord; bCoord = temp2;
		}
		int depthSteps = bDepth - aDepth;
		bDepth = aDepth;
		bCoord = bCoord.shiftLeft(-depthSteps);
		BigInteger ans = aCoord.subtract(bCoord).abs().add(BigInteger.valueOf(depthSteps));
		while (aDepth-- > 0)
		{
			depthSteps += 2;
			aCoord = aCoord.divide(BIGTWO);
			bCoord = bCoord.divide(BIGTWO);
			ans = min(ans, aCoord.subtract(bCoord).abs().add(BigInteger.valueOf(depthSteps)));
		}
        out.println(ans);
        out.close();
	}

    static class InputReader
	{
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream)
		{
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String nextString()
		{
            while (tokenizer == null || !tokenizer.hasMoreTokens())
			{
                try { tokenizer = new StringTokenizer(reader.readLine()); }
                catch (IOException e) { throw new RuntimeException(e); }
            }
            return tokenizer.nextToken();
        }

        public int nextInt()
		{
            return Integer.parseInt(nextString());
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 78 ms 10100 KB Output is correct
2 Correct 78 ms 9844 KB Output is correct
3 Correct 78 ms 9976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 25944 KB Output is correct
2 Correct 106 ms 13164 KB Output is correct
3 Correct 179 ms 25040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 9756 KB Output is correct
2 Correct 82 ms 10076 KB Output is correct
3 Correct 101 ms 9912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 142 ms 15308 KB Output is correct
2 Execution timed out 279 ms 32596 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 82 ms 10040 KB Output is correct
2 Correct 86 ms 9848 KB Output is correct
3 Correct 90 ms 10004 KB Output is correct
4 Correct 97 ms 9704 KB Output is correct
5 Correct 84 ms 9860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 133 ms 13708 KB Output is correct
2 Correct 152 ms 15068 KB Output is correct
3 Correct 77 ms 10064 KB Output is correct
4 Correct 82 ms 9972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 22812 KB Output is correct
2 Execution timed out 367 ms 49200 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1053 ms 185624 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1051 ms 183780 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1028 ms 184672 KB Time limit exceeded
2 Halted 0 ms 0 KB -