-
Notifications
You must be signed in to change notification settings - Fork 0
/
a_151_Chocola.java
46 lines (36 loc) · 1.22 KB
/
a_151_Chocola.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
import java.util.* ;
public class a_151_Chocola {
public static void main(String[] args) {
int n=4, m=6 ;
Integer costVer[] = { 2, 1, 3, 1, 4 } ; // m-1
Integer costHor[] = { 4, 1, 2 } ; // n-1
Arrays.sort(costHor, Collections.reverseOrder() ) ;
Arrays.sort(costVer, Collections.reverseOrder() ) ;
int v=0 , h=0 ;
int vp = 1 , hp= 1 ;
int cost = 0 ;
while(h < costHor.length && v < costVer.length ) {
if(costVer[v] <= costHor[h] ) { // horizonatal cut
cost += ( costHor[h] * vp ) ;
hp++ ;
h++ ;
}
else{ // verticals cut
cost += ( costVer[v] * hp ) ;
vp++ ;
v++ ;
}
}
while(h < costHor.length) { // Remaining horizonatal cut
cost += ( costHor[h] * vp ) ;
hp++ ;
h++ ;
}
while(v < costVer.length ) { // Remaining vertical cut
cost += ( costVer[v] * hp ) ;
vp++ ;
v++ ;
}
System.out.println("Min cost of cut = " + cost);
}
}