이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXK = 210;
const int MAXN = 1e5 + 10;
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))
inline ll sqr (ll x) {
return x * x;
}
int N, K;
ll dp[2][MAXN]; //minimum of sum of squares.
ll P[MAXN];
void rec (ll *cdp, ll *pdp, int lt, int rt, int olt, int ort) {
if (lt > rt) {
return;
}
int mid = (lt + rt) / 2;
int oi = -1;
for (int i = olt; i <= ort && i < mid; i++) {
ll val = sqr(P[mid] - P[i]) + pdp[i];
if (cdp[mid] > val) {
cdp[mid] = val;
oi = i;
}
}
assert(oi != -1);
rec(cdp, pdp, lt, mid - 1, olt, oi);
rec(cdp, pdp, mid + 1, rt, oi, ort);
}
int main() {
scanf("%d %d", &N, &K);
K++;
for (int i = 1; i <= N; i++) {
scanf("%lld", &P[i]);
P[i] += P[i - 1];
}
int cur = 0, prv = 1;
fillchar(dp[cur], 63);
dp[cur][0] = 0;
for (int i = 1; i <= K; i++) {
swap(cur, prv);
fillchar(dp[cur], 63);
rec(dp[cur], dp[prv], i, N, i - 1, N - 1);
}
printf("%lld\n", (sqr(P[N]) - dp[cur][N]) / 2);
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:47:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &P[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... |