• c++之路进阶——hdu3507(Print Article)


      参考博文:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html//讲的真的很好,有个小错误,博客里的num全为sum,像我这种弱渣都听懂了。真心点赞!!!

      

    Print Article

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


    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
    解释:    
     
     题解:
         1,用一个单调队列来维护解集。

         2,假设队列中从头到尾已经有元素a b c。那么当d要入队的时候,我们维护队列的上凸性质,即如果g[d,c]<g[c,b],那么就将c点删除。直到找到g[d,x]>=g[x,y]为止,并将d点加入在           该位置中。 

         3,求解时候,从队头开始,如果已有元素a b c,当i点要求解时,如果g[b,a]<sum[i],那么说明b点比a点更优,a点可以排除,于是a出队。最后dp[i]=getDp(q[head])。

      代码:
           
     1 #include<cstdio>
     2 #include<cmath>
     3 #include<iostream>
     4 #define maxn 500100
     5 
     6 using namespace std;
     7 
     8 struct get
     9     {
    10         int n,m,sum[maxn],a[maxn],dp[maxn],q[maxn];
    11         int getup(int j,int k){return dp[j]+sum[j]*sum[j]-dp[k]-sum[k]*sum[k];}//分子 
    12         int getdown(int j,int k){return 2*sum[j]-2*sum[k];}//分母 
    13         int getdp(int i,int j) {return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);}//dp[i]
    14         get()
    15            {
    16                 while (scanf("%d%d",&n,&m)==2)
    17                   {
    18                          for(int i=1;i<=n;i++) scanf("%d",&sum[i]);       
    19                    sum[0]=dp[0]=0;         
    20                    for(int i=1;i<=n;i++)sum[i]+=sum[i-1];
    21                         int  t=0,w=1;
    22                         for (int i=1;i<=n;i++)
    23                             {
    24                                    while (t+1<w&&sum[i]*getdown(q[t+1],q[t])>=getup(q[t+1],q[t])) t++;//维护队列,删除就点之前所有点
    25                                    dp[i]=getdp(i,q[t]);
    26                                    while (t+1<w&&getup(i,q[w-1])*getdown(q[w-1],q[w-2])<=getup(q[w-1],q[w-2])*getdown(i,q[w-1])) w--;//维护队列,保证队列具有 上凸性质。
    27                                     q[w++]=i;
    28                             }
    29                   printf("%d
    ",dp[n]);            
    30                   }
    31             }   
    32     }get;
    33 int main()
    34   {
    35       get;
    36     return 0;      
    37   }
     顺便吐槽一下,

    Presentation Error 这种错误你们见过么?我也是醉了!

    Recommend
    zhengfeng   |   We have carefully selected several similar problems for you:  3506 3501 3504 3505 3498 
     
  • 相关阅读:
    Python操作Word:常用对象介绍
    Python入门教程 超详细1小时学会Python
    用几何画板画七边形的方法
    用ChemDraw画3D图的方法
    Chem 3D模型的参数值更改方法
    Chem 3D中怎么创建立体模型
    在几何画板中输入绝对值的方法
    怎么给几何画板文字加立体阴影效果
    几何画板放大和缩小的方法
    ChemDraw Pro和ChemBio3D Ultra有什么区别
  • 原文地址:https://www.cnblogs.com/grhyxzc/p/5155506.html
Copyright © 2020-2023  润新知