zoukankan      html  css  js  c++  java
  • HDU3507 Print Article

    Print Article

    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
     
    Author
    Xnozero
     
    Source
     
    Recommend
    zhengfeng
     
    裸的斜率dp。。注意不等号方向。。样例数据挺好的。。不管你是什么符号都输出230..23333
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<iostream>
     4 #include<algorithm>
     5 using namespace std;
     6 const int N = 500100;
     7 #define For(i,n) for(int i=1;i<=n;i++)
     8 #define Rep(i,l,r) for(int i=l;i<=r;i++)
     9 
    10 int n,M,sum[N],dp[N],q[N];
    11 //  dp[i] = max{dp[j] + (sj-si)^2 + M}
    12 //-> dp[k] - dp[j] + sk^2 - sj^2 
    13 //    --------------------------   > 2si 时 j比k优 
    14 //              sk - sj
    15 
    16 int Up(int i,int j){
    17     return dp[i]+sum[i]*sum[i]-(dp[j]+sum[j]*sum[j]);
    18 }
    19 
    20 int Down(int i,int j){
    21     return sum[i]-sum[j]; 
    22 }
    23 
    24 void DP(){
    25     int l=0,r=1;
    26     For(i,n){
    27         while(l+1<r && Up(q[l],q[l+1])>=2*sum[i]*Down(q[l],q[l+1])) l++;
    28         dp[i]=dp[q[l]] + (sum[i]-sum[q[l]])*(sum[i]-sum[q[l]]) + M;
    29         while(l+1<r && Up(q[r-2],q[r-1])*Down(q[r-1],i)>=Up(q[r-1],i)*Down(q[r-2],q[r-1])) --r;
    30         q[r++]=i;
    31     } 
    32     printf("%d
    ",dp[n]);
    33 }
    34 
    35 int main(){
    36     while(scanf("%d%d",&n,&M)!=EOF){
    37         q[0]=dp[0]=0;
    38         For(i,n){
    39             scanf("%d",&sum[i]);
    40             sum[i]+=sum[i-1];
    41         }
    42         DP();
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    [算法] 带权图
    哥德巴赫猜想 ——— 极限算法(你要是能写出比我用时还短的代码算我输)
    详解 位运算
    内存对齐模式 —— 原理讲解
    C语言 文件操作
    指针与数组
    队列的实现
    堆栈的实现
    线性表 详讲及使用
    树莓派
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3951107.html
Copyright © 2011-2022 走看看