题解 | #不相邻取数#
不相邻取数
http://www.nowcoder.com/practice/a2be806a0e5747a088670f5dc62cfa1e
动态规划的简单运用
#include<iostream>
using namespace std;
int num[200001] = {0},dp[200001];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,ans = 0;
cin >> n;
for(int i = 0;i < n;++i){
cin >> num[i];
dp[i] = num[i];
}
for(int i = 2;i < n;++i){
if(i - 3 < 0)
dp[i] = dp[i - 2] + num[i];
else
dp[i] = max(dp[i - 2],dp[i - 3]) + num[i];
}
for(int i = 0;i < n;++i)
ans = max(ans,dp[i]);
cout << ans;
}