This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#ifdef DEBUG
#include "debug.hpp"
#endif
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define traverse(c, it) for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N) for(int i = 0; i < (N); ++i)
#define rrep(i, N) for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N) for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e) for(int i = (s); i <= (e); ++i)
#ifdef DEBUG
#define debug(x...) { \
++dbg::depth; \
string dbg_vals = dbg::to_string(x); \
--dbg::depth; \
dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
}
#define light_debug(x) { \
dbg::light = true; \
dbg::dout << __func__ << ":" << __LINE__; \
dbg::dout << " " << #x << " = " << x << endl; \
dbg::light = false; \
}
#else
#define debug(x...) 42
#define light_debug(x) 42
#endif
using ll = long long;
template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }
template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }
using point = complex<long long>;
namespace CHT {
deque<point> v;
deque<int> arg;
long long dot(point p, point q) {
return (conj(p) * q).real();
}
long long cross(point p, point q) {
return (conj(p) * q).imag();
}
void clear() {
v.clear(), arg.clear();
}
// assumes p.real is increasing.
void add_line(point p, int i) {
while(v.size() > 1) {
point a{v[int(v.size()) - 1]}, b{v[int(v.size()) - 2]};
if(cross(b - a, p - a) <= 0) v.pop_back(), arg.pop_back();
else break;
}
v.push_back(p); arg.push_back(i);
}
// assumes x is increasing.
pair<long long, int> get_best(long long x) {
point p(x, 1);
while(v.size() > 1 && dot(p, v[0]) <= dot(p, v[1]))
v.pop_front(), arg.pop_front();
return {dot(p, v[0]), arg[0]};
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, K; cin >> n >> K;
vector<long long> p(n); rep(i, n) cin >> p[i]; partial_sum(all(p), p.begin());
vector<long long> dp(n), dp_old(n);
rep(i, n) dp_old[i] = p[i] * p[i];
vector<vector<int>> pre(n, vector<int>(K));
rep1(k, K) {
CHT::clear();
rep2(i, k, n - 1) {
CHT::add_line(point(2 * p[i-1], -p[i-1] * p[i-1] - dp_old[i-1]), i-1);
auto [v, j] = CHT::get_best(p[i]);
dp[i] = p[i] * p[i] - v;
pre[i][k - 1] = j;
}
debug(dp_old);
dp_old = dp;
}
debug(dp_old);
cout << (p.back() * p.back() - dp.back()) / 2 << '\n';
int i{n - 1};
rrep(k, K) {
i = pre[i][k];
cout << i + 1 << ' ';
}
cout << '\n';
#ifdef DEBUG
dbg::dout << "\nExecution time: "
<< clock() * 1000 / CLOCKS_PER_SEC
<< "ms" << endl;
#endif
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:33:29: warning: statement has no effect [-Wunused-value]
33 | #define debug(x...) 42
| ^~
sequence.cpp:101:9: note: in expansion of macro 'debug'
101 | debug(dp_old);
| ^~~~~
sequence.cpp:33:29: warning: statement has no effect [-Wunused-value]
33 | #define debug(x...) 42
| ^~
sequence.cpp:104:5: note: in expansion of macro 'debug'
104 | debug(dp_old);
| ^~~~~
# | 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... |