이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ll dp[2][100005], ps[100005];
int p[205][100005];
const ll eps = 1e-9;
struct line {
ll idx, m, c;
ll cal (ll x) {return m*x + c;}
ld sectx (line l) {return (ld) (l.c-c) / (m-l.m);}
};
int main () {
int n, k, a;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a, ps[i] = ps[i-1] + a;
deque<line> d;
for (int j = 1; j <= k; j++) {
d.push_back({0, 0, 0});
for (int i = 1; i <= n; i++) {
while ((int) d.size() >= 2 && d.end()[-2].cal(ps[i]) + eps > d.back().cal(ps[i])) d.pop_back();
dp[j&1][i] = d.back().cal(ps[i]) + ps[i]*ps[n] - ps[i]*ps[i];
p[j][i] = d.back().idx;
line now = {i, ps[i], dp[~j&1][i] - ps[i]*ps[n]};
while ((int) d.size() >= 2 && d[0].sectx(d[1]) + eps > now.sectx(d[0])) d.pop_front();
d.push_front(now);
}
d.clear();
}
auto iter = max_element(dp[k&1]+1, dp[k&1]+n+1);
cout << *iter << "\n";
vector<int> path {iter-dp[k&1]};
for (int i = k; i > 0; i--) path.push_back(p[i][path.back()]);
for (int i = k-1; i >= 0; i--) cout << path[i] << " ";
cout << "\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:36:24: warning: narrowing conversion of '((iter - ((ll*)(& dp[(k & 1)]))) <unknown operator> 8)' from 'long int' to 'int' [-Wnarrowing]
36 | vector<int> path {iter-dp[k&1]};
| ~~~~^~~~~~~~
sequence.cpp:36:24: warning: narrowing conversion of '((iter - ((ll*)(& dp[(k & 1)]))) <unknown operator> 8)' from 'long int' to 'int' [-Wnarrowing]
# | 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... |