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>
using namespace std;
#define fileio() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define fio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define allr(x) x.rbegin(), x.rend()
#define cmprs(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define endl "\n"
#define sp " "
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rz resize
#define sz(a) (int)(a.size())
#define clr(a) a.clear()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
typedef pair<double, int> pdi;
const int dx[] = {1,-1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 101010; // PLZ CHK!
const int MAXM = 202;
struct LiChao {
struct Line {
ll a,b,i;
pll get(ll x) {return {a*x+b,i};}
};
struct Node {
int l,r;
ll s,e;
Line line;
};
vector<Node> tree;
void init(ll s, ll e) { tree.pb({-1,-1,s,e,{0,-LINF,-LINF}}); }
void upd(int node, Line v) {
auto [l,r,s,e,curl]=tree[node];
ll m=(s+e)>>1;
Line lo=curl, hi=v;
if (lo.get(s)>hi.get(s)) swap(lo,hi);
if (lo.get(e)<=hi.get(e)) {
tree[node].line=hi;
return;
}
if (lo.get(m)<=hi.get(m)) {
tree[node].line=hi;
if (tree[node].r==-1) {
tree[node].r=sz(tree);
tree.pb({-1,-1,m+1,e,{0,-LINF,-LINF}});
}
upd(tree[node].r,lo);
}
else {
tree[node].line=lo;
if (tree[node].l==-1) {
tree[node].l=sz(tree);
tree.pb({-1,-1,s,m,{0,-LINF,-LINF}});
}
upd(tree[node].l,hi);
}
}
pll qry(int node, ll x) {
if (node==-1) return {-LINF,-LINF};
ll s=tree[node].s, e=tree[node].e;
ll m=(s+e)>>1;
if (x<=m) return max(tree[node].line.get(x), qry(tree[node].l,x));
else return max(tree[node].line.get(x), qry(tree[node].r,x));
}
};
int N,M;
ll P[MAXN],D[2][MAXN];
int par[MAXM][MAXN];
int main() {
fio();
cin>>N>>M;
for (int i=1; i<=N; i++) cin>>P[i], P[i]+=P[i-1];
for (int j=1; j<=M; j++) {
LiChao lic; lic.init(0,1e9+1);
for (int i=1; i<=N; i++) {
lic.upd(0,{P[i-1],-P[i-1]*P[i-1]+D[(j-1)&1][i-1],i-1});
auto [mx,mxi]=lic.qry(0,P[i]);
D[j&1][i]=mx;
par[j][i]=mxi;
}
}
cout<<D[M&1][N]<<endl;
vector<int> idx;
for (int x=N,y=M; y>=1; y--) {
idx.pb(par[y][x]);
x=par[y][x];
}
reverse(all(idx));
for (int i:idx) cout<<i<<sp;
return 0;
}
# | 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... |