#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 40) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T>
bool chmin(T& a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
};
template <typename T>
bool chmax(T& a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
};
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
for(int i = 0; i < v.size(); i++) {
os << v[i] << (i + 1 == v.size() ? "\n" : " ");
}
return os;
}
template <typename T>
vector<T> make_v(size_t a) {
return vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) {
for(auto& e : t) {
fill_v(e, v);
}
};
struct CHT {
vector<pair<P, int>> lines;
bool check(pair<P, int> l1, pair<P, int> l2, pair<P, int> l3) {
return (l2.first.second - l3.first.second) *
(l2.first.first - l1.first.first) <=
(l1.first.second - l2.first.second) *
(l3.first.first - l2.first.first);
}
void add(ll a, ll b, int idx) {
pair<P, int> l = make_pair(P(a, b), idx);
while(lines.size() >= 2 &&
check(lines[(int)lines.size() - 2], lines.back(), l)) {
lines.pop_back();
}
lines.push_back(l);
}
int id = 0;
ll f(ll i, ll x) {
return lines[i].first.first * x + lines[i].first.second;
}
P query(ll x) {
int nid = id;
for(int i = id + 1; i < lines.size(); i++) {
if(f(i, x) > f(i - 1, x)) {
nid = i;
} else {
break;
}
}
id = nid;
if(id >= lines.size()) {
return P(0, 0);
}
return P(f(id, x), lines[id].second);
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<ll> s(n + 10);
for(int i = 1; i <= n; i++) {
ll a;
cin >> a;
s[i] = s[i - 1] + a;
}
vector<ll> res(n + 10);
vector<vector<int>> id(k + 1, vector<int>(n + 1));
for(int l = 0; l < k; l++) {
vector<ll> dp(n + 10);
CHT cht;
for(int i = l + 1; i <= n; i++) {
tie(dp[i], id[l][i]) = cht.query(s[i]);
cht.add(s[i], -s[i] * s[i] + res[i], i);
}
res = dp;
}
cout << res[n] << endl;
vector<int> v;
int idx = n;
for(int l = k - 1; l >= 0; l--) {
v.push_back(id[l][idx]);
idx = id[l][idx];
}
reverse(all(v));
for(int i = 0; i < k; i++) {
cout << v[i];
if(i < k - 1) {
cout << " ";
} else {
cout << endl;
}
}
}
Compilation message
sequence.cpp: In member function 'P CHT::query(ll)':
sequence.cpp:73:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = id + 1; i < lines.size(); i++) {
~~^~~~~~~~~~~~~~
sequence.cpp:81:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(id >= lines.size()) {
~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
2 ms |
256 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
2 ms |
428 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
2 ms |
256 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
2 ms |
256 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
3 ms |
512 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
3 ms |
512 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
2 ms |
512 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
3 ms |
512 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
8 ms |
384 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
3 ms |
512 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
8 ms |
1280 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
8 ms |
1152 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
6 ms |
1024 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
8 ms |
1280 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
6 ms |
1280 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
4 ms |
640 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
4 ms |
768 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1180 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
4 ms |
1120 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
99 ms |
9144 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
6 ms |
1448 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
50 ms |
5760 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
51 ms |
6496 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
71 ms |
7432 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
57 ms |
6072 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
71 ms |
6808 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
91 ms |
8340 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
9220 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
32 ms |
9196 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
1340 ms |
87508 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
40 ms |
9764 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Correct |
1389 ms |
86920 KB |
contestant found the optimal answer: 36183271951 == 36183271951 |
6 |
Correct |
441 ms |
61300 KB |
contestant found the optimal answer: 51629847150471 == 51629847150471 |
7 |
Correct |
1068 ms |
67636 KB |
contestant found the optimal answer: 124074747024496432 == 124074747024496432 |
8 |
Correct |
722 ms |
56596 KB |
contestant found the optimal answer: 309959349080800 == 309959349080800 |
9 |
Correct |
929 ms |
63544 KB |
contestant found the optimal answer: 124113525649823701 == 124113525649823701 |
10 |
Correct |
1260 ms |
79204 KB |
contestant found the optimal answer: 124309619349406845 == 124309619349406845 |