hdu1087

http://acm.hdu.edu.cn/showproblem.php?pid=1087

ContractedBlock.gif ExpandedBlockStart.gif View Code

   
// dp问题:
// 状态转移方程 b[i]=max(b[i], b[j]+a[i]);
#include " iostream "
using namespace std;
#define M 1010
int n;
int a[M],b[M];
int i,j;
int main()
{
while (cin >> n,n)
{
int Max =- 0xffff ;
memset(b,
0 , sizeof ( 0 ));
for (i = 0 ;i < n;i ++ )
cin
>> a[i];

b[
0 ] = a[ 0 ];
for (i = 1 ;i < n;i ++ )
{
b[i]
= a[i];
for (j = 0 ;j < i;j ++ )
if (a[j] < a[i] && b[i] < b[j] + a[i])
b[i]
= b[j] + a[i];
if (b[i] > Max) Max = b[i];
}
cout
<< Max << endl;
}
return 0 ;
}
ContractedBlock.gif ExpandedBlockStart.gif dfs(tle)

   
#include " iostream "
using namespace std;
int a[ 1010 ];
int n,sum;
int p = 0 ;
void dfs( int x, int y)
{
while (y <= x && x < n) y ++ ;

if (a[x] < a[y] && y < n && x < n)
{
sum
+= a[y];
x
= y;
if (x < n - 1 && y < n - 1 ) dfs(x,y);
}
else if (y < n - 1 ){ y ++ ; if (y < n - 1 ) dfs(x,y);}
}
int main()
{
int i,j;
while (cin >> n,n)
{
for (i = 0 ;i < n;i ++ ) cin >> a[i];
int Max =- 1 ;
sum
= 0 ;
for (i = 0 ;i < n;i ++ )
{
sum
= a[i];
if (Max < sum) Max = sum;
for (j = i + 1 ; j < n; j ++ )
{
p
= 0 ;
if (a[i] < a[j])
{
sum
+= a[j];
dfs(j,p);
}
if (sum > Max) Max = sum;
sum
= a[i];
}
}
cout
<< Max << endl;
}
return 0 ;
}

转载于:https://www.cnblogs.com/FCWORLD/archive/2011/03/23/1992089.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页