我想知道在处理时间方面批处理给定数字的最佳方法是什么。
拿物品:9, 18, 7, 8, 4, 9, 11, 15, 3, 8,
(项目1的处理时间为9,项目2的处理时间为18,等等)
如果批处理时间限制设置为20,则可能将项目分组为批次:{1, 3, 5} {2} {4, 6} {8, 9} {7, 10}
(组1为9 + 7 + 4 = 20)等等5批次项目已经在内容<= 20的情况下进行了。
理想情况下,我希望将它们分组为尽可能少的组。以上情况至少有5组,内容限制为20 ...
由于
答案 0 :(得分:4)
如果批处理时间限制设置为20,...
所以我假设没有大于批处理时间限制的元素。这是我的方法:
在Java中实施:
int[] input = { 9, 18, 7, 8, 4, 9, 11, 15, 3, 8 }; // Input items.
Arrays.sort(input); // Sort the items.
int left = 0, right = input.length - 1; // Left and right pointers.
int remainder = 0, limit = 20;
// list of groups.
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
while (right >= left)
{
ArrayList<Integer> subList = new ArrayList<Integer>();
list.add(subList);
// Add the current maximum element.
subList.add(input[right]);
if (right == left)
break;
remainder = limit - input[right];
right--;
// Add small elements until limit is reached.
while (remainder > input[left]) {
subList.add(input[left]);
remainder = remainder - input[left];
left++;
}
remainder = 0; // Reset the remainder.
}
打印论坛:
for (ArrayList<Integer> subList : list)
{
for (int i : subList)
System.out.print(i + " ");
System.out.println();
}
输出(每行代表一组数字)
18
15 3
11 4
9 7
9 8
8
答案 1 :(得分:3)
迭代:
IN: 9, 18, 7, 8, 4, 9, 11, 15, 3, 8
S1 (18) : 2:18 IN: 9, _, 7, 8, 4, 9, 11, 15, 3, 8
S2 (19) : 8:15, 5:4 IN: 9, _, 7, 8, _, 9, 11, _, 3, 8
S3 (20) : 7:11, 1:9 IN: _, _, 7, 8, _, 9, _, _, 3, 8
S4 (20) : 6: 9, 4:8, 0:3 IN: _, _, 7, _, _, _, _, _, _, 8
S5 (15) : 10: 8, 3:7 IN: _, _, _, _, _, _, _, _, _, _
代码:
public class Knapsack {
public static void knapsack( int capacity, int[] values, List< int[] > indices ) {
int[] in = Arrays.copyOf( values, values.length );
List< Integer > workspace = new LinkedList<>();
int wCapacity = capacity;
boolean inProgress = true;
while( inProgress ) {
int greatestValue = -1;
int greatestIndex = -1;
for( int i = 0; i < in.length; ++i ) {
int value = in[i];
if( value > Integer.MIN_VALUE
&& greatestValue < value && value <= wCapacity )
{
greatestValue = value;
greatestIndex = i;
}
}
if( greatestIndex >= 0 ) {
workspace.add( greatestIndex );
in[greatestIndex] = Integer.MIN_VALUE;
wCapacity -= greatestValue;
} else if( workspace.isEmpty()) {
inProgress = false;
} else {
int[] ws = new int[workspace.size()];
for( int i = 0; i < workspace.size(); ++i ) {
ws[i] = workspace.get(i).intValue();
}
indices.add( ws );
workspace = new LinkedList<>();
wCapacity = capacity;
}
}
}
static void print( int[] values, List< int[] > indices )
{
int r = 0;
for( int[] t : indices ) {
String items = "";
int sum = 0;
for( int index : t ) {
int value = values[index];
if( ! items.isEmpty()) {
items += ", ";
}
items += index + ":" + value;
sum += value;
}
System.out.println( "S" + ++r + " (" + sum + ") : " + items );
}
}
public static void main( String[] args ) {
int[] values = { 9, 18, 7, 8, 4, 9, 11, 15, 3, 8 };
List< int[] > indices = new LinkedList<>();
knapsack( 20, values, indices );
print( values, indices );
}
}
结果:
S1 (18) : 1:18
S2 (19) : 7:15, 4:4
S3 (20) : 6:11, 0:9
S4 (20) : 5:9, 3:8, 8:3
S5 (15) : 9:8, 2:7