forked from Guarav/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIEHARD.java
67 lines (49 loc) · 1.39 KB
/
DIEHARD.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package OC;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
/**
* Created by gaurav on 5/19/14.
*/
public class Main {
private int memoized[][][] = new int[2001][2001][2];
public static void main(String [] args)
{
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
Main diehard = new Main();
while(t-->0)
{
int a = scanner.nextInt();
int h = scanner.nextInt();
int ans = diehard.getMaximumTime(a, h);
System.out.println( ans );
}
}
public int getMaximumTime(int a, int h)
{
// starting point is by adding 3 to a and 2 to h
int startA = a + 3;
int startH = h + 2;
return getMaximumTime(startA, startH, 1) + 1;
}
private int getMaximumTime(int a, int h, int level)
{
if( a <= 0 || h <= 0 )
return -1;
if( memoized[a][h][level] != 0 )
return memoized[a][h][level];
if( level % 2 == 0 )
{
// add to air
return ( memoized[a][h][level] = 1 + getMaximumTime(a + 3, h + 2, (level + 1)%2) );
}
else
{
return ( memoized[a][h][level] = 1 + Math.max(getMaximumTime(a - 5, h - 10, (level + 1)%2), getMaximumTime(a - 20, h + 5, (level + 1)%2)) );
}
}
Main()
{
}
}