Atcoder C - Vacation ( DP )


C - Vacation


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100100 points

Problem Statement

Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.

The vacation consists of NN days. For each ii (1iN1≤i≤N), Taro will choose one of the following activities and do it on the ii-th day:

  • A: Swim in the sea. Gain aiai points of happiness.
  • B: Catch bugs in the mountains. Gain bibi points of happiness.
  • C: Do homework at home. Gain cici points of happiness.

As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.

Find the maximum possible total points of happiness that Taro gains.

Constraints

  • All values in input are integers.
  • 1N1051≤N≤105
  • 1ai,bi,ci1041≤ai,bi,ci≤104

Input

Input is given from Standard Input in the following format:

NN
a1a1 b1b1 c1c1
a2a2 b2b2 c2c2
::
aNaN bNbN cNcN

Output

Print the maximum possible total points of happiness that Taro gains.


Sample Input 1 Copy

Copy
3
10 40 70
20 50 80
30 60 90

Sample Output 1 Copy

Copy
210

If Taro does activities in the order C, B, C, he will gain 70+50+90=21070+50+90=210 points of happiness.


Sample Input 2 Copy

Copy
1
100 10 1

Sample Output 2 Copy

Copy
100

Sample Input 3 Copy

Copy
7
6 7 8
8 8 3
2 5 2
7 8 6
4 6 8
2 3 4
7 5 1

Sample Output 3 Copy

Copy
46

Taro should do activities in the order C, A, B, A, C, B, A

题意:裸的DP题意,题面单词很简单。

思路:下一个状态选上一个状态中的不和自己相同的那两个中的最大值。

转移方程可以见代码。

我的AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n;
ll a[maxn];
ll b[maxn];
ll c[maxn];
ll dp[maxn][5];

int main()
{
    gbtb;
    cin>>n;
    repd(i,1,n)
    {
        cin>>a[i]>>b[i]>>c[i];
    }
    dp[1][1]=a[1];
    dp[1][2]=b[1];
    dp[1][3]=c[1];
    repd(i,2,n)
    {
        dp[i][1]+=max(dp[i-1][2],dp[i-1][3])+a[i];
        dp[i][2]+=max(dp[i-1][1],dp[i-1][3])+b[i];
        dp[i][3]+=max(dp[i-1][1],dp[i-1][2])+c[i];
    }
    cout<<max(dp[n][3],max(dp[n][1],dp[n][2]));
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/qieqiemin/p/10251948.html