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;
    } 
  • 相关阅读:
    node.js----服务器http
    node.js---对文件操作
    node.js
    历史管理
    h5
    git与github
    js中面向对象(创建对象的几种方式)
    jq基础
    POJ 2492 A Bug's Life
    POJ 1742 Coins
  • 原文地址:https://www.cnblogs.com/Zars19/p/6642804.html
Copyright © 2011-2022 走看看