斜率优化专题

BZOJ 1597: [Usaco2008 Mar]土地购买

删掉没用的点,剩下的点排序
fi=min(fj+aj+1bi)

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
int n;
pair<ll,ll> p[100000],p2[100000];

ll f[100000];
int que[100000]={0};
int H = 0 , T = 0;
ll getdp(int i,int j) {
    return f[que[H]] + p2[que[H]+1].fi * p2[i].se;
}  
ll getUp(int j,int k) { //yj-yk
    return f[j]-f[k];
}
ll getDown(int j,int k) { // xj-xk
    return p2[j+1].fi - p2[k+1].fi;
}
// fi = min( fj + cost(j+1,i) )
void slope_dp(int n) {
    MEM(que) H=T=0;
    For(i,n) {
        while( H<T && getUp(que[H+1],que[H] ) <= getDown(que[H+1], que[H]) * (-p2[i].se ) )     // 条件 H+1比H优 
            ++H;
        f[i]=getdp(i,que[H]);
        while( H<T && getUp(i,que[T] ) * getDown(que[T],que[T-1]) >= getUp(que[T], que[T-1] ) * getDown(i, que[T] ) ) //上凸壳 ?
            --T;
        que[++T] =i;  
    }
}
int main()
{
//  freopen("bzoj1597.in","r",stdin);
//  freopen(".out","w",stdout);

    n=read();
    Rep(i,n) p[i].fi=read(),p[i].se=read();
    sort(p,p+n);
    int m=0;
    Rep(i,n) {
        if (!m) p2[++m] = p[i];
        else {
            while (m>0&&p2[m].fi<=p[i].fi&&p2[m].se<=p[i].se) m--;
            p2[++m] = p[i] ;
        }
    }
    reverse(p2+1,p2+1+m);

    slope_dp(m); 

    cout<<f[m]<<endl;

    return 0;
}

BZOJ 1010 玩具装箱toy

