zoukankan      html  css  js  c++  java
  • Codeforces 358 D. Dima and Hares


    dp[i][0]表示i号兔子先于i-1号兔子喂食,dp[i][1]反过来.

    倒着DP

    D. Dima and Hares
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 iradiates 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.

    Sample test(s)
    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

    /**
     * Created by ckboss on 14-10-6.
     */
    import java.util.*;
    
    public class DimaandHares {
        public static void main(String[] args){
            Scanner in = new Scanner(System.in);
            int n=in.nextInt();
            int[] A=new int[n+10],B=new int[n+10],C=new int[n+10];
            int[][] dp = new int[n+10][2];
            for(int i=1;i<=n;i++) A[i]=in.nextInt();
            for(int i=1;i<=n;i++) B[i]=in.nextInt();
            for(int i=1;i<=n;i++) C[i]=in.nextInt();
            dp[n][0]=A[n]; dp[n][1]=B[n];
            for(int i=n-1;i>0;i--)
            {
                dp[i][0]=Math.max(dp[i][0],dp[i+1][0]+B[i]);
                dp[i][0]=Math.max(dp[i][0],dp[i+1][1]+A[i]);
                dp[i][1]=Math.max(dp[i][1],dp[i+1][1]+B[i]);
                dp[i][1]=Math.max(dp[i][1],dp[i+1][0]+C[i]);
            }
            System.out.println(dp[1][0]);
        }
    }
    



  • 相关阅读:
    Intellij Idea安装主题包
    Spring中bean的含义
    IntelliJ IDEA 中 右键运行时没有run;新建时,选项没有Java class的解决方法和具体解释
    Linux下修改profile后用户无法登陆的问题
    11 | 怎么给字符串字段加索引?
    09 | 普通索引和唯一索引,应该怎么选择?
    08 | 事务到底是隔离的还是不隔离的?
    07 | 行锁功过:怎么减少行锁对性能的影响?
    06 | 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
    05 | 深入浅出索引(下)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4296028.html
Copyright © 2011-2022 走看看