zoukankan      html  css  js  c++  java
  • hdu 3507 Print Article

    http://acm.hdu.edu.cn/showproblem.php?pid=3507

    Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

    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
     
    斜率优化
    把斜率转化为乘积的形式,避免浮点数误差
    #include<cstdio>
    #include<cstring>
    #define N 500001
    using namespace std;
    typedef long long LL;
    LL sum[N],dp[N];
    int c,q[N],head,tail; 
    long long up(int k,int j) 
    {
        return dp[j]-dp[k]-sum[k]*sum[k]+sum[j]*sum[j];
    }
    long long down(int k,int j)
    {
        return 2*(sum[j]-sum[k]);
    }
    int main()
    {
        int n,m,x;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            memset(sum,0,sizeof(sum));
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&x);
                sum[i]=sum[i-1]+x;
            }
            head=tail=0;
            memset(q,0,sizeof(q));
            memset(dp,0,sizeof(dp));
            for(int i=1;i<=n;i++)
            {
                while(head<tail && up(q[head],q[head+1])<=sum[i]*down(q[head],q[head+1])) head++;
                dp[i]=dp[q[head]]+m+(sum[i]-sum[q[head]])*(sum[i]-sum[q[head]]);
                while(head<tail && up(q[tail-1],q[tail])*down(q[tail],i)>=up(q[tail],i)*down(q[tail-1],q[tail])) tail--;
                q[++tail]=i;
            }
            printf("%lld
    ",dp[n]);
        }
    }
  • 相关阅读:
    android Fragment和FragmentActivity
    android 生成xml文件
    android:在ViewPager中使用Button
    android延迟执行
    android.os.NetworkOnMainThreadException 异常
    导入android工程没有R文件的解决办法
    20个常用的Java程序块
    Java中的==、equals、hasCode方法
    《head first java 》读书笔记
    【写给自己】2014-03-13
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7479606.html
Copyright © 2011-2022 走看看