求最长子序列的和。
dp[i] = max(dp[i] , dp[j] + xx[i])。
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); if (n==0) { break; } int[] xx = new int[n]; for (int i = 0; i < xx.length; i++) { int sr = sc.nextInt(); if (sr<0) { xx[i] = 0; }else { xx[i] = sr; } } int[] dp = new int[n]; for (int i = 0; i < dp.length; i++) { dp[i] = xx[i]; } for (int i = 0; i < dp.length; i++) { for (int j = 0; j < i; j++) { if (xx[i] > xx[j]) { if (dp[i] < dp[j]+xx[i]) { dp[i] = dp[j]+xx[i]; } } } } /* for (int i = 0; i < dp.length; i++) { System.out.print(dp[i]); System.out.print(" "); }*/ Arrays.sort(dp); System.out.println(dp[n-1]); } sc.close(); } }
标签:int,System,hdu1087,xx,sc,动态,规划,dp,out From: https://www.cnblogs.com/xiaohuangTX/p/18238783