Uva oj 102-ecological bin packing (ecological Packing Problem)

Source: Internet
Author: User
Tags first string

Time Limit: 3.000 seconds
Time Limit: 3.000 seconds

 

Background
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.
The packing problem is that how to put objects of different weights into different boxes under certain constraints is an interesting question with a long history. Some packing problems are NP-complete, but they can be solved through dynamic programming or approximate optimal heuristic solutions.

In this problem you will be solving a bin packing problem that deals with recycling glass.
In this case, you need to solve the Packing Problem of a recycled glass bottle.

 

The Problem
Problem

Reconfiguring 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 reconfiguring 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.
Glass bottles need to be recycled by color into three types: Brown, green and colorless. In this case, you will get three waste bins, each containing a given number of brown, green, or colorless glass bottles. To facilitate recycling, the bottle needs to be split and moved so that each waste bin has only one color bottle.

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.
The problem is to minimize the number of bottles to be moved. You can assume that the only problem is to minimize the number of moves 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 231.
For the purpose of this problem, the capacity of each waste bin is infinite, and the only constraint is to move the bottle so that each waste bin has only one color bottle. The total number of bottles will never exceed 231.

 

The input
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:
The input consists of multiple rows of data. Each row contains nine integers. The first three integers in a row indicate the numbers of brown, green, and colorless bottles in the No. 1 waste bin, the three integers in the middle indicate the numbers of brown, green, and colorless bottles in the No. 2 waste bin, the last three integers indicate the numbers of brown, green, and colorless bottles in the No. 3 waste bin. For example, the following 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.
Indicates that there are 20 colorless bottles in the No. 1 waste bin, 12 green bottles in the No. 2 waste bin, and 15 brown bottles in the No. 3 waste bin.

Integers on a line will be separated by one or more spaces. Your program shocould process all lines in the input file.
Each integer in each row has one or more spaces. YourProgramAll rows in the input data to be processed.

 

The output
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 shoshould also print the minimum number of bottle movements.
Each line of input must have a corresponding line of output, indicating which color of the bottle is loaded into which waste bin to minimize the movement of the bottle. You should also print the minimum number of times the bottle moves.

The output shoshould 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 output uses three strings consisting of uppercase letters 'G', 'B', and 'C' (green, brown, and colorless respectively) to indicate the color of the bottles in each waste 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 first letter of the string is the color of the No. 1 waste bin, the second letter is the color of the No. 2 waste bin, and the third letter is the color of the No. 3 waste bin.

The integer indicating the minimum number of bottle movements shoshould follow the string.
Apply an integer after the string to output the minimum number of times the bottle is moved.

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 shocould be printed.
If there are more than one sequence of brown, green, and colorless waste bins that meet the same minimum number of moves, then input the smallest of the first string in alphabetical order.

 

Sample Input
Input example

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

 

Sample output
Output example

BCG 30
Cbg 50

 

Analysis
Analysis

The three colors are arranged in six ways. Calculate the number of moves required for each method and find the minimum value (No sort required ). Note that if the optimal value is the same, the dictionary order of the shift method should be smaller.

 

Solution
Answer

# Include <iostream> using namespace STD; int main (void) {// all 6 sort tables in three colors. // In alphabetical order. When the number of moves is the same, the latter must have more than 0th colors. Char * const pclrs [6] = {"BCG", "BGC", "cbg", "CGB", "GBC", "GCB "}; // cyclically process each line of input, and finally output the results for (int n [3] [3], amove [6], Nmin, I; cin> N [0] [0];) {// array n indicates the number of bottles of each color in each box. Dimension 1st is the number of a box, 2nd dimension: bottle color: CIN> N [0] [1]> N [0] [2]> N [1] [0]> N [1] [1]; // The input sequence is BGC. The input of the first data is placed in the loop header, to exit CIN> N [1] [2]> N [2] [0]> N [2] [1]> N [2] [2]; // calculate the number of moves for each method in the preceding six ways. // The number of moves for each method is 0th and the number of moves for the other 1st boxes is B, amoamove [0] = N [1] [0] + N [2] [0] + N [0] [2] + N [2] [2] + N [0] [1] + N [1] [1]; amove [1] = N [1] [0] + N [2] [0] + N [0] [1] + N [2] [1] + N [0] [2] + N [1] [2]; amove [2] = N [1] [2] + N [2] [2] + N [0] [0] + N [2] [0] + N [0] [1] + N [1] [1]; // 3rd migration types To move the G in 0th and 2nd boxes, amoamove [3] = N [1] [2] + N [2] [2] + N [0] [1] + N [2] [1] + N [0] [0] + N [1] [0]; amove [4] = N [1] [1] + N [2] [1] + N [0] [0] + N [2] [0] + N [0] [2] + N [1] [2]; amove [5] = N [1] [1] + N [2] [1] + N [0] [2] + N [2] [2] + N [0] [0] + N [1] [0]; // find the smallest number of moves for (Nmin = I = 0; ++ I <6; Nmin = amove [I] <amove [Nmin]? I: Nmin); // the minimum value of the output result cout <pclrs [Nmin] <''<amove [Nmin] <Endl;} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.