이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//I love armpit fetish
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << '\n';
#define BP() cerr << "OK!\n";
#define PR(A, n) {cerr << #A << " = "; for (int _=1; _<=n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define PR0(A, n) {cerr << #A << " = "; for (int _=0; _<n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define FILE_NAME "data"
typedef pair<int64_t, int64_t> line;
const int MAX_N = 100007;
const int MAX_K = 207;
const int64_t INF = 1e18;
int n, k, a[MAX_N];
int64_t ps[MAX_N], f[MAX_N][MAX_K];
struct convex_hull {
vector<line> d;
int head;
void reset() {
d.clear();
head = 0;
}
convex_hull() {
reset();
}
bool bad(line l1, line l2, line l3) {
return (l1.second - l3.second) * (l2.first - l1.first)
<= (l1.second - l2.second) * (l3.first - l1.first);
}
void add_line(int64_t a, int64_t b) {
d.push_back(line(a, b));
while (d.size()>2 && bad(d[d.size()-3], d[d.size()-2], d.back()))
d.erase(d.end() - 2);
}
int64_t get(int64_t x) {
if (d.size()==0)
return -INF;
head = min(head, (int)d.size()-1);
while (head+1<d.size() && d[head].first * x + d[head].second
< d[head+1].first * x + d[head+1].second)
++head;
return d[head].first * x + d[head].second;
}
};
void enter() {
cin >> n >> k;
++k;
for (int i=1; i<=n; ++i) {
cin >> a[i];
ps[i] = ps[i-1] + a[i];
}
}
void trace(int pos, int nPart) {
if (pos==0)
return;
for (int i=pos-1; i>=0; --i) {
if (f[pos][nPart] == f[i][nPart-1] + ps[i] * (ps[pos] - ps[i])) {
trace(i, nPart-1);
break;
}
}
if (pos!=n) {
cout << pos << ' ';
}
}
void dp() {
f[0][0] = 0;
convex_hull CH;
for (int i=1; i<=k; ++i)
f[0][i] = -INF;
for (int i=1; i<=n; ++i)
f[i][0] = -INF;
for (int j=1; j<=k; ++j) {
CH.reset();
if (f[0][j-1]!=-INF)
CH.add_line(ps[0], f[0][j-1] - 1LL * ps[0] * ps[0]);
for (int i=1; i<=n; ++i) {
f[i][j] = max(-INF, CH.get(ps[i]));
// if (i==6 && j==7) {
// cerr << ps[i] << ' ' << f[i][j-1] - 1LL * ps[i] * ps[i] << '\n';
// }
// if (i==7 && j==7) {
// //cerr << CH.d[4].first << ' ' << CH.d[4].second << '\n';
// debug(CH.d[4].first * ps[7] + CH.d[4].second);
// debug(CH.d[5].first * ps[7] + CH.d[5].second);
// }
if (f[i][j-1]!=-INF)
CH.add_line(ps[i], f[i][j-1] - 1LL * ps[i] * ps[i]);
}
}
cout << f[n][k] << '\n';
trace(n, k);
}
int main() {
#ifdef OFFLINE_JUDGE
freopen(FILE_NAME".inp", "r", stdin);
freopen(FILE_NAME".out", "w", stdout);
#endif
ios::sync_with_stdio(0); cin.tie(0);
enter();
dp();
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In member function 'int64_t convex_hull::get(int64_t)':
sequence.cpp:49:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (head+1<d.size() && d[head].first * x + d[head].second
~~~~~~^~~~~~~~~
# | 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... |