zoukankan      html  css  js  c++  java
  • hdu3507Print Article(斜率优化dp)

    Print Article

    Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
    Total Submission(s): 12824    Accepted Submission(s): 3967


    Problem 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.
     
    Input
    There are many test cases. For each test case, There are two numbers N and M in the first line (0 ≤ n ≤ 500000, 0 ≤ M ≤ 1000). Then, there are N numbers in the next 2 to N + 1 lines. Input are terminated by EOF.
     
    Output
    A single number, meaning the mininum cost to print the article.
     
    Sample Input
    5 5 5 9 5 7 5
     
    Sample Output
    230
     
    Author
    Xnozero
     
    Source
     
    Recommend
    zhengfeng   |   We have carefully selected several similar problems for you:  3506 3501 3504 3505 3498 
     
    斜率优化dp学习:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    #define N 500005
    
    using namespace std;
    int dp[N],q[N],sum[N];
    int head,tail,n,m;
    
    int get_dp(int i,int j)
    {
        return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
    }
    
    int get_up(int j,int k)
    {
        return dp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]);
    }
    
    int get_down(int j,int k)
    {
        return 2*(sum[j]-sum[k]);
    }
    
    int main()
    {
        while(scanf("%d%d",&n,&m)==2)
        {
            for(int i=1;i<=n;i++) scanf("%d",&sum[i]);
            sum[0]=dp[0]=0;head=tail=0;
            for(int i=1;i<=n;i++) sum[i]+=sum[i-1];
            q[tail++]=0;
            for(int i=1;i<=n;i++)
            {
                while(head+1<tail && get_up(q[head+1],q[head])<=sum[i]*get_down(q[head+1],q[head]))
                  head++;
                dp[i]=get_dp(i,q[head]);
                while(head+1<tail && get_up(i,q[tail-1])*get_down(q[tail-1],q[tail-2])<=get_up(q[tail-1],q[tail-2])*get_down(i,q[tail-1]))
                  tail--;
                q[tail++]=i;
            }
            printf("%d
    ",dp[n]);
        }
        return 0;
    }
    折花枝,恨花枝,准拟花开人共卮,开时人去时。 怕相思,已相思,轮到相思没处辞,眉间露一丝。
  • 相关阅读:
    经典javascript
    大话prototype
    DataTable使用方法总结
    实验四 Web服务器1socket编程
    2.4 OpenEuler中C语言中的函数调用测试
    20191323王予涵第13章学习笔记
    20191323王予涵第十三章学习笔记
    2.5 OpenEuler 中C与汇编的混合编程
    个人贡献
    20191323王予涵第十二章学习笔记
  • 原文地址:https://www.cnblogs.com/L-Memory/p/7206544.html
Copyright © 2011-2022 走看看