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;
    } 
  • 相关阅读:
    centos7刚安装解决网络问题
    python matplotlib的常用绘图方法
    python实现最小可编辑距离
    JDBC快速入门
    关于MYSQL常用操作查询语言
    3w字详解java集合
    ELK实时日志分析平台环境部署--完整记录(转)
    ELK简介(转)
    python格式化输出(% & format)
    ElasticSearch查询 搜索 | 更新 | 查询
  • 原文地址:https://www.cnblogs.com/Zars19/p/6642804.html
Copyright © 2011-2022 走看看