이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define long long long
using namespace std;
const int N = 1e5+5;
struct line {
long m, b;
int idx;
line() { }
line(long m, long b, int idx) : m(m), b(b), idx(idx) { }
long f(long x) { return m * x + b; }
};
struct cht {
int ptr = 0, sz = 0;
line l[N];
bool bad(line a, line b, line c) { return (c.b - a.b) * (a.m - b.m) <= (b.b - a.b) * (a.m - c.m); }
void add(long m, long b, int idx) {
line now(m, b, idx);
while(sz > 1 && bad(l[sz-2], l[sz-1], now)) --sz;
l[++sz] = now;
}
line query(long x) {
if(ptr >= sz) ptr = sz - 1;
while(ptr + 1 < sz && l[ptr].f(x) < l[ptr+1].f(x)) ++ptr;
return l[ptr];
}
void clear() { sz = 0, ptr = 0; }
} hull[2];
int n, k, pv[N][205];
long dp[N], pref[N];
int main() {
scanf("%d %d", &n, &k), ++k;
for(int i = 1; i <= n; i++) scanf("%lld", pref+i);
for(int i = 1; i <= n; i++) pref[i] += pref[i-1];
hull[0].add(0, 0, 0);
for(int j = 1; j <= k; j++) {
int now = j & 1, pre = now ^ 1;
hull[now].clear();
for(int i = j; i <= n; i++) {
line ret = hull[pre].query(pref[i]);
dp[i] = ret.f(pref[i]);
pv[i][j] = ret.idx;
hull[now].add(pref[i], dp[i] - pref[i] * pref[i], i);
}
}
vector<int> ans;
for(int j = k, i = n; j > 1; j--) ans.emplace_back(i = pv[i][j]);
reverse(ans.begin(), ans.end());
printf("%lld\n", dp[n]);
for(int i : ans) printf("%d ", i);
printf("\n");
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:38:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k), ++k;
~~~~~~~~~~~~~~~~~~~~~~^~~~~
sequence.cpp:39:38: 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", pref+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... |