题目传送门
我被坑了,再次纪念一下这道题。
AC code:
#include<bits/stdc++.h>
// #include<iostream>
// #include<cstdio>
// #include<vector>
// #include<cstring>
using namespace std;
#define per(i,a,b) for(int i = (a);i <= (b);++i)
#define rep(i,a,b) for(int i = (a);i >= (b);--i)
const int maxn = 2e5 + 10;
#define INF 0x3f3f3f
typedef long long LL;
int n = 0,m = 0;
LL dp[maxn][2];
LL a[maxn],b[maxn];
void solve(){
per(i,1,n){
dp[i][0] = max(dp[i-1][1],dp[i-2][1]) + a[i];
dp[i][1] = max(dp[i-1][0],dp[i-2][0]) + b[i];
}
printf("%I64d\n",max(dp[n][0],dp[n][1]));
}
int main(){
while(~scanf("%d",&n)){
memset(dp,0,sizeof(dp));
per(i,1,n){
scanf("%I64d",&a[i]);
}
per(i,1,n){
scanf("%I64d",&b[i]);
}
solve();
}
return 0;
}
本文分享了一道经典的动态规划问题及其解题思路。通过递推公式更新状态数组,求解两个序列的最大收益。代码采用C++实现,展示了清晰的状态转移过程。

被折叠的 条评论
为什么被折叠?



