poj 2385 Apple Catching 经典dp
2011-08-28 16:03
351 查看
这道题让我对于这种类型题有了一个比较深的理解!!!主要思路就是用跳的数目去更新dp[][]。
#include<iostream>
using namespace std;
int dp[50][1005];
int a[1005];
int main()
{
int t,w;
while(cin>>t>>w)
{
for(int i=1;i<=t;i++)
cin>>a[i];
memset(dp,0,sizeof(dp));
for(int i=1;i<=w+1;i++)//用每一个跳的数目去更新dp
{
for(int j=1;j<=t;j++)//
{
if(i&1)//这个是看了别人这么学!!自己才学的!!如果是偶数就是0,如果是奇数就是1
dp[i][j]=max(dp[i][j-1],dp[i-1][j-1])+(a[j]&1);//当时奇数时说明跳到2树了,那么当a[j]为2的时候加一,在使用&特别注意要加括号
else
dp[i][j]=max(dp[i][j-1],dp[i-1][j-1])+((a[j]&1)^1);//当为偶数时说明跳到1树,那么当a[j]为1时加一
}
}
cout<<dp[w+1][t]<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int dp[50][1005];
int a[1005];
int main()
{
int t,w;
while(cin>>t>>w)
{
for(int i=1;i<=t;i++)
cin>>a[i];
memset(dp,0,sizeof(dp));
for(int i=1;i<=w+1;i++)//用每一个跳的数目去更新dp
{
for(int j=1;j<=t;j++)//
{
if(i&1)//这个是看了别人这么学!!自己才学的!!如果是偶数就是0,如果是奇数就是1
dp[i][j]=max(dp[i][j-1],dp[i-1][j-1])+(a[j]&1);//当时奇数时说明跳到2树了,那么当a[j]为2的时候加一,在使用&特别注意要加括号
else
dp[i][j]=max(dp[i][j-1],dp[i-1][j-1])+((a[j]&1)^1);//当为偶数时说明跳到1树,那么当a[j]为1时加一
}
}
cout<<dp[w+1][t]<<endl;
}
return 0;
}
相关文章推荐
- POJ_2385_Apple Catching_简单DP
- POJ - 2385 Apple Catching (DP)
- DP:Apple Catching(POJ 2385)
- POJ 2385 Apple Catching (DP)
- POJ 2385 - Apple Catching [DP]
- Apple Catching (poj 2385 简单dp)
- POJ 2385 Apple Catching(DP)
- poj 2385 Apple Catching (DP)
- POJ 2385 Apple Catching【DP】
- POJ2385 Apple Catching(DP/递推)
- POJ 2385 Apple Catching(DP)
- POJ 2385 Apple Catching(线性DP)
- poj 2385 Apple Catching 基础dp
- POJ2385-Apple Catching-简单dp
- POJ 2385-Apple Catching (基础DP)
- POJ2385 Apple Catching (dp)
- POJ 2385 Apple Catching(基础DP)
- poj 2385Apple Catching(简单dp)
- POJ 2385 Apple Catching 接苹果 DP
- POJ 2385 Apple Catching [DP]