P3648 [APIO2014] Optimization of sequence into slope

Solution: The slope optimization \ (the DP \)

Submission: \ (2 \) times (purposely did not open \ (Long \ Long \) , and then he died)

answer:

Good put its own formula pushed out:
Simple:
the definition of \ (f [i] [j ] \) represents the former \ (I \) number for \ (J \) Maximum score cuts in, \ (S [I] \) for the prefix and
then transfer equation is:
\ (F [I] [J] = \ max (F [. 1-I] [J] + S [J] * (S [I] -s [J ])) \)
optimized at (first dimension omitted):
\ (F [I] = \ max (MEM [J] + S [J] * (S [I] -s [J]) \) , \ (F [J]) \) , \ (MEM [J] \) is equivalent to \ (f [i-1]
[j] \) into the slope of the optimization equation:
\ (MEM [J] -s [J ] ^ 2 = -s [i]
* s [j] + f [i] \) seeking \ (f [i] \) maximum, so maintaining the convex hull; slope \ (- s [i] \ ) monotone decreasing, so a monotone queue maintains a convex hull.
Kang then a solution to a problem the others: Maintenance under convex hull?
? ? ? Black question mark .jpg
later discovered that he is a fool: he was right, but the slope there and they take less compared to a \ (- 1 \)
\ (ou \) . . It is all right
(so I wrote or your own)

#include<cstdio>
#include<iostream>
#include<cstring>
#define R register int
#define ll long long
using namespace std;
namespace Luitaryi {
template<class I> inline I g(I& x) { x=0;
    register I f=1; register char ch; while(!isdigit(ch=getchar())) f=ch=='-'?-1:f;
    do x=x*10+(ch^48); while(isdigit(ch=getchar())); return x*=f;
} const int N=100010;
int n,k,h,t,q[N],pre[210][N]; 
ll f[N],mem[N],s[N];
inline int X(int i) {return s[i];}
inline ll Y(int i) {return mem[i]-s[i]*s[i];}
inline double K(int i,int j) {return 1.0*(Y(i)-Y(j))/(X(i)==X(j)?-1e-12:X(i)-X(j));}
inline void main() {
    g(n),g(k); for(R i=1,x;i<=n;++i) g(x),s[i]=s[i-1]+x;
    for(R i=1;i<=k;++i) { h=t=0; 
        for(R j=1;j<=n;++j) {
            while(h<t&&K(q[h],q[h+1])>=-s[j]) ++h;
            f[j]=mem[q[h]]+1ll*s[q[h]]*s[j]-1ll*s[q[h]]*s[q[h]];
            pre[i][j]=q[h];
            while(h<t&&K(q[t-1],j)<=K(q[t],j)) --t; q[++t]=j;
        } memcpy(mem,f,sizeof(f));
    } printf("%lld\n",f[n]);
    for(R i=k,j=n;i;--i) j=pre[i][j],printf("%d ",j); puts("");
}
} signed main() {Luitaryi::main(); return 0;}

2019.08.17
83

Guess you like

Origin www.cnblogs.com/Jackpei/p/11366957.html