ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Takahashi and Animals

2022-05-19 10:33:59  阅读:138  来源: 互联网

标签:fed Animals int long cat include dp Takahashi


Takahashi and Animals

https://atcoder.jp/contests/abc251/tasks/abc251_e

 

 

 

Solution

 

 

参考

https://blog.csdn.net/qq_52678569/article/details/124790849

https://blog.csdn.net/qq_34364611/article/details/124784187

 

Code

#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#include <deque>
#include <queue>
#include <string>
#include <set>
using namespace std;


/*
https://atcoder.jp/contests/abc251/tasks/abc251_e
*/

const int N = 300000 + 16;

int n;
int a[N] = {0};

long long dp[N][2] = {{0}};

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    /*
    dp[i][j] -- the cost to feed all top i cats
    but for i cat, it has two situations:
    dp[i][0] -- don't feed i cat with a[i] food cost
    dp[i][1] -- feed i cat with a[i] food cost

    generally speaking:
    dp[i][0] = dp[i-1][1]  // due to lack of feeding a[i] to i cat, then i-1 cat must be fed
    dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + a[i] // due to i cat fed with a[i], so i-1 cat can be fed or not

    but for i == 1, it depends on if n cat was fed with a[n] or not
    case 1
        if n cat was fed with a[n], then 1 cat can be fed or not
        dp[1][0] = 0
        dp[1][1] = a[1]
    case 2
        if n cat was not fed with a[n], then 1 cat should be fed only
        dp[1][0] = INF
        dp[1][1] = a[1]
    */

    long long ans = 1e18;

    // for case 1
    dp[1][0] = 0;
    dp[1][1] = a[1];

    for (int i = 2; i <= n; i++) {
        dp[i][0] = dp[i - 1][1];
        dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + a[i];
    }

    ans = min({ans, dp[n][1]});

    // for case 2
    dp[1][0] = 1e18;
    dp[1][1] = a[1];

    for (int i = 2; i <= n; i++) {
        dp[i][0] = dp[i - 1][1];
        dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + a[i];
    }

    ans = min({ans, dp[n][0]});

    cout << ans << endl;
}

 

标签:fed,Animals,int,long,cat,include,dp,Takahashi
来源: https://www.cnblogs.com/lightsong/p/16287537.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有