bzoj3675

题解:首先要明确一件事,就是当分割的方案固定时,无论先分割的哪一段,结果都是不变的,然后能列出dp方程:\(dp[i][j]=max(dp[k][j-1]+(a[i]-a[k])*(a[n]-a[i]))\),a[i]表示前缀和,我们能先枚举第二维,那么每层的dp值只和上一层相关,用滚动数组即可完成,然后对于方程变成了:dp[i]-a[i]a[n]-a[i]a[k]=max(dp'[k]-a[k]*a[n]),接下来就能斜率优化了

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}

using namespace std;

const ull ba=233;
const db eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+10,maxn=100000+10,inf=0x3f3f3f3f;

int n,k,q[N];
ll a[N],dp[N],te[N];
inline ll x(int i){return a[i];}
inline ll y(int i){return dp[i]-a[i]*a[n];}
inline db slope(int i,int j){return (db)(y(j)-y(i))/(x(j)-x(i));}
int main()
{
    scanf("%d%d",&n,&k);k++;
    for(int i=1;i<=n;i++)scanf("%lld",&a[i]),a[i]+=a[i-1];
    for(int _=1;_<=k;_++)
    {
        int head=1,last=1;q[head]=0;
        for(int i=1;i<=n;i++)
        {
            while(head<last&&slope(q[head],q[head+1])>-a[i])head++;
            int x=q[head];
            te[i]=dp[x]+(a[i]-a[x])*(a[n]-a[i]);
            while(head<last&&slope(q[last-1],q[last])<slope(q[last],i))last--;
            if(a[i]!=a[i-1])q[++last]=i;
        }
        for(int i=1;i<=n;i++)dp[i]=te[i];
    }
    printf("%lld\n",dp[n]);
    return 0;
}
/********************
6 3
4 1 3 4 2 3
********************/
posted @ 2019-02-24 09:56  walfy  阅读(191)  评论(0编辑  收藏  举报