华为OD机考-用户调度问题-DP(JAVA 2025B卷)
import java.util.Scanner;public class UserScheduling {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt(); // 用户个数int[][] costs = new int[n][3]; // 存储每个用户使用A/B/C策略的系统消耗for (int i = 0; i < n; i++) {costs[i][0] = scanner.nextInt(); // A策略消耗costs[i][1] = scanner.nextInt(); // B策略消耗costs[i][2] = scanner.nextInt(); // C策略消耗}// 初始化dp数组int[][] dp = new int[n + 1][3];for (int i = 1; i <= 3; i++) {dp[1][i - 1] = costs[0][i - 1];}// 动态规划求解for (int i = 2; i <= n; i++) {for (int j = 0; j < 3; j++) {int minCost = Integer.MAX_VALUE;for (int k = 0; k < 3; k++) {if (k != j) {minCost = Math.min(minCost, dp[i - 1][k]);}}dp[i][j] = minCost + costs[i - 1][j];}}// 找到最优策略组合下的总的系统资源消耗数int minTotalCost = Integer.MAX_VALUE;for (int i = 0; i < 3; i++) {minTotalCost = Math.min(minTotalCost, dp[n][i]);}System.out.println(minTotalCost);}
}