作爲一門功課,我有以下程序用Java做:揹包變型算法
書櫃,我們有哪些必須由專人用K作家複製ň一摞書。 每本書都有Ui頁,其中Ai是本書。
我們需要爲每位作家提供連續的書籍,而且我們不能拆分書籍的頁面。
製作一個程序,它可以爲作者提供書籍,但通過最小化作者將複製的最大頁面數量。
作爲輸入,用戶將給出一串數字,其中第一個數字是書籍的數量N,第二個數字是作家的數量K,其餘的數字是每本書的頁數。
作爲輸出,程序將向用戶輸出作者將複製的最大頁數。
實施例:
輸入:15 6 30 40 10 40 50 20 30 40 10 70 10 50 30 50 10
輸出:90
在這個例子中,第一寫入器可以採取BOOK1 = 30和book2 = 40,但不能拿book1 = 30和book3 = 10。換句話說,你只能從堆棧頂部拿書,而不能把它們混合起來。
這是我實現:
import java.util.*;
public class Library {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// to work with 1.6 erase the second "Integer"
//in 1.7 this works properly
List<Integer> booksList = new LinkedList<Integer>();
System.out.printf("Give: ");
String answer = input.nextLine();
String[] arr = answer.split(" ");
for (String num : arr) {
booksList.add(Integer.parseInt(num));
}
int books = booksList.remove(0);
int writers = booksList.remove(0);
while (booksList.size() > writers) {
mergeMinimalPair(booksList);
}
System.out.println(getMax(booksList));
}
public static void mergeMinimalPair(List<Integer> books) {
int index = 0;
int minValue = books.get(0) + books.get(1);
for (int i = 1; i < books.size() - 1; i++) {
if ((books.get(i) + books.get(i + 1)) < minValue) {
index = i;
minValue = books.get(i) + books.get(i + 1);
}
}
combine(books, index, index + 1);
}
public static void combine(List<Integer> books, int indexA, int indexB) {
Integer a = books.get(indexA);
Integer b = books.get(indexB);
books.remove(indexB);
books.add(indexA, a + b);
books.remove(indexB);
}
public static int getMax(List<Integer> books) {
int max = books.get(0);
for (int i = 1; i < books.size(); i++) {
if (books.get(i) > max) {
max = books.get(i);
}
}
return max;
}
}
我做的每一個我融合在一起的時間是對最小的書,直到我的列表的長度等於作家的數量,但它不工作,在這個例子中,而不是90它輸出100.
我聽說過動態規劃解決方案和殘酷的解決方案,像揹包一樣的問題,但在我的大學他們還沒有教我們關於動態規劃,所以無論教授是困惑的關於我們所知道的或他希望我們找到一個殘酷的解決方案。
我確定我的解決方案可以工作,但由於某種原因,它只是沒有,如果你能指出我在這方面的另一個解決方案的提示或我誤以爲我會很高興。
您可以指向DP解決方案或Brutal解決方案,但如果您指向DP解決方案,請注意我幾乎不知道DP實施。
編輯:我已經看過一些揹包類問題,但我無法找到一個具有這種變化和非DP的解決方案,我能理解
我可以在這裏看到不少解決方案。 – g13n
@ g13n我看了一些類似揹包的問題,但我找不到這個特別的變化,尤其是沒有DP解決方案 –
你有沒有看過相關的問題給你,我可以看到一堆暴力解決方案;-) – g13n