zoukankan      html  css  js  c++  java
  • [luogu3195 HNOI2008] 玩具装箱TOY (斜率优化dp)

    题目描述

    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,L.接下来N行输入Ci.1<=N<=50000,1<=L,Ci<=10^7

    输出格式:
    输出最小费用

    输入输出样例

    输入样例#1:
    5 4
    3
    4
    2
    1
    4
    输出样例#1:
    1

    简单的斜率优化基础题 式子有点长(懒得打

    code:

    //By Menteur_Hxy
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <map>
    #include <vector>
    #include <queue>
    #include <set>
    #include <ctime>
    #define M(a,b) memset(a,(b),sizeof(a))
    #define F(i,a,b) for(register int i=(a);i<=(b);i++)
    #define LL long long
    using namespace std;
    
    inline LL rd() {
    	LL x=0,fla=1; char c=' ';
    	while(c>'9'|| c<'0') {if(c=='-') fla=-fla; c=getchar();}
    	while(c<='9' && c>='0') x=x*10+c-'0',c=getchar();
    	return x*fla;
    }
    
    inline void out(LL x){
        int a[25],wei=0;
        if(x<0) putchar('-'),x=-x;
        for(;x;x/=10) a[++wei]=x%10;
        if(wei==0){ puts("0"); return;}
        for(int j=wei;j>=1;--j) putchar('0'+a[j]);
        putchar('
    ');
    }
    
    const int N=50010;
    const int INF=0x3f3f3f3f;
    int n,L;
    LL da[N],f[N],s[N],q[N];
    
    double slope(LL k,LL j) {
    	return (double) (f[j]-f[k]+(da[j]+L)*(da[j]+L)-(da[k]+L)*(da[k]+L))/(2.0*(da[j]-da[k]));
    }
    
    int main() {
    	n=rd();L=rd();L++;
    	F(i,1,n) da[i]=rd(),da[i]+=da[i-1]+1;
    //	F(i,1,n) cout<<da[i]<<" ";cout<<endl;
    	int h=0,t=0;
    	F(i,1,n) {
    		while(h<t && slope(q[h],q[h+1])<=da[i]) h++;
    		int v=q[h];
    		f[i]=f[v]+(da[i]-da[v]-L)*(da[i]-da[v]-L);
    		while(h<t && slope(q[t],i)<slope(q[t-1],q[t])) t--;
    		q[++t]=i;
    	}
    //	F(i,1,n) cout<<f[i]<<" ";cout<<endl;
    	out(f[n]);
    	return 0;
    }
    
    版权声明:本文为博主原创文章,未经博主允许不得转载。 博主:https://www.cnblogs.com/Menteur-Hxy/
  • 相关阅读:
    string与wstring之间的转换
    QTableWidget去除选中虚边框
    在新机器部署Qt+mysql程序
    Qt文件路径分隔符
    MySQL通过增加用户实现远程连接数据库
    Qt 配置文件QSettings读取以及中文问题
    git config proxy
    ubuntu14.04 us sources.list
    How to keep Environment Variables when Using SUDO
    ubuntu hash sum mismatch error
  • 原文地址:https://www.cnblogs.com/Menteur-Hxy/p/9150052.html
Copyright © 2011-2022 走看看