This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
*/
#include <bits/stdc++.h>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define N 100005
#define bit(x,y) ((x>>y)&1LL)
#define na(x) (#x) << ":" << x
ostream& operator << (ostream &os, vector<long long>&x) {
for (int i = 0; i < x.size(); i++) os << x[i] << sp;
return os;
}
ostream& operator << (ostream &os, pair<int, int> x) {
cout << x.fi << sp << x.se << sp;
return os;
}
ostream& operator << (ostream &os, vector<pair<int, int> >&x) {
for (int i = 0; i < x.size(); i++) os << x[i] << endl;
return os;
}
#define ll long long
vector<ll> A, B, C;
bool bad(int l1, int l2, int l3) {
return (B[l3] - B[l1]) * (A[l1] - A[l2]) < (A[l1] - A[l3]) * (B[l2] - B[l1]) ;
}
void add(ll x, ll y, int z) {
if (!A.empty()) {
if (A.back() == x && B.back() <= y) A.pop_back(), B.pop_back(), C.pop_back();
}
A.push_back(x);
B.push_back(y);
C.push_back(z);
while (A.size() > 2 && bad(A.size() - 3, A.size() - 2, A.size() - 1)) {
A.erase(A.end() - 2);
B.erase(B.end() - 2);
C.erase(C.end() - 2);
}
}
int curit = 0;
pair<ll, int> query(int x) {
if (A.empty()) return mp(0, 0);
curit = min(curit, (int)A.size() - 1);
while (curit != A.size() - 1 && A[curit] * 1LL * x + B[curit] <= A[curit + 1] * 1LL * x + B[curit + 1]) curit++;
return mp(A[curit] * 1LL * x + B[curit], C[curit]);
}
int n, m;
int a[N];
ll sum[N];
ll dp[N][2];
int par[N][205];
vector<int> order;
signed main() {
#ifdef in1code
freopen("1test.inp", "r", stdin);
#endif
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sum[i] = sum[i - 1] + a[i];
for (int i = 1; i <= n; i++) dp[i][0] = 0;
for (int p = 1; p <= m; p++) {
int c = p & 1;
for (int i = 0; i < N; i++) dp[i][c] = 0;
A.clear(); B.clear(); C.clear();
for (int i = 1; i <= n; i++) {
pair<ll, int> rec = query(sum[i]);
dp[i][c] = rec.fi;
par[i][p] = rec.se;
add(sum[i], dp[i][c ^ 1] - sum[i]*sum[i], i);
}
}
printf("%lld\n", dp[n][m & 1]);
pair<int, int> cur = mp(n, m);
while (cur.se >= 1) {
order.push_back(par[cur.fi][cur.se]);
cur.fi = par[cur.fi][cur.se]; cur.se--;
}
for (int i = order.size() - 1; i >= 0; i--) printf("%d ", order[i]);
// cout << 1.0 * clock() / CLOCKS_PER_SEC << endl;
}
Compilation message (stderr)
sequence.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<long long int>&)':
sequence.cpp:14:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < x.size(); i++) os << x[i] << sp;
^
sequence.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<std::pair<int, int> >&)':
sequence.cpp:22:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < x.size(); i++) os << x[i] << endl;
^
sequence.cpp: In function 'std::pair<long long int, int> query(int)':
sequence.cpp:50:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (curit != A.size() - 1 && A[curit] * 1LL * x + B[curit] <= A[curit + 1] * 1LL * x + B[curit + 1]) curit++;
^
sequence.cpp: In function 'int main()':
sequence.cpp:65:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
^
sequence.cpp:66:77: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sum[i] = sum[i - 1] + a[i];
^
# | 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... |