[恢]hdu 1087
2011-12-21 11:49:05
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1087
题意:有n个point,每次只能往后跳,而且只能跳到分数比当前的大的point。score是路径上所有point的和。求最大score。
mark:dp。效率是O(n^2)。dp[i] = max{dp[j] + a[i]), 0<=j<=i-1。
代码:
# include <stdio.h>
int a[1010], dp[1010] ;
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 = 1 ; i <= n ;i++)
scanf ("%d", &a[i]) ;
ans = 0 ;
dp[0] = 0 ;
for (i = 1 ; i <= n ; i++)
{
dp[i] = 0 ;
for (j = 0 ; j < i ;j++ )
{
if (a[j] < a[i])
dp[i] = max(dp[i], dp[j] + a[i]) ;
}
if (dp[i] > ans) ans = dp[i] ;
}
printf ("%d\n", ans) ;
}
return 0 ;
}