zoukankan      html  css  js  c++  java
  • Print Article

    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
    ***************************************************************************************
    讲解的很清楚,有图又推导,不过看了很长时间才看懂,关键求斜率推导式
    ***************************************************************************************
     1 #include<iostream>
     2 #include<cstring>
     3 #include<string>
     4 #include<cstdio>
     5 #include<cmath>
     6 #define  L  long long
     7 using namespace std;
     8 L dp[500005];
     9 L sum[500005];
    10 int  q[500005];
    11 int n,m,i,j;
    12 void  init()
    13 {
    14     for(i=1;i<=n;i++)
    15      cin>>sum[i];
    16     for(i=2;i<=n;i++)
    17       sum[i]+=sum[i-1];
    18 
    19 }
    20 L getdp(int i,int j)//求dp
    21     {
    22         return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
    23     }
    24 L  getup(int j,int k)//分子
    25  {
    26      return   (dp[j]+sum[j]*sum[j])-(dp[k]+sum[k]*sum[k]);
    27  }
    28 L getdown(int j,int k)//分母
    29   {
    30       return  2*(sum[j]-sum[k]);
    31   }
    32 int main()
    33 {
    34    while(scanf("%d %d",&n,&m)!=EOF)
    35     {
    36         memset(dp,0,sizeof(dp));
    37         memset(sum,0,sizeof(sum));
    38         memset(q,0,sizeof(q));
    39         init();
    40       int head=0;
    41     int tail=0;
    42      q[0]=dp[0]=0;
    43      tail++;
    44      for(i=1;i<=n;i++)
    45       {
    46           while(head+1<tail&&getup(q[head+1],q[head])<=sum[i]*getdown(q[head+1],q[head]))
    47             head++;
    48             dp[i]=getdp(i,q[head]);
    49           while(head+1<tail&&getup(i,q[tail-1])*getdown(q[tail-1],q[tail-2])<=getup(q[tail-1],q[tail-2])*getdown(i,q[tail-1]))//删去一定不满足条件的点
    50             tail--;
    51           q[tail++]=i;
    52       }
    53 
    54   cout<<dp[n]<<endl;
    55 }
    56   return 0;
    57 
    58 
    59 
    60 }
    View Code
  • 相关阅读:
    第七周作业
    人月神话之没有银弹
    第六周作业
    第五周作业
    第四周作业
    第三周作业
    人月神话之沟通
    第二周作业
    第一周作业
    第八周作业
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3251238.html
Copyright © 2011-2022 走看看