CodeForces - 358D Dima and Hares

Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.

Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?

Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don’t have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.

Help Inna maximize the total joy the hares radiate. :)

Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 … an. The second line contains b1, b2, …, bn. The third line contains c1, c2, …, cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.

Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry.

Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare.

Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.

Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.

Example
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4

题意:喂兔子求能得到最大的joy值,其中每个兔子的joy值和他的邻居兔子有关系,也就是先喂他还是他的一个或者两个邻居得到的joy值是不一样的

解题思路:
http://blog.csdn.net/kevinkitty_love/article/details/14002835

dp[i][0] 表示i比i-1后喂
dp[i][1]表示i比i-1先喂

joy[0][i]是输入的第一行数据 表示i的邻居都是饿的他的joy值
joy[1][i]是输入的第二行数据 表示i的邻居有一个饱的他的joy值
joy[2][i]是输入的第三行数据 表示i的邻居都是饱的他的joy值

**dp[i][0]=max(dp[i-1][0]+joy[1][i-1],dp[i-1][1]+joy[0][i-1]);
dp[i][1]=max(dp[i-1][0]+joy[2][i-1],dp[i-1][1]+joy[1][i-1]);**

分析这三只兔子:(0表示没喂 1表示喂了 x表示将要喂他)
i-2 i-1 i

i后喂:i-1先喂,比较 i-2先喂(1 x 0)时i-1有一个邻居饱了的joy 和
i-1先喂(0 x 0)时i-1的邻居都饿着的joy

i先喂:i-1后喂,比较 i-2 先喂(1 x 1)时 i-1两个邻居都饱着的joy 和
i-1先喂(0 x 1)时i-1的邻居有一个饱的joy

AC代码:

#include
#include
#define INF 0x3f3f3f3f
using namespace std;

int dp[3010][2];//dp[i][0]表示hare i 比 i-1 后feed 情况下, 前i-1 个 hare 的最大收益
                //dp[i][1]表示hare i 比 i-1 先feed 情况下, 前i-1 个 hare 的最大收益
int joy[3][3010];

int main()
{
    int n;
    scanf("%d",&n);
    for(int j=0;j<3;j++)//0表示他的邻居都饿着1表示只要有一个饱的2表示他的邻居都饱着
        for(int i=0;i
            scanf("%d",&joy[j][i]);
    dp[0][0]=-INF;
    dp[0][1]=0;
    //dp[1][0]=cst[0][0];  
    //dp[1][1]=cst[1][0]; 
    for(int i=1;i<=n;i++)//i-2,i-1,i 
    {
        dp[i][0]=max(dp[i-1][0]+joy[1][i-1],dp[i-1][1]+joy[0][i-1]);//后喂i-1+邻居有一个饱 先喂i-1+邻居都饿着
        dp[i][1]=max(dp[i-1][0]+joy[2][i-1],dp[i-1][1]+joy[1][i-1]);//后喂i-1+邻居都饱着 先喂i-1+邻居有一个饱
    }
    printf("%d\n",dp[n][0]);
    return 0;
}

你可能感兴趣的:(ACM,DP一般看规律)