Algorithm, Data structure/Solved Algorithmic Problem

UVa - Ecological Bin Packing

JaykayChoi 2016. 5. 27. 00:30

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


출처: https://uva.onlinejudge.org/



brown, greenclear 로 구분되는 3종류의 재활용 병이 있고, 각각의 병을 3개의 통에 담는 문제입니다. 입력은 한 줄 마다 9개의 정수가 있고, 첫 번째 세 개의 정수는 1번 통에 있는 brown, green, and clear bottles 의 개수입니다. 다음 각 세 개의 정수는 2번 통, 3번 통에 있는 각 병의 개수입니다. 세 가지 종류의 병을 각각의 통에 분리하여 담는다고 할 때 옮긴 횟수를 최소화할 수 있는 통의 순서와 옮긴 횟수를 구하는 문제입니다.

이 문제는 별 다른 풀이법이 생각나지 않아 naive 하게 풀었습니다. 더 좋은 풀이법이 있을 것 같네요.

3개의 통을 배열하는 방법은 3! = 6 가지 입니다. 6개의 방법만 구현해보면 되니 굳이 재귀함수로 수열을 만들 이유는 없지만 통이 더 많이지는 경우도 고려하여 재귀함수로 수열을 만들었습니다. 마지막으로 output 을 검토한 결과 수열의 우선 순위가 BGC가 아닌 BCG 인 점이 좀 이해가 되지 않네요. (독해의 문제일지도)




my solving 

c++

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <queue>
#include <string>
#include <cstring>
using namespace std;
 
 
int bin[3][3= { 0, };
int minValue = 2147483647;
string minValueBinOrder = "BGC";
 
void solveNaive(int* seq)
{
    string curStr = "";
    for (int i = 0; i < 3; i++)
    {
        switch (seq[i])
        {
        case 0:
            curStr += "B";
            break;
        case 1:
            curStr += "G";
            break;
        case 2:
            curStr += "C";
            break;
        }
    }
 
    int sum = 0;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            if (seq[i] == j)
            {
                continue;
            }
            sum += *(*(bin + i) + j);
 
            if (sum >= minValue)
                return;
        }
    }
 
    if (minValue > sum)
    {
        minValue = sum;
        minValueBinOrder = curStr;
    }
 
}
 
void makeSequence(int* arr, int n, int size)
{
    if (n == size - 1)
    {
        int seq[3];
        for (int i = 0; i < size; i++)
            seq[i] = *(arr + i);
        solveNaive(seq);
        return;
    }
 
    for (int i = n; i < size; i++)
    {
        int temp = arr[n];
        arr[n] = arr[i];
        arr[i] = temp;
 
        makeSequence(arr, n + 1, size);
    }
 
    int temp = arr[n];
    for (int i = n; i < size - 1; i++)
        arr[i] = arr[i + 1];
    arr[size - 1= temp;
}
 
int main()
{
    int a[9= { 0, };
    while (cin >> a[0>> a[1>> a[2>> a[3>> a[4>> a[5>> a[6>> a[7>> a[8])
    {
        memset(bin, 0sizeof(bin));
        minValue = 2147483647;
        minValueBinOrder = "BGC";
 
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                bin[i][j] = a[i * + j];
            }
        }
        int a[3= { 0,2,};
        makeSequence(a, 03);
 
        cout << minValueBinOrder << " " << minValue << endl;
    }
    return 0;
}
cs







'Algorithm, Data structure > Solved Algorithmic Problem' 카테고리의 다른 글

USACO 1.1 - Greedy Gift Givers  (0) 2016.05.29
Project Euler #26 - Reciprocal cycles  (0) 2016.05.28
USACO 1.1 - Your Ride Is Here  (0) 2016.05.27
UVa -Train Swapping  (0) 2016.05.25
UVa -Power Crisis  (0) 2016.05.25