参考博文:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html//讲的真的很好,有个小错误,博客里的num全为sum,像我这种弱渣都听懂了。真心点赞!!!

Print Article

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

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
解释:    
 
 题解:
     1,用一个单调队列来维护解集。

2,假设队列中从头到尾已经有元素a b c。那么当d要入队的时候,我们维护队列的上凸性质,即如果g[d,c]<g[c,b],那么就将c点删除。直到找到g[d,x]>=g[x,y]为止,并将d点加入在           该位置中。

3,求解时候,从队头开始,如果已有元素a b c,当i点要求解时,如果g[b,a]<sum[i],那么说明b点比a点更优,a点可以排除,于是a出队。最后dp[i]=getDp(q[head])。

  代码:

 #include<cstdio>
#include<cmath>
#include<iostream>
#define maxn 500100 using namespace std; struct get
{
int n,m,sum[maxn],a[maxn],dp[maxn],q[maxn];
int getup(int j,int k){return dp[j]+sum[j]*sum[j]-dp[k]-sum[k]*sum[k];}//分子
int getdown(int j,int k){return *sum[j]-*sum[k];}//分母
int getdp(int i,int j) {return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);}//dp[i]
get()
{
while (scanf("%d%d",&n,&m)==)
{
for(int i=;i<=n;i++) scanf("%d",&sum[i]);
sum[]=dp[]=;
for(int i=;i<=n;i++)sum[i]+=sum[i-];
int t=,w=;
for (int i=;i<=n;i++)
{
while (t+<w&&sum[i]*getdown(q[t+],q[t])>=getup(q[t+],q[t])) t++;//维护队列,删除就点之前所有点
dp[i]=getdp(i,q[t]);
while (t+<w&&getup(i,q[w-])*getdown(q[w-],q[w-])<=getup(q[w-],q[w-])*getdown(i,q[w-])) w--;//维护队列,保证队列具有 上凸性质。
q[w++]=i;
}
printf("%d\n",dp[n]);
}
}
}get;
int main()
{
get;
return ;
}
 顺便吐槽一下,

Presentation Error 这种错误你们见过么?我也是醉了!

Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  3506 3501 3504 3505 3498 
 

最新文章

  1. 网易云音乐PC端刷曲快捷键
  2. C++ VS2012 内存泄露检测
  3. 解决passwd 为普通用户设密码 不成功的方法
  4. 流式大数据处理的三种框架:Storm,Spark和Samza
  5. .htaccess更改目录下的默认主页
  6. Web安全开发注意事项
  7. Linux 网络编程六(socket通信UDP版)
  8. Hibernate 一对多自身双向关联关系 用于类别表的实现
  9. adb server is out of date.
  10. Objective中的协议(Protocol)
  11. 【机器学习算法-python实现】KNN-k近邻算法的实现(附源代码)
  12. Professional C# 6 and .NET Core 1.0 - What’s New in C# 6
  13. Azure Powershell获取指定订阅下的虚拟机信息(ASM)
  14. R语言︱画图
  15. RTMPdump(libRTMP) 源代码分析 6: 建立一个流媒体连接 (NetStream部分 1)
  16. luogu P5324 [BJOI2019]删数
  17. Linux下安装Anaconda
  18. [Swift]LaunchScreen.storyboard设置启动页
  19. 【Spark 深入学习 01】 Spark是什么鬼?
  20. JOBDU 题目1131:合唱队形

热门文章

  1. 高级 Synth
  2. 删除下标为n的数组值
  3. [Virtualization][SDN] VXLAN到底是什么 [转]
  4. HBase的数据迁移(含HDFS的数据迁移)
  5. JS中数组对象去重
  6. Asp.Net ORM
  7. 设计模式:中介者模式(Mediator)
  8. 用facebook账号登陆到你的Magento网店
  9. Android笔记: Android版本号
  10. 【C++】运算符重载