HDU 3507:Print Article

HDU 3507:Print Article

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3507

题目大意:给定$n$,$m$,输出序列$n$个数,每连续输出代价为连续输出的数字和的平方加上$m$.

斜率优化DP

定义$sum_{pq}=\sum_{k=p+1}^qa_k=pre[q]-pre[p]$,其中$pre[]$维护的是前缀和.

直接DP:$dp[i]=min\{dp[q]+sum_{qi}+m\}(q<i)$,如此复杂度为$O(n^2)$,故需要优化.

设$p<q$,若从$q$转移而来比从$p$更优,则有$dp[q]+sum_{qi}+m<dp[p]+sum_{pi}+m$,

化简可得,$\frac{(dp[q]+pre[q]^2)-(dp[p]+pre[p]^2)}{2pre[q]-2pre[p]}<pre[i]$.

令$y(k)=dp[k]+pre[k]^2$,$x(k)=2pre[k]$,

则上式可写为$\frac{y(q)-y(p)}{x(q)-x(p)}<pre[i]$.

其中,$\frac{y(q)-y(p)}{x(q)-x(p)}$可看做斜率,定义$g(p,q)=\frac{y(q)-y(p)}{x(q)-x(p)}$,

故有,若$g(p,q)<pre[i]$,则$q$比$p$更优.

待更...

代码如下:

 1 #include <cstdio>
 2 #define N 500005
 3 using namespace std;
 4 typedef long long ll;
 5 int n,m,dp[N],a[N],pre[N],deq[N],r,f;
 6 int y(int n){return dp[n]+pre[n]*pre[n];}
 7 int x(int n){return 2*pre[n];}
 8 int main(void){
 9     while(~scanf("%d%d",&n,&m)){
10         r=-1,f=0;
11         deq[++r]=0;
12         for(int i=1;i<=n;++i){
13             scanf("%d",&a[i]);
14             pre[i]=pre[i-1]+a[i];
15         }
16         for(int i=1;i<=n;++i){
17             while(r-f>0){
18                 if(y(deq[f+1])-y(deq[f])
19                    <=pre[i]*(x(deq[f+1])-x(deq[f])))
20                     f++;
21                 else break;
22             }
23             dp[i]=dp[deq[f]]+(pre[i]-pre[deq[f]])*(pre[i]-pre[deq[f]])+m;
24             while(r-f>0){
25                 if((y(deq[r])-y(deq[r-1]))*(x(i)-x(deq[r]))
26                     >=(y(i)-y(deq[r]))*(x(deq[r])-x(deq[r-1])))
27                     r--;
28                 else break;
29             }
30             deq[++r]=i;
31         }
32         printf("%d\n",dp[n]);
33     }
34 }
时间: 2024-11-26 14:44:45

HDU 3507:Print Article的相关文章

HDU3507:Print Article(斜率优化dp)

传送门 题意: 现有\(n\)个数,每个数的值为\(a_i\),现在可以把数划分为多段,每一段的代价为\((\sum_{k=i}^{j}c_i)^2+M\). 问怎么划分,代价最小. 思路: 考虑dp,那么dp式子很简单: \[ dp(i)=min\{dp(j)+(S_i-S_j)^2+M\} \] 注意这种\(dp\)形式,后面加上的部分与\(i,j\)两个变量有关,这种一般可以考虑分离变量然后斜率dp优化. (PS.如果可以分为多个部分,每个部分只和一个有关,那么可以考虑单调队列优化.fro

DP(斜率优化):HDU 3507 Print Article

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 8199    Accepted Submission(s): 2549 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique

斜率优化dp简讲 &amp;&amp; HDU 3507 Print Article

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 t

HDU 3507 Print Article (斜率优化)

HDU 3507 Print Article (斜率优化) ACM 题目地址: HDU 3507 Print Article 题意: 给定一个长度为n的序列,和一个常数m,我们可以将序列分成随意段,每段的权值为sum(arr[i]) + C(x<=i<=y),求一种划分方法使得整个序列的权值最小 分析: from:亟隐's blog f[i]=min(f[k]+(sum(i)-sum(k))^2 )+m = f[k]+sum^2(i)+sum^2(k)-2*sum(i)*sum(k)+m. 也

HDU 3507 Print Article 斜率优化

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 4810    Accepted Submission(s): 1451 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique

Print Article hdu 3507 一道斜率优化DP 表示是基础题,但对我来说很难

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 4990    Accepted Submission(s): 1509 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique

HDU 3507 Print Article(斜率优化)

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 15536    Accepted Submission(s): 4813 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antiqu

HDU 3507 Print Article(DP+斜率优化)

 Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7960    Accepted Submission(s): 2465 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antiq

HDOJ 3507 Print Article

斜率优化DP Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 5519    Accepted Submission(s): 1707 Problem Description Zero has an old printer that doesn't work well sometimes. As it is