Description
牛客网 2018校招真题 包裹运输
Solving Ideas
- 对于 6 × 6 的产品,每一个都需要一个包裹,而且无法填充
- 对于 5 × 5 的产品, 每一个都需要一个包裹,且包裹余下空间只能填充 1 × 1 的产品
- 对于 4 × 4 的产品, 每一个都需要一个包裹,余下空间能填充 2 × 2 或 1 × 1 的产品,根据贪心的思想,优先并尽可能地填充 size 更大的产品
- 对于3 × 3 的产品,一个包裹最多能填充 4 个 3 × 3 的产品,如果 3 × 3 的产品不能填充满一个包裹,则余下空间能填充 2 × 2 或 1 × 1 的产品,同样优先填充 size 更大的产品
- 对于 2 × 2 的产品,一个包裹最多能填充 9 个 2 × 2 的产品,如果 2 × 2 的产品不能填充满一个包裹,则余下空间能填充 1 × 1 的产品
- 最后计算需要多少个包裹来填充 1 × 1 的产品
Solution
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author wylu
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine()) != null) {
String[] strs = line.split(" ");
int[] counts = new int[strs.length];
boolean flag = true;
for (int i = 0; i < strs.length; i++) {
counts[i] = Integer.parseInt(strs[i]);
if (flag && counts[i] != 0) flag = false;
}
if (flag) return;
// 6 * 6
int res = counts[5];
// 5 * 5
res += counts[4];
counts[0] -= counts[4] * 11; //余下空间用来填充 1 × 1 的产品
// 4 * 4
res += counts[3];
int fillTwo = counts[3] * 5;
// 先尽可能地用 2 × 2 的进行填充
counts[1] -= (fillTwo >= counts[1]) ? counts[1] : fillTwo;
// 然后用 1 × 1 的进行填充
counts[0] -= (fillTwo >= counts[1]) ? (fillTwo - counts[1]) : 0;
// 3 * 3
int rmd = counts[2] % 4;
res += (counts[2] / 4 + (rmd == 0 ? 0 : 1));
int[] two = {0, 5, 3, 1}, one = {0, 27, 18, 9};
if (rmd > 0) {
// 先尽可能地用 2 × 2 的进行填充
counts[1] -= (two[rmd] >= counts[1]) ? counts[1] : two[rmd];
// 然后用 1 × 1 的进行填充
counts[0] -= (two[rmd] >= counts[1]) ? (one[rmd] - counts[1] * 4) : (8 - rmd);
}
// 2 * 2
res += (counts[1] / 9 + (counts[1] % 9 == 0 ? 0 : 1));
if (counts[1] % 9 > 0) counts[0] -= (36 - counts[1] % 9 * 4);
// 1 * 1
if (counts[1] > 0) res += (counts[0] / 36 + (counts[0] % 36 == 0 ? 0 : 1));
System.out.println(res);
}
}
}