102 - Ecological Bin Packing

UVa網站題目連結
My Solved Problems Performance

Background

Bin packing, or the placement of objects of certain weights into different bins subject to certain constraints, is an historically interesting problem. Some bin packing problems are NP-complete but are amenable to dynamic programming solutions or to approximately optimal heuristic solutions.

In this problem you will be solving a bin packing problem that deals with recycling glass.

The Problem

Recycling glass requires that the glass be separated by color into one of three categories: brown glass, green glass, and clear glass. In this problem you will be given three recycling bins, each containing a specified number of brown, green and clear bottles. In order to be recycled, the bottles will need to be moved so that each bin contains bottles of only one color.

The problem is to minimize the number of bottles that are moved. You may assume that the only problem is to minimize the number of movements between boxes.

For the purposes of this problem, each bin has infinite capacity and the only constraint is moving the bottles so that each bin contains bottles of a single color. The total number of bottles will never exceed 2^31.

The Input

The input consists of a series of lines with each line containing 9 integers. The first three integers on a line represent the number of brown, green, and clear bottles (respectively) in bin number 1, the second three represent the number of brown, green and clear bottles (respectively) in bin number 2, and the last three integers represent the number of brown, green, and clear bottles (respectively) in bin number 3. For example, the line 10 15 20 30 12 8 15 8 31

indicates that there are 20 clear bottles in bin 1, 12 green bottles in bin 2, and 15 brown bottles in bin 3.

Integers on a line will be separated by one or more spaces. Your program should process all lines in the input file.

The Output

For each line of input there will be one line of output indicating what color bottles go in what bin to minimize the number of bottle movements. You should also print the minimum number of bottle movements.

The output should consist of a string of the three upper case characters 'G', 'B', 'C' (representing the colors green, brown, and clear) representing the color associated with each bin.

The first character of the string represents the color associated with the first bin, the second character of the string represents the color associated with the second bin, and the third character represents the color associated with the third bin.

The integer indicating the minimum number of bottle movements should follow the string.

If more than one order of brown, green, and clear bins yields the minimum number of movements then the alphabetically first string representing a minimal configuration should be printed.

Sample Input

1 2 3 4 5 6 7 8 9
5 10 5 20 10 5 10 20 10

Sample Output

BCG 30
CBG 50

Solution

  1. import java.io.BufferedInputStream;
  2. import java.util.Scanner;

  3. public class Main {

  4. static String sArray[] = {"BCG", "BGC", "CBG", "CGB", "GBC", "GCB"};
  5. static int iArray[] = new int[6];
  6. static int inArray[] = new int[9];
  7. static Scanner in = new Scanner(new BufferedInputStream(System.in));

  8. public static void main (String[] args) {

  9. while (in.hasNext()) {
  10. process();
  11. }
  12. }

  13. private static void process() {

  14. int total = 0;
  15. for (int i=0; i<9; i++) {
  16. inArray[i] = in.nextInt();
  17. total += inArray[i];
  18. }
  19. iArray[0] = total - inArray[0] - inArray[5] - inArray[7];
  20. iArray[1] = total - inArray[0] - inArray[4] - inArray[8];
  21. iArray[2] = total - inArray[2] - inArray[3] - inArray[7];
  22. iArray[3] = total - inArray[2] - inArray[4] - inArray[6];
  23. iArray[4] = total - inArray[1] - inArray[3] - inArray[8];
  24. iArray[5] = total - inArray[1] - inArray[5] - inArray[6];

  25. int min = iArray[0];
  26. int result = 0;
  27. for (int i=1; i<6; i++) {
  28. if (min > iArray[i]) {
  29. min = iArray[i];
  30. result = i;
  31. }
  32. }
  33. System.out.println(sArray[result] + " " + iArray[result]);
  34. }
  35. }
關鍵字:UVa Online Judge, ACM. Java

留言

這個網誌中的熱門文章

【房屋稅繳納攻略】2024/113年度信用卡回饋/分期整理!

【銀行代碼查詢】3碼銀行代碼列表、7碼分行代碼查詢

【博客來折價券】25/50/100/150現領現折+天天簽到換200+OP兩倍換!

【博客來折價券】博客來免費序號e-coupon分享(持續更新)

【Hami Video】免費Hami Video體驗/試用序號分享(隨時更新)!

【牌照稅繳納攻略】2024/113年度信用卡回饋/分期整理!

【所得稅繳納攻略】信用卡回饋/分期+拆單賺回饋教學!