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];
int s[100001];
ll sqs[100001];
ll f[100001];
int trace[100001][201];
deque <int> q;
const ll inf=mask(60);
class data{
public:
int a, first;
ll b;
// data (int _a, ll _b, int _first){
// a=_a;
// b=_b;
// first=_first;
// }
ll first_better(data &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;
}
}
};
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) sqs[i]=((ll)s[i])*s[i];
FOR(i, 1, n) d[i].a=s[i];
ll a;
FOR(j, 1, k){
q.clear();
d[0].a=d[0].b=d[0].first=0;
q.pb(0);
FOR(i, 1, n){
d[i].b=f[i]-sqs[i];
while((q.size()>1)&&(d[q[1]].first<=s[i])) q.pop_front();
f[i]=((ll)s[i])*d[q.front()].a+d[q.front()].b;
trace[i][j]=q.front();
while(true){
a=d[i].first_better(d[q.back()]);
if(a>=s[n]) break;
if(a<=d[q.back()].first){
q.pop_back();
if(q.empty()){
d[i].first=0;
q.pb(i);
break;
}
}
else{
d[i].first=a;
q.pb(i);
break;
}
}
}
}
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... |