示例1:
输入:
5 100 10
10 20 30 40 50
3 4 5 6 10
20 30 20 40 30
输出:
0 30 0 40 0
示例2:
输入:
5 100 10
10 20 30 40 50
3 4 5 6 5
20 30 20 40 40
输出:
0 30 0 0 40
示例3:
输入:
5 100 10
10 20 30 40 50
3 4 5 6 10
20 30 20 40 70
输出:
0 0 0 0 70
示例4:
输入:
5 100 10
10 20 30 40 50
3 4 5 6 10
30 71 71 71 60
输出:
0 29 0 71 0
方式1:
import java.util.Scanner;
import java.util.StringJoiner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int Num = in.nextInt();
int money = in.nextInt();
int expectRisk = in.nextInt();
int[] rates = new int[Num];
for (int i = 0; i < Num; i++) {
rates[i] = in.nextInt();
}
int[] risks = new int[Num];
for (int i = 0; i < Num; i++) {
risks[i] = in.nextInt();
}
int[] quotas = new int[Num];
for (int i = 0; i < Num; i++) {
quotas[i] = in.nextInt();
}
// 拓展数组长度+1,最后一个位置处理只选择一个产品的情况
int[] extendRates = new int[Num + 1];
int[] extendRisks = new int[Num + 1];
int[] extendQuotas = new int[Num + 1];
System.arraycopy(rates, 0, extendRates, 0, Num);
System.arraycopy(risks, 0, extendRisks, 0, Num);
System.arraycopy(quotas, 0, extendQuotas, 0, Num);
// 最后一个位置时,均为0
extendRates[Num] = 0;
extendRisks[Num] = 0;
extendQuotas[Num] = 0;
int maxRevenue = 0;
int[] result = new int[Num];
for (int i = 0; i < Num + 1; i++) {
for (int j = i + 1; j < Num + 1; j++) {
if (extendRisks[i] + extendRisks[j] <= expectRisk) {
int maxIndex = extendRates[i] > extendRates[j] ? i : j;
int otherIndex = i + j - maxIndex;
int maxQuotas = Math.min(money, extendQuotas[maxIndex]);
int otherQuotas = Math.min(money - maxQuotas, extendQuotas[otherIndex]);
int revenue = extendRates[maxIndex] * maxQuotas + extendRates[otherIndex] * otherQuotas;
if (revenue > maxRevenue) {
maxRevenue = revenue;
for (int k = 0; k < Num; k++) {
result[k] = k == maxIndex ? maxQuotas : k == otherIndex ? otherQuotas : 0;
}
}
}
}
}
StringJoiner stringJoiner = new StringJoiner(" ");
for (int i = 0; i < Num; i++) {
stringJoiner.add(String.valueOf(result[i]));
}
System.out.println(stringJoiner.toString());
}
}
方式2:
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