이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
#define REP(x, a, b) for (int x = a; x < b; ++x)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A, n) { cout << #A << " = "; FOR(_, 1, n) cout << A[_] << " "; cout << endl; }
#define PR0(A, n) { cout << #A << " = "; REP(_, 0, n) cout << A[_] << " "; cout << endl; }
#define BitCount(x) __builtin_popcount(x)
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
const int M = 2e2 + 10;
const int INF = 2e9 + 10;
int n, m, ansSize = 0;
int a[N], ans[N];
int S[N];
int tr[M][N];
LL dp[2][N];
void Compute(int p, int l, int r, int u, int v) {
if (l > r) return;
int m = (l + r) >> 1;
int nxt = p & 1, pre = nxt ^ 1;
if (p < m) {
int x = max(u, p), y = min(m - 1, v), best = -1;
FOR(i, x, y)
if (dp[nxt][m] < dp[pre][i] + (LL)S[i] * (S[m] - S[i])) {
dp[nxt][m] = dp[pre][i] + (LL)S[i] * (S[m] - S[i]);
tr[p][m] = i;
best = i;
}
Compute(p, l, m - 1, u, best);
Compute(p, m + 1, r, best, v);
} else {
Compute(p, l, m - 1, u, v);
Compute(p, m + 1, r, u, v);
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // LOCAL
scanf("%d%d", &n, &m);
FOR(i, 1, n) scanf("%d", &a[i]);
FOR(i, 1, n) S[i] = S[i - 1] + a[i];
FOR(i, 1, n) dp[0][i] = 0;
FOR(i, 1, m) {
FOR(j, 0, n) dp[i & 1][j] = -INF;
Compute(i, 1, n, 1, n);
}
printf("%lld\n", dp[m & 1][n]);
while (tr[m][n] != 0) {
ans[++ansSize] = tr[m][n];
n = tr[m][n]; --m;
}
FOD(i, ansSize, 1) printf("%d ", ans[i]);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:52:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
^
sequence.cpp:53:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
FOR(i, 1, n) scanf("%d", &a[i]);
^
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |