zoukankan      html  css  js  c++  java
  • (DP) codeforces 358D

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

    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

    题目大意:从1到n的位置取数,取数的得到值与周围的数有没有取过有关,所有数都要取,求最终得到的最大结果

    解题思路:dp题,转移方程如下

    dp[i][0]=max(dp[i-1][0]+b[i-1],dp[i-1][1]+c[i-1])

    dp[i][1]=max(dp[i-1][0]+a[i-1],dp[i-1][1]+b[i-1])

    a,b,c分别表示周围没有数,有一个数,有两个数取过的情况。

    dp[i][0]表示取i个位置时,i-1没取过的情况。(实际取数的情况,先i,再i-1)

    dp[i][1]表示取i个位置时,i-1取过的情况。(实际取数的情况,先i-1,再i)

    囧。。。窝就是个DP渣渣

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    using namespace std;
    int a[3005],b[3005],c[3005],dp[3005][2],n;
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&b[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&c[i]);
        if(n==1)
        {
            printf("%d
    ",a[1]);
            return 0;
        }
        dp[2][0]=b[1];
        dp[2][1]=a[1];
        for(int i=3;i<=n;i++)
        {
            dp[i][0]=max(dp[i-1][0]+b[i-1],dp[i-1][1]+c[i-1]);
            dp[i][1]=max(dp[i-1][0]+a[i-1],dp[i-1][1]+b[i-1]);
        }
        int ans=max(dp[n][0]+a[n],dp[n][1]+b[n]);
        printf("%d
    ",ans);
        return 0;
    }
    

      

  • 相关阅读:
    40 修改了机器名,portal重装后,还需要做什么?
    39 路径分析小练习
    38 空间查询小练习
    面向对象一些概念简述
    Js函数基本介绍
    JS中的变量和数据类型
    js基础语法
    Css样式优先级
    对响应式布局的理解和认知
    关于Flex布局
  • 原文地址:https://www.cnblogs.com/water-full/p/4510451.html
Copyright © 2011-2022 走看看