先看 AC 代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int n, a[N], dp[N], ans[N];
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) ans[i] = a[i];
for (int i = 2; i <= n; i++)
for (int j = 1; j < i; j++)
if (a[i] >= a[j] && dp[i] <= dp[j]) dp[i] = dp[j] + 1, ans[i] = ans[j] + a[i];
for (int i = 1; i <= n; i++)
cout << ans[i] << " ";
return 0;
}
再看50 pts 代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int n, a[N], dp[N], ans[N];
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) ans[i] = a[i];
dp[1] = 1;
for (int i = 2; i <= n; i++)
for (int j = 1; j < i; j++)
if (a[i] >= a[j] && dp[i] <= dp[j]) dp[i] = dp[j] + 1, ans[i] = ans[j] + a[i];
for (int i = 1; i <= n; i++)
cout << ans[i] << " ";
return 0;
}
为什么 dp1 不能设初始值为 1?