【美团】放它一马
题目
思路
动态规划,当前的怪物,即第i个,取决于是否击败,和上一轮的击败数,即需要维护每一轮当前怪物对于不同(0-9)的已经击败怪物数量的状态,并且更新最大经验值。
代码
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);int n = in.nextInt();long[][] dp = new long[n+1][10];long ans = 0;long[] a = new long[n+1];for(int i = 1; i <= n; i ++) {a[i] = in.nextInt();}for(int i = 1; i <= n; i ++) {for(int j = 0; j <= 9 && j < i; j ++) {// 放走int beatNum1 = j;long score1 = dp[i-1][j] + i;// 不放走int beatNum2 = (j + 1)%10;long score2 = dp[i-1][j] + a[i] + a[i] * beatNum2;dp[i][beatNum1] = Math.max(dp[i][beatNum1], score1);dp[i][beatNum2] = Math.max(dp[i][beatNum2], score2);ans = Math.max(ans, dp[i][beatNum1]);ans = Math.max(ans, dp[i][beatNum2]);}}System.out.println(ans);}
}