#include
<
iostream
>
using
namespace
std;
int
n,m,x,f[
1000
][
31
];
int
main()
{
scanf(
"
%d%d
"
,
&
n,
&
m);
memset(f,
0
,
sizeof
(f));
for
(
int
i
=
1
;i
<=
n;
++
i){
scanf(
"
%d
"
,
&
x);
--
x;
f[i][
0
]
=
f[i
-
1
][
0
]
+
(x
==
0
?
1
:
0
);
for
(
int
j
=
1
;j
<=
m;
++
j)
f[i][j]
=
max(f[i
-
1
][j],f[i
-
1
][j
-
1
])
+
(j
%
2
==
x
?
1
:
0
);
//
j%2==x 表示j次移动后奶牛是在那棵数下。能否接到第i可以树。
}
int
ans
=
0
;
for
(
int
i
=
1
;i
<=
m;
++
i)
if
(f[n][i]
>
ans) ans
=
f[n][i];
printf(
"
%d\n
"
,ans);
return
0
;
}
滚动数组:
#include
<
iostream
>
#define
max(a,b)(a>b?a:b)
using
namespace
std;
int
main()
{
int
n,m,x,f[
2
][
31
];
scanf(
"
%d%d
"
,
&
n,
&
m);
memset(f,
0
,
sizeof
(f));
for
(
int
i
=
1
;i
<=
n;
++
i){
scanf(
"
%d
"
,
&
x);
--
x;
f[i
&
1
][
0
]
=
f[
1
-
(i
&
1
)][
0
]
+
(x
==
0
?
1
:
0
);
for
(
int
j
=
1
;j
<=
m;
++
j)
f[i
&
1
][j]
=
max(f[
1
-
(i
&
1
)][j],f[
1
-
(i
&
1
)][j
-
1
])
+
(j
%
2
==
x
?
1
:
0
);
}
x
=
0
;
for
(
int
i
=
0
;i
<=
m;
++
i)
if
(f[n
&
1
][i]
>
x) x
=
f[n
&
1
][i];
printf(
"
%d\n
"
,x);
return
0
;
}
posted on 2009-04-18 14:45
xfstart07 阅读(114)
评论(0) 编辑 收藏 引用 所属分类:
代码库