zoukankan      html  css  js  c++  java
  • 【BZOJ1010】玩具装箱

    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。但他希望费用最小.

    Input

    第一行输入两个整数N,L.接下来N行输入Ci.1<=N<=50000,1<=L,Ci<=10^7

    Output

    输出最小费用

    Sample Input

    5 4
    3
    4
    2
    1
    4

    Sample Output

    1

    【分析】

    首先直接推导出动态规划的转移方程:f[i]=f[j]+(sum[i]-sum[j]-c)^2;

    发现显然会超时,由方程想到加斜率优化。

    设两个点,k,j(k<=j),然后打表验证一下单调性就行了...=_=(我好懒..)

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <cmath>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <queue>
     9 #include <map>
    10 //#define LOCAL
    11 #define ll long long
    12 const int maxn=50000+5;
    13 const int INF=0x7fffffff;
    14 using namespace std;
    15 ll sum[maxn],data[maxn],c;
    16 ll f[maxn],Q[maxn];
    17  
    18 ll g(int k,int j)
    19 {
    20          //g函数 
    21          return f[k]+(sum[k]+c)*(sum[k]+c)-f[j]-(sum[j]+c)*(sum[j]+c);
    22 }
    23 ll s(int k,int j) {return 2*(sum[k]-sum[j]);} 
    24  
    25 int main() 
    26 {
    27     int n;
    28     #ifdef LOCAL 
    29     freopen("data.txt","r",stdin);
    30     freopen("out.txt","w",stdout);
    31     #endif
    32     sum[0]=0;
    33     scanf("%d%lld",&n,&c);
    34     for (int i=1;i<=n;i++)
    35     {
    36         scanf("%lld",&data[i]);
    37         sum[i]=sum[i-1]+data[i];
    38     }
    39     //加上各个玩具中间的空格 
    40     for (int i=1;i<=n;i++) sum[i]+=i;
    41     c++;
    42     int l=0,r=0;
    43     f[0]=0;Q[r++]=0;
    44     for (int i=1;i<=n;i++)
    45     {
    46         while (l<r-1 && g(Q[l+1],Q[l])<=sum[i]*s(Q[l+1],Q[l])) l++;
    47         f[i]=f[Q[l]]+(sum[i]-sum[Q[l]]-c)*(sum[i]-sum[Q[l]]-c);
    48         Q[r++]=i;
    49         for (int j=r-2;j>l;j--)
    50         {
    51             ll x,y,z;
    52             z=Q[j+1];y=Q[j];x=Q[j-1];
    53             if (!(g(y,x)*s(z,y)<g(z,y)*s(y,x))) Q[j]=Q[--r];
    54             else break;
    55              
    56         }
    57     }
    58     printf("%lld
    ",f[n]);
    59     return 0;
    60 }
  • 相关阅读:
    BZOJ 2655: calc(拉格朗日插值)
    BZOJ 1485: [HNOI2009]有趣的数列(卡特兰数)
    [学习笔记] 关于组合数的一些总结
    CF 1076E Vasya and a Tree(线段树+树剖)
    CF 1082E Increasing Frequency(贪心)
    51nod 1149 Pi的递推式(组合数学)
    LOJ 2743(洛谷 4365) 「九省联考 2018」秘密袭击——整体DP+插值思想
    关于 unsigned int 比较大小
    洛谷 3295 [SCOI2016]萌萌哒——并查集优化连边
    洛谷 P4512 [模板] 多项式除法
  • 原文地址:https://www.cnblogs.com/hoskey/p/3895055.html
Copyright © 2011-2022 走看看