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>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
#define pb push_back
#define sz(a) a.size()
#define nl '\n'
#define popb pop_back()
#define ld double
#define ull unsigned long long
#define ff first
#define ss second
#define fix fixed << setprecision
#define pii pair<int, int>
#define E exit (0)
#define int long long
const int inf = 1e15, N = 1e5 + 5, mod = 1e9 + 7;
double eps = 1e-6;
bool fl = 0;
vector<pii> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int ntr(pii a, pii b) {
int x = b.ff - a.ff;
int y = a.ss - b.ss;
return (x + y - 1) / (y);
}
void solve() {
int n, k;
cin >> n >> k;
int p[n + 1];
pii pr[n + 1][k + 1];
p[0] = 0;
for (int i = 1, a; i <= n; i++) {
cin >> a;
p[i] = p[i - 1] + a;
}
int dp[n + 1][k + 1];
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= k; s++) dp[i][s] = -inf;
}
for (int i = 0; i <= k; i++) dp[0][i] = 0;
vector<pair<pii, pii>> s;
s.pb({{-inf, -1}, {0, 0}});
for (int j = 0; j <= k; j++) {
int l = 0;
for (int i = 1; i <= n; i++) {
while (l < sz(s) && s[l].ff.ff <= p[i]) l++;
dp[i][j] = (dp[i - 1][j], s[l - 1].ss.ff + s[l - 1].ss.ss * p[i]);
if (dp[i][j] == dp[i - 1][j]) {
pr[i][j] = {i - 1, j};
}else {
pr[i][j] = {s[l - 1].ff.ss, j - 1};
}
}
s.clear();
for (int i = 1; i <= n; i++) {
int a = dp[i][j] - p[i] * p[i], b = p[i];
if (sz(s) && a == s.back().ss.ff && b == s.back().ss.ss) continue;
int ps = -inf;
while (sz(s) && ntr({a, b}, s.back().ss) <= s.back().ff.ff) s.popb;
if (sz(s)) ps = ntr({a, b}, s.back().ss);
s.pb({{ps, i}, {a, b}});
}
}
cout << dp[n][k] << nl;
vector<int> ans;
int i = n, j = k;
while (j > 0) {
int i1 = pr[i][j].ff, j1 = pr[i][j].ss;
if (j1 == j) {
i = i1, j = j1;
continue;
}
ans.pb(i1);
i = i1, j = j1;
}
for (auto x: ans) cout << x << ' ';
}
main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
int tp = 1;
if (fl) cin >> tp;
while (tp--) solve();
}
Compilation message (stderr)
sequence.cpp: In function 'void solve()':
sequence.cpp:58:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while (l < sz(s) && s[l].ff.ff <= p[i]) l++;
| ^
sequence.cpp:59:27: warning: left operand of comma operator has no effect [-Wunused-value]
59 | dp[i][j] = (dp[i - 1][j], s[l - 1].ss.ff + s[l - 1].ss.ss * p[i]);
| ~~~~~~~~~~~^
sequence.cpp: At global scope:
sequence.cpp:91:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
91 | main() {
| ^~~~
# | 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... |