Takahashi and Animals

Takahashi and Animals

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;}

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部