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;
    } 
  • 相关阅读:
    苏菲玛索的早年写真真的迷人
    VC编程技巧:IE控件的高级用法
    玫举窗口句柄,然后用窗口句柄去取得进程ID,比较得到的进程ID是不是你的进程ID....
    一个封装的XMLHttp Object Pool and XMLHttp chunnel Pool,挺不错
    VSTS For Testers读书笔记(3)
    VSTS For Testers读书笔记(8)
    美丽的厦门
    VSTS For Testers读书笔记(10)
    VSTS For Testers读书笔记(7)
    VSTS For Testers读书笔记(12)
  • 原文地址:https://www.cnblogs.com/Zars19/p/6642804.html
Copyright © 2011-2022 走看看