zoukankan      html  css  js  c++  java
  • HDU 3507 斜率优化dp

    Print Article

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


    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
     
    题意:给你n个数以及M   将n个数连续分成若干部分求 和的最小值
    题解:d[i]=dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
    假设k<j<i 从j处分开比从k处分开更优则可以得到
    dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j])<dp[k]+m+(sum[i]-sum[k])*(sum[i]-sum[k]);
    =>(dp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]))/2*(sum[j]-sum[k])<=sum[i] //演算一下
    右侧 sum[i]是前缀和 是递增的
    左侧 恒小于右式才能更新dp[i]   维护一个下凸包
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 #include <stack>
     7 #include <queue>
     8 #include <cmath>
     9 #include <map>
    10 #define ll __int64
    11 using namespace  std;
    12 int dp[500005];
    13 int q[500005];
    14 int sum[500005];
    15 int head,tail,n,m;
    16 int getDP(int i,int j)
    17 {
    18     return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
    19 }
    20 int getUP(int j,int k)
    21 {
    22     return dp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]);
    23 }
    24 int getdown(int j,int k)
    25 {
    26     return 2*(sum[j]-sum[k]);
    27 }
    28 int main()
    29 {
    30     while(scanf("%d %d",&n,&m)==2)
    31     {
    32         for(int i=1; i<=n; i++)
    33             scanf("%d",&sum[i]);
    34         sum[0]=dp[0]=0;
    35         for(int i=1; i<=n; i++)
    36             sum[i]+=sum[i-1];
    37         head=tail=0;
    38         q[tail++]=0;
    39         for(int i=1; i<=n; i++)
    40         {
    41             while(head+1<tail && getUP(q[head+1],q[head])<=sum[i]*getdown(q[head+1],q[head]))
    42                 head++;
    43             dp[i]=getDP(i,q[head]);
    44             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]))
    45                 tail--;
    46             q[tail++]=i;
    47         }
    48         printf("%d
    ",dp[n]);
    49     }
    50     return 0;
    51 }
     
  • 相关阅读:
    五险一金的详细解释
    Android源码下载和编译Tips
    C++ STL遍历map的时候如何删除其中的element
    Android 2.3.4 RTSP的实现不在StageFright中,在opencore中
    找不到显示桌面的快捷方式怎么办|显示桌面的快捷方式找不到解决方法|显示桌面代码|
    选择适合过一辈子的人
    .net兼职人员| .net兼职系统开发人员| .net兼职开发人员
    skype帐号|超值skype帐号|14分钟skype账号|1元40个|5毛20个|15天有效期
    黄金市场的时间段分析
    解决导航问题winform的左侧树控件右侧panel加载用户控件
  • 原文地址:https://www.cnblogs.com/hsd-/p/6413559.html
Copyright © 2011-2022 走看看