#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
template <bool kGetMin = false>
struct LineDeque
{
using T = long long;
struct Line
{
mutable T a, b, p;
bool operator<(const Line &other) const { return a < other.a; }
bool operator<(T x) const { return p < x; }
T eval(T x) const { return a * x + b; }
};
using Set = std::deque<pair<Line, int>>;
using Iterator = typename Set::iterator;
Set s;
static const T inf = std::numeric_limits<T>::max();
// doubles: inf = 1/.0, div(a, b) = a / b
T div(T a, T b)
{ // floored division
return a / b - ((a ^ b) < 0 && a % b);
}
bool intersect(Line &x, Line &y)
{
if (x.a == y.a)
{
x.p = x.b > y.b ? inf : -inf;
}
else
{
x.p = div(y.b - x.b, x.a - y.a);
}
return x.p >= y.p;
}
void _add_line1(T a, T b, int id)
{ // increasing slope
Line to_add = {a, b, inf};
if (s.size())
{
intersect(s.back().first, to_add);
}
while (s.size() >= 2 && intersect(s.at(s.size() - 2).first, s.at(s.size() - 1).first))
{
s.pop_back();
intersect(s.back().first, to_add);
}
s.push_back({to_add, id});
}
void _add_line2(T a, T b, int id)
{ // decreasing slope
Line to_add = {a, b, 0};
while (s.size() && intersect(to_add, s.front()))
{
s.pop_front();
}
s.push_front({to_add, id});
}
void add_line_increasing_slope(T a, T b, int id)
{
if constexpr (kGetMin)
{
_add_line2(-a, -b, id);
}
else
{
_add_line1(a, b, id);
}
}
void add_line_decreasing_slope(T a, T b, int id)
{
if constexpr (kGetMin)
{
_add_line1(-a, -b, id);
}
else
{
_add_line2(a, b, id);
}
}
T query(T x)
{ // arbitrary
assert(!s.empty());
auto l = *std::lower_bound(s.begin(), s.end());
// To get min
if constexpr (kGetMin)
{
return -l.eval(x);
}
else
{
return l.eval(x);
}
}
pair<T, int> query_increasing(T x)
{
while (s.size() >= 2 && s[0].first.eval(x) <= s[1].first.eval(x))
{
s.pop_front();
}
if constexpr (kGetMin)
{
return {-s[0].first.eval(x), s[0].second};
}
else
{
return {s[0].first.eval(x), s[0].second};
}
}
pair<T, int> query_decreasing(T x)
{
while (s.size() >= 2 && s.at(s.size() - 1).first.eval(x) <= s.at(s.size() - 2).first.eval(x))
{
s.pop_back();
}
if constexpr (kGetMin)
{
return {-s[0].first.eval(x), s[0].second};
}
else
{
return {s[0].first.eval(x), s[0].second};
}
}
};
int n, k, a[100005];
int s[100005];
pair<ll, int> dp[100005][250];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
LineDeque<false> d[205];
for (int i = 1; i <= k; i++)
{
d[i - 1].add_line_increasing_slope(s[0], dp[0][i - 1].first - s[0] * s[0], 0);
for (int j = 1; j <= n; j++)
{
dp[j][i] = max(dp[j][i], d[i - 1].query_increasing(s[j]));
d[i - 1].add_line_increasing_slope(s[j - 1], dp[j - 1][i - 1].first - s[j - 1] * s[j - 1], j - 1);
// cout << i << " " << j << " " << dp[j][i].first << " " << dp[j][i].second << endl;
// d[i].add_line_increasing_slope(s[j], dp[j][i] - s[j] * s[j]);
}
}
vector<int> res;
int x = n;
int the = k;
// cout << dp[x][the].second << endl;
// cout << dp[6][the - 1].second << endl;
// cout << dp[4][the - 2].second << endl;
// cout << d
while (x != 0 && the != 0)
{
x = dp[x][the].second;
res.push_back(x);
the--;
}
cout << dp[n][k].first << endl;
reverse(res.begin(), res.end());
for (int i : res)
{
cout << i << " ";
}
}
Compilation message
sequence.cpp: In member function 'void LineDeque<kGetMin>::add_line_increasing_slope(LineDeque<kGetMin>::T, LineDeque<kGetMin>::T, int)':
sequence.cpp:70:12: warning: 'if constexpr' only available with '-std=c++17' or '-std=gnu++17'
70 | if constexpr (kGetMin)
| ^~~~~~~~~
sequence.cpp: In member function 'void LineDeque<kGetMin>::add_line_decreasing_slope(LineDeque<kGetMin>::T, LineDeque<kGetMin>::T, int)':
sequence.cpp:82:12: warning: 'if constexpr' only available with '-std=c++17' or '-std=gnu++17'
82 | if constexpr (kGetMin)
| ^~~~~~~~~
sequence.cpp: In member function 'LineDeque<kGetMin>::T LineDeque<kGetMin>::query(LineDeque<kGetMin>::T)':
sequence.cpp:97:12: warning: 'if constexpr' only available with '-std=c++17' or '-std=gnu++17'
97 | if constexpr (kGetMin)
| ^~~~~~~~~
sequence.cpp: In member function 'std::pair<long long int, int> LineDeque<kGetMin>::query_increasing(LineDeque<kGetMin>::T)':
sequence.cpp:113:12: warning: 'if constexpr' only available with '-std=c++17' or '-std=gnu++17'
113 | if constexpr (kGetMin)
| ^~~~~~~~~
sequence.cpp: In member function 'std::pair<long long int, int> LineDeque<kGetMin>::query_decreasing(LineDeque<kGetMin>::T)':
sequence.cpp:129:12: warning: 'if constexpr' only available with '-std=c++17' or '-std=gnu++17'
129 | if constexpr (kGetMin)
| ^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
604 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Incorrect |
0 ms |
608 KB |
Integer 0 violates the range [1, 1] |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
604 KB |
contestant didn't find the optimal answer: 1064061 < 1093956 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1372 KB |
contestant didn't find the optimal answer: 414151152 < 610590000 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Incorrect |
2 ms |
4444 KB |
contestant didn't find the optimal answer: 140403179 < 140412195 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
39772 KB |
declared answer doesn't correspond to the split scheme: declared = 6113645600, real = 1818678304 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
58 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |