이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
#define N 100005
#define K 202
int n, k; ll cum[N];
ll curc[N], curm[N], curi[N], curlen, advc[N], advm[N], advi[N], advlen, dp;
int tr[K][N], ans[K];
void ins(ll c, ll m, ll i) {
if (advlen && m == advm[advlen-1]) {
if (c > advc[advlen-1]) advlen--;
else return;
}
while (advlen >= 2 && (c - advc[advlen-2])/(advm[advlen-2] - m) > (c - advc[advlen-1])/(advm[advlen-1] - m)) advlen--;
advc[advlen] = c; advm[advlen] = m; advi[advlen++] = i;
}
void swp() {
swap(curc, advc); swap(curm, advm); swap(curi, advi); swap(curlen, advlen);
advlen = 0;
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%lld", cum+i);
for (int i = 1; i <= n; i++) cum[i] += cum[i-1];
for (int i = 1; i <= n; i++) ins(-(cum[i]*cum[i]), cum[i], i);
swp();
for (int lay = 1; lay <= k; lay++) {
int hllpnt = 0;
for (int i = lay+1; i <= n; i++) {
ll qx = cum[i];
while (hllpnt+1 < curlen && curi[hllpnt+1] < i && curm[hllpnt]*qx + curc[hllpnt] <= curm[hllpnt+1]*qx + curc[hllpnt+1]) hllpnt++;
dp = curm[hllpnt]*qx + curc[hllpnt];
tr[lay][i] = curi[hllpnt];
if (i < n) ins(dp - cum[i]*cum[i], cum[i], i);
}
swp();
}
printf("%lld\n", dp);
int trn = n, trk = k;
while (trk) {
trn = tr[trk][trn];
trk--;
ans[trk] = trn;
}
for (int i = 0; i < k; i++) printf("%d%c", ans[i], i==k-1 ? '\n' : ' ');
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:25: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:26:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= n; i++) scanf("%lld", cum+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... |