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

    Print Article

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


    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 <cstdio>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 
     7 typedef long long int LL;
     8 
     9 LL dp[550000],sum[550000],deq[650000];
    10 int n,m,s,t,e;
    11 
    12 double slope(int i,int j)
    13 {
    14     return (double)(dp[i]+sum[i]*sum[i]-dp[j]-sum[j]*sum[j])/(sum[i]-sum[j]);
    15 }
    16 
    17 int main()
    18 {
    19     while(scanf("%d%d",&n,&m)!=EOF)
    20     {
    21         sum[0]=0;
    22         for(int i=1;i<=n;i++)
    23         {
    24             scanf("%d",sum+i);
    25             sum[i]+=sum[i-1];
    26         }
    27         s=0;
    28         for(int i=1;i<=n;i++)
    29         {
    30             if(sum[i]!=sum[s])
    31             {
    32                 s++;
    33                 sum[s]=sum[i];
    34             }
    35         }
    36         n=s;
    37         s=0,e=0,dp[0]=0,deq[0]=0;
    38         for(int i=1;i<=n;i++)
    39         {
    40             while(s<e&&slope(deq[s],deq[s+1])<=2*sum[i]) s++;
    41             int j=deq[s];
    42             dp[i]=dp[j]+(sum[i]-sum[j])*(sum[i]-sum[j])+m;
    43             while(s<e&&slope(deq[e-1],deq[e])>=slope(i,deq[e])) e--; e++;
    44             deq[e]=i;
    45         }
    46         printf("%d
    ",dp[n]);
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    常用第三方快递鸟单号查询Api接口免费对接调用攻略
    Solution -「CF 1477A」Nezzar and Board
    Solution -「THUPC 2021」区间矩阵乘法
    Solution Set -「CF 1520」
    Solution -「HNOI 2010」城市建设
    Solution -「NOI 2007」货币兑换
    Solution -「洛谷 P6156」简单题
    Solution -「YunoOI 2017」由乃的 OJ
    Journey -「CQOI 2021」
    Note -「SOS DP」高维前缀和
  • 原文地址:https://www.cnblogs.com/CKboss/p/3358866.html
Copyright © 2011-2022 走看看