import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<SightseeingSpot> spots = new ArrayList<>();
spots.add(new SightseeingSpot("故宫", 1, 7));
spots.add(new SightseeingSpot("颐和园", 2, 8));
spots.add(new SightseeingSpot("长城", 3, 9));
spots.add(new SightseeingSpot("天坛", 1, 6));
int totalDays = 4;
Result result = optimizeTrip(spots, totalDays);
System.out.println("最优化的旅游行程:");
for (SightseeingSpot spot : result.selectedSpots) {
System.out.println(spot.name + " - 时间:" + spot.time + "天,评分:" + spot.score);
}
System.out.println("最高总分:" + result.totalScore);
}
public static class Result {
List<SightseeingSpot> selectedSpots;
int totalScore;
public Result(List<SightseeingSpot> selectedSpots, int totalScore) {
this.selectedSpots = selectedSpots;
this.totalScore = totalScore;
}
}
public static Result optimizeTrip(List<SightseeingSpot> spots, int totalDays) {
int n = spots.size();
int[][] dp = new int[n + 1][totalDays + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= totalDays; j++) {
if (spots.get(i - 1).time > j) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - spots.get(i - 1).time] + spots.get(i - 1).score);
}
}
}
List<SightseeingSpot> selectedSpots = new ArrayList<>();
int j = totalDays;
for (int i = n; i > 0; i--) {
if (dp[i][j] != dp[i - 1][j]) {
selectedSpots.add(spots.get(i - 1));
j -= spots.get(i - 1).time;
}
}
Collections.reverse(selectedSpots);
return new Result(selectedSpots, dp[n][totalDays]);
}
}
class SightseeingSpot {
String name;
int time;
int score;
public SightseeingSpot(String name, int time, int score) {
this.name = name;
this.time = time;
this.score = score;
}
}