fi=min(fj+(ci+icjj1L)2
Fi=fi+(ci+i)2
Fi=min(Fj(cj+j)2+(ci+i)22(cicj+ij1L)2

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (50000+10)
int n;
ll c[MAXN],L;
ll f[MAXN];
int que[MAXN]={0};
int H = 0 , T = 0;
ll sqr(ll x) {return x*x;} 
ll getdp(int i,int j) {
    return f[j] - sqr(c[j]+j) + sqr(c[i]+i) + sqr(c[i]-c[j] +i-j-1-L);
}  
ll getUp(int j,int k) { //yj-yk
    return f[j]-f[k] ;
}
ll getDown(int j,int k) { // xj-xk
    return c[j]+j-c[k]-k;
}
// fi = min( fj + cost(j+1,i) )
void slope_dp(int n) {
    MEM(que) H=T=0;
    f[0]=0;
    For(i,n) {
        while( H<T && getUp(que[H+1],que[H] ) <= getDown(que[H+1], que[H]) * (2LL* (c[i]+i - 1 - L)) )      // 条件 que[H+1]比que[H]优 
            ++H;
        f[i]=getdp(i,que[H]);
        while( H<T && getUp(i,que[T] ) * getDown(que[T],que[T-1]) <= getUp(que[T], que[T-1] ) * getDown(i, que[T] ) ) // 上凸壳  
            --T;
        que[++T] =i;  
    }
}
int main()
{
//  freopen("bzoj1010.in","r",stdin);
//  freopen(".out","w",stdout);

    cin>>n>>L;
    c[0]=0;
    For(i,n) {
        cin>>c[i];
        c[i]+=c[i-1];
    }
    slope_dp(n);
    cout<<f[n]-sqr(c[n]+n)<<endl;
    return 0;
}

HDU 3507 Print Article

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
int n,M;
#define MAXN (500000+10)
ll c[MAXN];
ll f[MAXN];
int que[MAXN]={0};
int H = 0 , T = 0;
ll sqr(ll x){return x*x;}
ll getdp(int i,int j) {
    return f[j] + sqr(c[i]-c[j]) + M;
}  
ll getUp(int j,int k) { //yj-yk
    return f[j] + sqr(c[j]) -f[k] -sqr(c[k]);
}
ll getDown(int j,int k) { // xj-xk
    return c[j] - c[k];
}
// fi = min( fj + cost(j+1,i) )
void slope_dp(int n) {
    MEM(que) H=T=0;
    f[0]=0;
    For(i,n) {
        while( H<T && getUp(que[H+1],que[H] ) <= getDown(que[H+1], que[H]) * (2*c[i]) )     // 条件 que[H+1]比que[H]优 
            ++H;
        f[i]=getdp(i,que[H]);
        while( H<T && getUp(i,que[T] ) * getDown(que[T],que[T-1]) <= getUp(que[T], que[T-1] ) * getDown(i, que[T] ) ) // 大等于和下等于取决于  min,max 
            --T;
        que[++T] =i;  
    }
}
int main()
{
//  freopen("hdu3507.in","r",stdin);
//  freopen(".out","w",stdout);

    while(cin>>n>>M) {
        c[0]=0;
        For(i,n) {
            cin>>c[i];
            c[i]+=c[i-1];
        }
        slope_dp(n);
        cout<<f[n]<<endl;
    }
    return 0;
}

HDU 2829 Lawrence

注意这题的初始化

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
ll sqr(ll x){ return x*x;}
#define MAXN (1000+10)
int n,m;
ll a[MAXN],c[MAXN],s[MAXN];
ll f[MAXN][MAXN];
int que[MAXN]={0};
int H = 0 , T = 0;
ll getdp(int i,int j,int x) {
    return f[x-1][j] + (sqr(c[i] - c[j] ) -(s[i] - s[j] ))/2;
}  
ll getUp(int j,int k,int x) { //yj-yk
    return f[x-1][j]+(sqr(c[j])+s[j])/2-f[x-1][k]-(sqr(c[k])+s[k])/2;
}
ll getDown(int j,int k) { // xj-xk
    return c[j] - c[k] ;
}
// fi = min( fj + cost(j+1,i) )
void slope_dp(int n) {
    MEM(que) 
    MEMI(f) f[0][0]=0;
    For(x,m+1) {
        H=T=0;
        For(i,n) {
            while( H<T && getUp(que[H+1],que[H] ,x) <= getDown(que[H+1], que[H]) * ( c[i] ) )       // 条件 que[H+1]比que[H]优 
                ++H;
            f[x][i]=getdp(i,que[H],x);
            while( H<T && getUp(i,que[T] ,x) * getDown(que[T],que[T-1]) <= getUp(que[T], que[T-1] ,x ) * getDown(i, que[T] ) ) // 大等于和下等于取决于  min,max 
                --T;
            que[++T] =i;  
        }
    }
}
int main()
{
//  freopen("hdu2829.in","r",stdin);
//  freopen(".out","w",stdout);

    while(cin>>n>>m && n && m) {
        For(i,n) cin>>a[i];
        c[0]=0;
        For(i,n) c[i] = c[i-1] + a[i] ,s[i] = s[i-1] + sqr( a[i] );     
        slope_dp(n);
        cout << f[m+1][n] << endl;
    }

    return 0;
}

POJ 2018 Best Cow Fences

求一段数列的长度>=m子串的最大平均值, n<=200000
周源的论文上有证明
因为不能去掉队首元素,我们考虑保留原有队列二分,显然单峰性质是成立的
直接判定相邻两个元素。。独特的三分写法?

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip> 
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
int n,m;
#define MAXN (100000+10)
ll a[MAXN];
int que[MAXN]={0};
int H = 0 , T = 0;
ll getUp(int j,int k) { //yj-yk
    return a[j]-a[k];
}
ll getDown(int j,int k) { // xj-xk
    return j-k;
}
// fi = min( fj + cost(j+1,i) )
void slope_dp(int n) {
    ld ret=0;   
    MEM(que) H=T=0;
    Fork(i,m,n) {
        int l=H,r=T;
        while(l<=r) {
            int mid=(l+r)/2;
            ret=max(ret,(ld)getUp(i,que[mid])*1000/getDown(i,que[mid]) );
            if (getUp(i,que[mid])*getDown(i,que[mid-1])>=getUp(i,que[mid-1])*getDown(i,que[mid])) {
                l=mid+1;
            } else {
                r=mid-1;
            }
        }           
        while( H<T && getUp(i-m+1,que[T] ) * getDown(i-m+1,que[T-1]) <= getUp(i-m+1, que[T-1] ) * getDown(i-m+1, que[T] ) ) // 大等于和下等于取决于  min,max 
            --T;
        que[++T] =i-m+1;  
    }
    cout<<setiosflags(ios::fixed)<<setprecision(0);
    cout<<floor(ret)<<endl; 
}

int main()
{
//  freopen("poj2018.in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n>>m;
    a[0]=0;
    For(i,n) {
        cin>>a[i];
        a[i]+=a[i-1];
    }
    slope_dp(n);
    return 0;
}

BZOJ 1096 [ZJOI2007]仓库建设

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (1000000+10)
int n;
ll f[MAXN],p[MAXN],Cost[MAXN],c[MAXN],d[MAXN],x[MAXN];
int que[MAXN]={0};
int H = 0 , T = 0;
ll getdp(int i,int j) {
    return f[j] + x[i]*(c[i]-c[j]) -( d[i]-d[j] )+ Cost[i];
}  
ll getUp(int j,int k) { //yj-yk
    return d[j] + f[j] -d[k] - f[k];
}
ll getDown(int j,int k) { // xj-xk
    return c[j]-c[k];
}
// fi = min( fj + cost(j+1,i) )
void slope_dp(int n) {
    MEM(que) H=T=0;
    f[0]=0;
    For(i,n) {
        while( H<T && getUp(que[H+1],que[H])<=x[i]*getDown(que[H+1],que[H]) )       // 条件 que[H+1]比que[H]优 
            ++H;
        f[i]=getdp(i,que[H]);
        while( H<T && getUp(i,que[T] ) * getDown(que[T],que[T-1]) <= getUp(que[T], que[T-1] ) * getDown(i, que[T] ) ) // 大等于和下等于取决于  min,max 
            --T;
        que[++T] =i;  
    }
}
int main()
{
//  freopen("bzoj1096.in","r",stdin);
//  freopen(".out","w",stdout);
    int n=read();
    x[0]=p[0]=c[0]=d[0]=Cost[0]=0;
    For(i,n) {
        x[i]=read();
        p[i]=read();
        Cost[i]=read();
        d[i]=d[i-1]+p[i]*x[i];
    }
    For(i,n) c[i]=p[i]+c[i-1];
    slope_dp(n);
    cout<<f[n]<<endl;

    return 0;
}

BZOJ 3156 防御准备

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (1000000+10)
int n;
ll f[MAXN],s[MAXN],a[MAXN];
int que[MAXN]={0};
int H = 0 , T = 0;
ll getdp(int i,int j) {
    return f[j] + (ll)(i-j)*(i-j-1)/2 + a[i];
}  
ll getUp(int j,int k) { //yj-yk
    return 2*(f[j]-f[k]) + s[j]-s[k];
}
ll getDown(int j,int k) { // xj-xk
    return 2*(j-k);
}
// fi = min( fj + cost(j+1,i) )
void slope_dp(int n) {
    MEM(que) H=T=0;
    f[0]=0;
    For(i,n) {
        while( H<T && getUp(que[H+1],que[H])<=i*getDown(que[H+1],que[H]) )      // 条件 que[H+1]比que[H]优 
            ++H;
        f[i]=getdp(i,que[H]);
        while( H<T && getUp(i,que[T] ) * getDown(que[T],que[T-1]) <= getUp(que[T], que[T-1] ) * getDown(i, que[T] ) ) // 大等于和下等于取决于  min,max 
            --T;
        que[++T] =i;  
    }
}
int main()
{
//  freopen("bzoj3156.in","r",stdin);
//  freopen(".out","w",stdout);
    int n=read();
    s[0]=f[0]=a[0]=0;
    For(i,n) {
        a[i]=read();
        s[i]=i+(ll)i*i;
    }
    slope_dp(n);
    cout<<f[n]<<endl;

    return 0;
}

BZOJ 3437 小P的牧场

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (1000000+10)
int n;
ll f[MAXN],p[MAXN],Cost[MAXN],c[MAXN],d[MAXN],x[MAXN];
int que[MAXN]={0};
int H = 0 , T = 0;
ll getdp(int i,int j) {
    return f[j] + x[i]*(c[i]-c[j]) -( d[i]-d[j] )+ Cost[i];
}  
ll getUp(int j,int k) { //yj-yk
    return d[j] + f[j] -d[k] - f[k];
}
ll getDown(int j,int k) { // xj-xk
    return c[j]-c[k];
}
// fi = min( fj + cost(j+1,i) )
void slope_dp(int n) {
    MEM(que) H=T=0;
    f[0]=0;
    For(i,n) {
        while( H<T && getUp(que[H+1],que[H])<=x[i]*getDown(que[H+1],que[H]) )       // 条件 que[H+1]比que[H]优 
            ++H;
        f[i]=getdp(i,que[H]);
        while( H<T && getUp(i,que[T] ) * getDown(que[T],que[T-1]) <= getUp(que[T], que[T-1] ) * getDown(i, que[T] ) ) // 大等于和下等于取决于  min,max 
            --T;
        que[++T] =i;  
    }
}
int main()
{
//  freopen("bzoj3437.in","r",stdin);
//  freopen(".out","w",stdout);
    int n=read();
    x[0]=p[0]=c[0]=d[0]=Cost[0]=0;
    For(i,n) x[i]=i-1;
    For(i,n) Cost[i]=read();
    For(i,n) p[i]=read();
    For(i,n) {
        d[i]=d[i-1]+p[i]*x[i];
    }
    For(i,n) c[i]=p[i]+c[i-1];
    slope_dp(n);
    cout<<f[n]<<endl;

    return 0;
}

BZOJ 3675 [Apio2014]序列分割

必须开滚动数组

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (100000+10)
#define MAXK (200+10)
int n,k;
ll f[4][MAXN],a[MAXN],d[MAXN],c[MAXN];
int que[MAXN]={0};
int H = 0 , T = 0;
ll getdp(int i,int j,int K) {
    return f[K%3][j] + (c[i]-c[j])*d[i];
}  
ll getUp(int j,int k,int K) { //yj-yk
    return f[K%3][j]-f[K%3][k];
}
ll getDown(int j,int k) { // xj-xk
    return c[j]-c[k];
}
void slope_dp(int n) {
    MEMi(f)
    f[0][0]=0;
    For(K,k) {  
        H=T=0; que[0]=0;f[K%3][0]=-INF;
        For(i,n) {
            while( H<T && getUp(que[H+1],que[H],K-1 ) >= getDown(que[H+1], que[H])* d[i] )      // 条件 que[H+1]比que[H]优 
                ++H;
            f[K%3][i]=getdp(i,que[H],K-1);
            while( H<T && getUp(i,que[T],K-1 ) * getDown(que[T],que[T-1]) >= getUp(que[T], que[T-1],K-1 ) * getDown(i, que[T] ) ) // 大等于和下等于取决于  min,max 
                --T;
            que[++T] =i;  
        }
    }
}

int main()
{
//  freopen("bzoj3675.in","r",stdin);
//  freopen(".out","w",stdout);
    n=read(); k=read()+1;
    ll S=0;
    For(i,n) {
        a[i]=read();
        S+=a[i];
    }
    c[0]=0,d[0]=S;
    For(i,n) {
        c[i]=c[i-1]+a[i];
        d[i]=S-c[i];
    }
    slope_dp(n);
    cout<<f[k%3][n]<<endl;
    return 0;
}

BZOJ 4518 [Sdoi2016]征途

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (3000+10)
int n,m;
ll f[2][MAXN],c[MAXN];
int que[MAXN]={0};
int H = 0 , T = 0;
ll getdp(int i,int j,int K) {
    return f[K%2][j] + (c[i]-c[j])*(c[i]-c[j]);
}  
ll getUp(int j,int k,int K) { //yj-yk
    return f[K%2][j]+c[j]*c[j]-f[K%2][k] - c[k]*c[k];
}
ll getDown(int j,int k) { // xj-xk
    return 2*(c[j]-c[k]);
}
// fi = min( fj + cost(j+1,i) )
void slope_dp(int n) {
    MEMI(f)
    f[0][0]=0;
    For(K,m) {
        H=T=0;  f[K%2][0]=INF;
        For(i,n) {
            while( H<T && getUp(que[H+1],que[H] ,K-1 ) <= getDown(que[H+1], que[H]) * c[i] )        // 条件 que[H+1]比que[H]优 
                ++H;
            f[K%2][i]=getdp(i,que[H],K-1);
            while( H<T && getUp(i,que[T],K-1 ) * getDown(que[T],que[T-1]) <= getUp(que[T], que[T-1],K-1 ) * getDown(i, que[T] ) ) // 大等于和下等于取决于  min,max 
                --T;
            que[++T] =i;  
        }
    }
}
int main()
{
//  freopen("bzoj4518.in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n>>m;
    c[0]=0;
    For(i,n) {
        c[i]=read()+c[i-1];
    }
    slope_dp(n);
    cout<<m*f[m%2][n]-c[n]*c[n]<<endl;  
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值