您的位置:首页 > 其它

hdu1087

2016-01-15 00:00 183 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1087





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
;
}






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
;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: