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")
#pragma GCC optimize("unroll-loops")
#define fi first
#define se second
#define ryan bear
#define sq(X) ((X)*(X))
#define eb emplace_back
#define all(V) (V).begin(), (V).end()
#define unq(V) (V).erase(unique(all(V)), (V).end())
using namespace std;
typedef long long ll;
typedef vector<ll> vlm;
typedef vector<int> vim;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll INF = (1ll<<60);
int N, K; vlm ar; ll mx; int mi; vim lst[205];
struct line {
ll a, b; int ind;
ll get(ll x) { return a*x+b; }
line() {}
line(ll A, ll B, int i) { a=A, b=B, ind=i; }
};
struct LiChao {
vlm l, r; int tp;
vector<line> L;
void init() {
l.resize(N+10); r.resize(N+10); L.resize(N+10); tp=2;
fill(all(l), 0); fill(all(r), 0); fill(all(L), line(0, -INF, 0));
}
void upd(int now, line v, ll s, ll e) {
ll md=(s+e)/2;
line lo=L[now], hi=v;
if (lo.get(s)>hi.get(s)) swap(lo, hi);
if (lo.get(e)<=hi.get(e)) { L[now]=hi; return ; }
if (lo.get(md)>=hi.get(md)) {
L[now]=lo;
if (!l[now]) { l[now]=tp; tp++; }
upd(l[now], hi, s, md);
}
else {
L[now]=hi;
if (!r[now]) { r[now]=tp; tp++; }
upd(r[now], lo, md+1, e);
}
}
pll get(int now, ll x, ll s, ll e) {
if (!now) return pll(-INF, 0);
ll md=(s+e)/2;
if (x<=md) return max(pll(L[now].get(x), L[now].ind), get(l[now], x, s, md));
else return max(pll(L[now].get(x), L[now].ind), get(r[now], x, md+1, e));
}
}lct[205];
void dfs(int k, int i) {
if (!k) return ;
dfs(k-1, lst[k][i]);
printf("%d ", i);
}
int main() {
scanf("%d %d", &N, &K); ar.resize(N+1);
for (int i=1; i<=N; i++) { scanf("%lld", &ar[i]); ar[i]+=ar[i-1]; }
for (int i=0; i<=K; i++) { lct[i].init(); lst[i].resize(N+1); }
lct[0].upd(1, line(ar[N], 0, 0), 0, ar[N]);
for (int i=1; i<N; i++) {
if (i>=K) {
auto k=lct[K-1].get(1, ar[i], 0, ar[N]);
if (mx<=k.fi-ar[i]*ar[i]) mx=k.fi-ar[i]*ar[i], mi=i;
}
for (int j=min(i, K); j; j--) {
auto k=lct[j-1].get(1, ar[i], 0, ar[N]); k.fi-=ar[i]*ar[i];
lst[j][i]=k.se;
lct[j].upd(1, line(ar[N]+ar[i], k.fi-ar[N]*ar[i], i), 0, ar[N]);
}
}
printf("%lld\n", mx);
dfs(K, mi); puts("");
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &K); ar.resize(N+1);
~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:68:34: 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("%lld", &ar[i]); ar[i]+=ar[i-1]; }
~~~~~^~~~~~~~~~~~~~~~
# | 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... |