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>
#define fi first
#define se second
#define all(x) x.begin(), x.begin()
using namespace std;
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b;} else return 0; return 1;
}
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b;} else return 0; return 1;
}
const int MOD = 1e9 + 7;
const long long INF = 1e18;
const int oo = 1e9;
const int N = 107;
int dp2[N][N][N], a[N], n;
long long pref[N];
bool visited2[N][N][N];
int backtrack2(int i, int lastStart, int lastEnd, int seg) {
if (i > n) {
if (pref[n] - pref[lastEnd] >= pref[lastEnd] - pref[lastStart - 1]) return seg;
return -1;
}
if (visited2[i][lastStart][lastEnd]) return dp2[i][lastStart][lastEnd];
visited2[i][lastStart][lastEnd] = 1;
int res = 0;
if (i == 1) {
maxi(res, backtrack2(i + 1, 1, 1, seg + 1));
maxi(res, backtrack2(i + 1, 0, 0, seg));
} else {
if (lastEnd == lastStart) {
if (pref[i] - pref[lastEnd] >= a[lastEnd]) {
maxi(res, backtrack2(i + 1, lastEnd + 1, i, seg + 1));
maxi(res, backtrack2(i + 1, lastStart, lastEnd, seg));
} else maxi(res, backtrack2(i + 1, lastStart, lastEnd, seg));
} else {
if (pref[i] - pref[lastEnd] >= pref[lastEnd] - pref[lastStart - 1]) {
maxi(res, backtrack2(i + 1, lastEnd + 1, i, seg + 1));
maxi(res, backtrack2(i + 1, lastStart, lastEnd, seg));
} else maxi(res, backtrack2(i + 1, lastStart, lastEnd, seg));
}
}
return dp2[i][lastStart][lastEnd] = res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i], pref[i] = pref[i - 1] + a[i];
cout << backtrack2(1, 0, 0, 1);
}
# | 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... |