示例:
输入:
5 100 10
10 20 30 40 50
3 4 5 6 10
20 30 20 40 30
输出:
0 30 0 40 0
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int number = scanner.nextInt();int N = scanner.nextInt();int X = scanner.nextInt();int[] returnList = new int[number];int[] riskList = new int[number];int[] maxCostList = new int[number];for (int i = 0; i < number; i++) {returnList[i] = scanner.nextInt();}for (int i = 0; i < number; i++) {riskList[i] = scanner.nextInt();}for (int i = 0; i < number; i++) {maxCostList[i] = scanner.nextInt();}int[] maxReturnStatus = getMaxReturnStatus(number, N, X, returnList, riskList, maxCostList);for (int i = 0; i < number; i++) {System.out.print(maxReturnStatus[i] + " ");}}private static int[] getMaxReturnStatus(int number, int N, int X, int[] returnList, int[] riskList, int[] maxCostList) {number++;int[] returnListExtended = new int[number];int[] riskListExtended = new int[number];int[] maxCostListExtended = new int[number];System.arraycopy(returnList, 0, returnListExtended, 0, returnList.length);System.arraycopy(riskList, 0, riskListExtended, 0, riskList.length);System.arraycopy(maxCostList, 0, maxCostListExtended, 0, maxCostList.length);returnListExtended[number - 1] = 0;riskListExtended[number - 1] = 0;maxCostListExtended[number - 1] = 0;int maxReturn = 0;int maxReturnRisk = 0;int[] maxStatus = new int[number];for (int i = 0; i < number; i++) {for (int j = i + 1; j < number; j++) {if (riskListExtended[i] + riskListExtended[j] <= X) {int maxReturnProductIndex = (returnListExtended[i] > returnListExtended[j]) ? i : j;int otherReturnProductIndex = i + j - maxReturnProductIndex;int maxReturnCost = Math.min(N, maxCostListExtended[maxReturnProductIndex]);int otherReturnCost = Math.min(N - maxReturnCost, maxCostListExtended[otherReturnProductIndex]);int curReturn = returnListExtended[maxReturnProductIndex] * maxReturnCost+ returnListExtended[otherReturnProductIndex] * otherReturnCost;if (curReturn > maxReturn) {maxReturn = curReturn;maxReturnRisk = riskListExtended[i] + riskListExtended[j];for (int k = 0; k < number; k++) {maxStatus[k] = (k == maxReturnProductIndex) ? maxReturnCost: (k == otherReturnProductIndex) ? otherReturnCost : 0;}}}}}return maxStatus;}
}
https://blog.csdn.net/weixin_52908342/article/details/135226203