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
    题意:要喂n只兔子,兔子的开心值受到相邻伙伴的影响,求n只兔子开心值的最大值。

             第二行:旁边兔子未吃饱;第三行:一只相邻的兔子吃饱;第四行:两只相邻的兔子都吃饱。

//状态转移;
//dp[i][j][k] i表示第几只兔子,j表示当喂这个兔子时,它旁边的兔子的状态,
// k表示是左还是右; 
// dp[i][0][0]  表示当喂第i只兔子时,两边兔子都是饥饿的 ; 
//dp[i][1][0]  表示当喂第i只兔子时,左边兔子已经喂过了; 
//dp[i][1][1] 表示当喂第i只兔子时,右边兔子已经喂过了 ; 

//dp[i][2][0] 表示当第i只兔子时,两边的兔子都已经喂过了;

 代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n;
int dp[3100][5][3];
int a[3][3100];
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0; i<3; i++)
            for(int j=1; j<=n; j++)
                scanf("%d",&a[i][j]);
        memset(dp,0,sizeof(dp));
        dp[1][0][0]=a[0][1]; //第一只兔子只有这两种状态
        dp[1][1][1]=a[1][1];
         for(int i=2;i<=n;i++)
        {
            dp[i][0][0]=max(dp[i-1][1][1]+a[0][i],dp[i-1][2][0]+a[0][i]);
            dp[i][1][0]=max(dp[i-1][1][0]+a[1][i],dp[i-1][0][0]+a[1][i]);
            dp[i][1][1]=max(dp[i-1][1][1]+a[1][i],dp[i-1][2][0]+a[1][i]);
            dp[i][2][0]=max(dp[i-1][1][0]+a[2][i],dp[i-1][0][0]+a[2][i]);
        }
        int ans;
        ans=max(dp[n][0][0],dp[n][1][0]);
        printf("%d\n",ans);
    }
    return 0;
}
//dp[i][0][0]=max(dp[i-1][1][1]+a[0][i],dp[i-1][2][0]+a[0][i]);  
//dp[i][1][0]=max(dp[i-1][1][0]+a[1][i],dp[i-1][0][0]+a[1][i]);  
//dp[i][1][1]=max(dp[i-1][1][1]+a[1][i],dp[i-1][2][0]+a[1][i]);  
//dp[i][2][0]=max(dp[i-1][1][0]+a[2][i],dp[i-1][0][0]+a[2][i]);  
//解释一下这些状态转移,以第一个为例;  
  
//dp[i][0][0]表示是当喂第i只兔子时,它两边的兔子都还没喂,看这个状态是由前一只兔子的那些状态转移过来的啊;   
//当喂前一只兔子时,这一只兔子一定喂过了,所以由前一只兔子的两种状态的一种转变过来,  
//那两种状态啊:一:当喂前一只兔子时,这一只兔子一定喂过了dp[i-1][1][1],二:当喂前一只兔子时,  
//前一只兔子两边的兔子都喂过了 dp[i-1][2][0];  
  
// 第二个:   
//dp[i][1][0],表示当喂这只兔子时,它左边已经喂,那么当喂它左边(前一个)兔子时,这一只一定没有喂  
// 所以可能由前一只兔子的两种状态转移过来,一:dp[i-1][1][0] 当喂前一只兔子时,前一只兔子左边的兔子已经喂过,  
// 二:dp[i-1][2][0] 表示当喂前一只兔子时,前一只兔子两边的兔子都没有喂;  
  
//第三个:  
//dp[i][1][1] 表示当喂这只兔子时,它右边的已经喂过了,而它前一个还没有喂,那么当喂前一只兔子时,这只一定喂过了   
// 所以由前一个兔子的两种状态,一:当喂前一只兔子时, 这只一定喂过了,dp[i-1][1][1]   
// 二:当喂前一只兔子时,可能两边的兔子都喂过了dp[i-1][2][0];  
  
// 第四个:
// dp[i][2][0]表示当喂第i只兔子时,它两边的兔子都喂过了,当喂它前一个时,这只兔子没有喂过;dp[i-1][1][0]表示
//前一只兔子的左边已喂过,dp[i-1][0][0]表示左右两边都没喂过。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值