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 CHT {
ll a[MAXN],b[MAXN],c[MAXN];
int pv=0,top=0;
ll f(int i, ll x) {return a[i]*x+b[i];}
void upd(ll aa, ll bb, ll cc) {
if (top>=1 && aa==a[top-1]) {
c[top-1]=cc;
return;
}
ll t1=(b[top-1]-b[top-2])*(a[top-2]-aa);
ll t2=(bb-b[top-2])*(a[top-2]-a[top-1]);
while (top>=2 && t1>=t2) top--;
a[top]=aa, b[top]=bb, c[top]=cc; top++;
if(pv>=top) pv=top-1;
}
pll qry(ll x) {
while(pv+1<top && a[pv+1]*x+b[pv+1]>a[pv]*x+b[pv]) pv++;
return {f(pv,x),c[pv]};
}
};
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++) {
CHT cht;
for (int i=1; i<=N; i++) {
cht.upd(P[i-1],-P[i-1]*P[i-1]+D[(j-1)&1][i-1],i-1);
auto [mx,mxi]=cht.qry(P[i]);
D[j&1][i]=mx;
par[j][i]=mxi;
}
}
cout<<D[M&1][N]<<endl;
vector<int> idx; idx.pb(-1);
for (int x=N,y=M; y>=1; y--) {
idx.pb(par[y][x]);
x=par[y][x];
}
sort(all(idx));
for (int i=1; i<=M; i++) {
if (idx[i]==0) idx[i]=1;
if (idx[i-1]>=idx[i]) idx[i]=idx[i-1]+1;
cout<<idx[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... |