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 FOR(i, j, k) for(int i=(j); i<=(k); i++)
#define FFOR(i, j, k) for(int i=(j); i<(k); i++)
#define DFOR(i, j, k) for(int i=(j); i>=(k); i--)
#define bug(x) cerr<<#x<<" = "<<(x)<<'\n'
#define pb push_back
#define mp make_pair
#define setbit(s, i) (s|=(1LL<<(i)))
#define bit(s, i) (((s)>>(i))&1LL)
#define mask(i) ((1LL<<(i)))
#define builtin_popcount __builtin_popcountll
typedef long long ll;
typedef long double ld;
template <typename T> inline void read(T &x){
char c;
bool nega=0;
while((!isdigit(c=getchar()))&&(c!='-'));
if(c=='-'){
nega=1;
c=getchar();
}
x=c-48;
while(isdigit(c=getchar())) x=x*10+c-48;
if(nega) x=-x;
}
template <typename T> inline void writep(T x){
if(x>9) writep(x/10);
putchar(x%10+48);
}
template <typename T> inline void write(T x){
if(x<0){
putchar('-');
x=-x;
}
writep(x);
}
template <typename T> inline void writeln(T x){
write(x);
putchar('\n');
}
#define taskname "sequence"
#define sqr(x) ((x)*(x))
int n, k;
ll a[100001];
ll s[100001];
ll f[100001];
int trace[100001][201];
deque <int> q;
const ll inf=mask(60);
class line{
public:
int a;
ll b;
///f(x)=ax+b;
line(int _a, ll _b){
a=_a;
b=_b;
}
line(){}
ll first_better(line &L){///a>=L.a
if(a==L.a){
if(L.b<=b) return 0;
else return inf;
}
else{
if(L.b<=b) return 0;
return (L.b-b-1)/(a-L.a)+1;
}
}
};
class data{
public:
line L;
int first;
data (line _L, int _first){
L=_L;
first=_first;
}
data(){}
};
void track(int nn, int k){
if(nn==0) return;
track(trace[nn][k], k-1);
if(nn==n) return;
write(nn);
putchar(' ');
}
data d[100001];
int main(){
#ifdef Kanikou
if(fopen(taskname".inp", "r"))
freopen(taskname".inp", "r", stdin);
#endif // Kanikou
read(n);
read(k);
FOR(i, 1, n) read(a[i]);
FOR(i, 1, n) s[i]=s[i-1]+a[i];
FOR(i, 1, n) f[i]=-mask(60);
f[0]=0;
ll a;
FOR(j, 0, k){
q.clear();
d[0]=data(line(0, 0), 0);
q.pb(0);
FOR(i, 1, n){
d[i]=data(line(s[i], f[i]-sqr(s[i])), 0);
while((q.size()>1)&&(d[q[1]].first<=s[i])) q.pop_front();
f[i]=s[i]*d[q.front()].L.a+d[q.front()].L.b;
trace[i][j]=q.front();
while(!q.empty()){
a=d[i].L.first_better(d[q.back()].L);
if(a>=s[n]) break;
if(a<=d[q.back()].first) q.pop_back();
else{
d[i].first=a;
q.pb(i);
break;
}
}
if(q.empty()) q.pb(i);
}
}
writeln(f[n]);
track(n, k);
}
# | 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... |