This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector <int>;
using VLL = vector <ll>;
int n, m;
const int N = 3e3 + 5;
const ll INF = 1e18;
ll ans = 1;
ll nums[N], pre[N], dp[N][N];
signed main () {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++) cin >> nums[i];
for (int i = 1; i <= n; i++) pre[i] += pre[i-1] + nums[i];
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) dp[i][j] = INF;
dp[1][1] = nums[1];
ll sum = 0;
int m = n+1;
for (int i = 1; i < n; i++)
for (int j = 1; j <= n; j++) {
dp[i][j] = min(dp[i][j], dp[i][j-1]+nums[j]);
ll tar = dp[i][j] + pre[j];
int id = lower_bound(pre, pre + m, tar) - pre;
if (id <= n && pre[id] >= tar) {
dp[i+1][id] = min(dp[i+1][id], pre[id] - pre[j]);
ans = i+1;
}
}
// for (int i = 1; i <= n; i++)
// for (int j = 1; j <= n; j++) cout << (dp[i][j] == INF ? -1 : dp[i][j]) << " \n"[j==n];
cout << ans << '\n';
}
Compilation message (stderr)
segments.cpp: In function 'int main()':
segments.cpp:23:5: warning: unused variable 'sum' [-Wunused-variable]
23 | ll sum = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |