-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDBOY.java
More file actions
66 lines (42 loc) · 1.33 KB
/
DBOY.java
File metadata and controls
66 lines (42 loc) · 1.33 KB
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
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
class Ideone{
//https://www.codechef.com/problems/DBOY
public static Scanner scn = new Scanner(System.in);
public static void main (String[] args) throws java.lang.Exception{
int T = scn.nextInt();
while(T-- > 0){
int N = scn.nextInt();
int[] H = new int[N];//house array
int[] K = new int[N];//refuel quantity
for(int i = 0; i < N; i++)
H[i] = scn.nextInt();
for(int i = 0; i < N; i++)
K[i] = scn.nextInt();
HashMap<Integer, Long> dp = new HashMap<>();
long ans = 0;
for(int distance : H){
int fuelNeeded = distance << 1;//fuel needed is double the distance
ans += getRefuelCount(fuelNeeded, K, dp);
}
System.out.println("" + ans);
}
}
public static long getRefuelCount(int fuelNeeded, int[] K, HashMap<Integer, Long> dp){
if(fuelNeeded < 0)
return Integer.MAX_VALUE;
if(fuelNeeded == 0)
return 0;
//if already computed return that answer
if(dp.containsKey(fuelNeeded))
return dp.get(fuelNeeded);
long ans = Integer.MAX_VALUE;
for(int refuelAmt : K)
ans = Math.min(ans , getRefuelCount(fuelNeeded - refuelAmt, K, dp));
//saving answer in dp
dp.put(fuelNeeded, 1 + ans);
return dp.get(fuelNeeded);
}
}