forked from nanwan03/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1190 生日蛋糕.java
70 lines (69 loc) · 1.72 KB
/
1190 生日蛋糕.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
67
68
69
70
import java.util.*;
public class Main {
private static int minArea = 0x3f3f3f3f;
private static int[] minV = new int[30];
private static int[] minA = new int[30];
private static int vol = 0;
private static int level = 0;
private static int curArea = 0;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
vol = in.nextInt();
level = in.nextInt();
minV[0] = 0;
minA[0] = 0;
for (int i = 1; i <= level; ++i) {
minV[i] = minV[i - 1] + i * i * i;
minA[i] = minA[i - 1] + 2 * i * i;
}
if (minV[level] > vol) {
System.out.println(0);
} else {
int maxH = (vol - minV[level - 1]) / (level * level) + 1;
int maxR = (int)Math.sqrt((vol - minV[level - 1]) / level) + 1;
helper(vol, level, maxR, maxH);
minArea = (minArea == 0x3f3f3f3f) ? 0 : minArea;
System.out.println(minArea);
}
}
private static void helper(int volLeft, int curLevel, int maxR, int maxH) {
if (curLevel == 0) {
if (volLeft == 0) {
minArea = Math.min(minArea, curArea);
}
return;
}
if (volLeft <= 0) {
return;
}
if (minV[curLevel] > volLeft) {
return;
}
if (curArea + minA[curLevel] >= minArea) {
return;
}
if (maxH < curLevel || maxR < curLevel) {
return;
}
if (maxVLeft(curLevel, maxH, maxR) < volLeft) {
return;
}
for (int i = maxR; i >= curLevel; --i) {
if (curLevel == level) {
curArea = i * i;
}
for (int j = maxH; j >= curLevel; --j) {
curArea += 2 * i * j;
helper(volLeft - i * i * j, curLevel - 1, i - 1, j - 1);
curArea -= 2 * i * j;
}
}
}
private static int maxVLeft(int curLevel, int curH, int curR) {
int rst = 0;
for (int i = 0; i < curLevel; ++i) {
rst += (curR - i) * (curR - i) * (curH - i);
}
return rst;
}
}