标签:hdu1087
3 1 3 2 4 1 2 3 4 4 3 3 2 1 0
4 10 3
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); final int maxn = 1002; int n, i, j, ans, tmp; int[] dp = new int[maxn], arr = new int[maxn]; while(true){ n = cin.nextInt(); if(n == 0) break; ans = arr[0] = dp[0] = cin.nextInt(); for(i = 1; i < n; ++i){ arr[i] = dp[i] = cin.nextInt(); for(j = 0; j < i; ++j) if(arr[i] > arr[j]) dp[i] = max(dp[i], dp[j] + arr[i]); if(dp[i] > ans) ans = dp[i]; } System.out.println(ans); } } public static int max(int a, int b){ return a > b ? a : b; } }
#include <stdio.h> #define maxn 1002 //dp[i]表示以i结束的序列的最大值 int arr[maxn], dp[maxn]; int max(int a, int b){ return a > b ? a : b; } int main() { int n, i, j, ans; while(scanf("%d", &n), n){ for(i = 0; i < n; ++i) scanf("%d", arr + i); ans = dp[0] = max(0, arr[0]); for(i = 1; i < n; ++i){ dp[i] = max(0, arr[i]); //attention for(j = 0; j < i; ++j) if(arr[j] < arr[i]) dp[i] = max(dp[i], dp[j] + arr[i]); if(dp[i] > ans) ans = dp[i]; } printf("%d\n", ans); } return 0; }
HDU1087 Super Jumping! Jumping! Jumping! 【DP】
标签:hdu1087
原文地址:http://blog.csdn.net/chang_mu/article/details/38875401