1 条题解
-
0
一定要特判啊,否则就会像上面的 Python 代码一样,在 n = 1, 2 的时候被卡掉,只有 90 分😭
c++ AC 代码:
/* from math import sqrt,ceil,gcd,log;re=lambda:map(int,input().strip().split()) n, = re() a = list(re()) dp = [0] * n dp[0] = a[0] dp[1] = a[0] + a[1] for i in range(2, n): dp[i] = a[i] + max(dp[i-1], dp[i-2]) print(dp[-1]) */ #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n), dp(n); for (auto &i : a) cin >> i; if (n == 1) { cout << a[0]; } else if (n == 2) { cout << max(a[0], a[0] + a[1]); } else { dp[0] = a[0]; dp[1] = a[0] + a[1]; for (int i = 2; i < n; ++i) { dp[i] = a[i] + max(dp[i - 1], dp[i - 2]); } cout << dp.back(); } return 0; }
- 1
信息
- ID
- 443
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 10
- 标签
- (无)
- 递交数
- 4
- 已通过
- 1
- 上传者