zoukankan      html  css  js  c++  java
  • [HDU 3507]Print Article(斜率优化Dp)

    Description

    Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it to print articles. But it is too old to work for a long time and it will certainly wear and tear, so Zero use a cost to evaluate this degree.
    One day Zero want to print an article which has N words, and each word i has a cost Ci to be printed. Also, Zero know that print k words in one line will cost

    M is a const number.
    Now Zero want to know the minimum cost in order to arrange the article perfectly.

    Solution

    斜率优化Dp入门题

    代码明明很简单然而一开始写挂了(??)我还对拍了啊喂…什么错都没拍出来

    每次交都觉得能A,每次都证实是错觉,结果成为了HDU交的次数最多的一道题,想哭TvT

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<queue>
    using namespace std;
    int n,m;
    int sum[500005],f[500005];
    int q[1000005],head,tail;
    int read()
    {
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9'){
            if(c=='-')f=-1;c=getchar();
        }
        while(c>='0'&&c<='9'){
            x=x*10+c-'0';c=getchar();
        }
        return x*f;
    }
    int getx(int i,int j)
    {
        return f[j]+sum[j]*sum[j]-f[i]-sum[i]*sum[i];
    }
    int gety(int i,int j)
    {
        return 2*(sum[j]-sum[i]);
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            for(int i=1;i<=n;i++)
            {
                sum[i]=read();
                sum[i]+=sum[i-1];
            }
            head=tail=0;q[tail++]=0;
            for(int i=1;i<=n;i++)
            {
                while(head+1<tail&&getx(q[head],q[head+1])<=sum[i]*gety(q[head],q[head+1]))//要取到等号
                head++;
                f[i]=f[q[head]]+(sum[i]-sum[q[head]])*(sum[i]-sum[q[head]])+m;
                while(head+1<tail&&getx(q[tail-1],i)*gety(q[tail-2],q[tail-1])<=getx(q[tail-2],q[tail-1])*gety(q[tail-1],i))//顺序不能反否则符号会变
                tail--;
                q[tail++]=i;
            }
            printf("%d
    ",f[n]);
        }
        return 0;
    } 
  • 相关阅读:
    Java:Socket通信
    菜鸟玩云计算之十八:Hadoop 2.5.0 HA 集群安装第1章
    tolua reference
    严格符合CommonJS规范的包特性
    C++第11周(春)项目3
    Android动态逆向分析工具ZjDroid--脱壳神器
    报文格式【定长报文】
    OC3大回调模式使用总结(三)block回调
    Qt creator 编译错误 :cannot find file .pro qt
    OpenCV【2】---读取png图片显示到QT label上的问题
  • 原文地址:https://www.cnblogs.com/Zars19/p/6642804.html
Copyright © 2011-2022 走看看