编辑代码

import java.util.ArrayList;
import java.util.List;

public class ItineraryOptimization {

    static class Spot {
        int time;
        int score;

        Spot(int time, int score) {
            this.time = time;
            this.score = score;
        }
    }

    public static List<Spot> optimizeItinerary(int[] times, int[] scores, int totalDays) {
        int numSpots = times.length;
        int[][] dp = new int[numSpots + 1][totalDays + 1];

        for (int i = 1; i <= numSpots; i++) {
            for (int j = 1; j <= totalDays; j++) {
                if (times[i - 1] <= j) {
                    dp[i][j] = Math.max(dp[i - 1][j], scores[i - 1] + dp[i - 1][j - times[i - 1]]);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }

        List<Spot> selectedSpots = new ArrayList<>();
        int i = numSpots;
        int j = totalDays;

        while (i > 0 && j > 0) {
            if (dp[i][j] != dp[i - 1][j]) {
                selectedSpots.add(new Spot(times[i - 1], scores[i - 1]));
                j -= times[i - 1];
            }
            i--;
        }

        return selectedSpots;
    }

    public static void main(String[] args) {
        // 景点所需时间和评分
        int[] times = {1, 2, 3, 1};
        int[] scores = {7, 8, 9, 6};
        // 总共的旅游天数
        int totalDays = 4;

        List<Spot> optimizedItinerary = optimizeItinerary(times, scores, totalDays);

        System.out.println("最优化的旅游行程:");
        for (Spot spot : optimizedItinerary) {
            System.out.println("景点时间:" + spot.time + "天,景点评分:" + spot.score);
        }
    }
}