• [BZOJ1010]玩具装箱toy(斜率优化)


    Description

    P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京。他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中。P教授有编号为1...N的N件玩具,第i件玩具经过压缩后变成一维长度为Ci.为了方便整理,P教授要求在一个一维容器中的玩具编号是连续的。同时如果一个一维容器中有多个玩具,那么两件玩具之间要加入一个单位长度的填充物,形式地说如果将第i件玩具到第j个玩具放到一个容器中,那么容器的长度将为 x=j-i+Sigma(Ck) i<=K<=j 制作容器的费用与容器的长度有关,根据教授研究,如果容器长度为x,其制作费用为(X-L)^2.其中L是一个 常量。P教授不关心容器的数目,他可以制作出任意长度的容器,甚至超过L。但他希望费用最小.

    N<=50000

    Solution

    斜率优化DP入门题,

    常规方程:(dp[i]=min{dp[j]+(i-j-1+sum[i]-sum[j]-L)^2})

    (O(N^2))显然不行

    (f[i]=sum[i]+i,C=L+1)

    • 那么(dp[i]=min{dp[j]+(f[i]-f[j]-C)^2},0leq j <i)

    设在状态 i 中,决策 k ( k > j ) 较决策 j 更优得斜率式:

    • (( dp[ k ] – dp[ j ] + (f[ k ] + C )^2 – ( f[ j ] + C )^2 ) / 2*(f[ k ] – f[ j ] ) ≤ f[ i ])

    易证,

    即可将复杂度优化到 (O(N)),开个优先队列维护即可

    Code

    #include <cstdio>
    #include <algorithm>
    #define ll long long
    #define N 50010
    #define squ(x) ((x)*(x))
    using namespace std;
    
    int n,C,l,r,q[N];
    ll dp[N],f[N];
    
    inline int read(){
    	int x=0,f=1;char ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    	return x*f;
    }
    
    double calc(int x,int y){
    	return (dp[y]-dp[x]+squ(f[y]+C)-squ(f[x]+C))/(2.0*(f[y]-f[x]));
    }
    
    void DP(){
    	l=1,r=0;q[++r]=0;
    	for(int i=1;i<=n;++i){
    		while(l<r&&calc(q[l],q[l+1])<=f[i]) l++;
    		int t=q[l];
    		dp[i]=dp[t]+squ(f[i]-f[t]-C);
    		while(l<r&&calc(q[r],i)<calc(q[r-1],q[r])) r--;
    		q[++r]=i;
    	}
    }
    
    int main(){
    	n=read(),C=read()+1;
    	for(int i=1;i<=n;++i) f[i]=f[i-1]+read();
    	for(int i=1;i<=n;++i) f[i]+=i;
    	DP();
    	printf("%lld
    ",dp[n]);
    	return 0;
    } 
    
  • 相关阅读:
    [leetcode] Longest Palindromic Substring
    [leetcode] Add Two Numbers
    [leetcode] Longest Substring Without Repeating Characters
    [leetcode] Median of Two Sorted Arrays
    [leetcode] Two Sum
    poj 2718 Smallest Difference
    AOJ 0525 Osenbei
    poj3190 stall revertation
    poj1328Radar Installation
    poj 2376 Cleaning Shifts
  • 原文地址:https://www.cnblogs.com/void-f/p/8401001.html
Copyright © 2020-2023  润新知