hdu3507Print Article(斜率优化dp)
2024-09-07 16:26:51
Print Article
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 12824 Accepted Submission(s):
3967
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.
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.
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.
article.
Sample Input
5 5
5
9
5
7
5
5
9
5
7
5
Sample Output
230
Author
Xnozero
Source
Recommend
斜率优化dp学习:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html
#include<iostream>
#include<cstdio>
#include<cstring> #define N 500005 using namespace std;
int dp[N],q[N],sum[N];
int head,tail,n,m; int get_dp(int i,int j)
{
return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
} int get_up(int j,int k)
{
return dp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]);
} int get_down(int j,int k)
{
return *(sum[j]-sum[k]);
} int main()
{
while(scanf("%d%d",&n,&m)==)
{
for(int i=;i<=n;i++) scanf("%d",&sum[i]);
sum[]=dp[]=;head=tail=;
for(int i=;i<=n;i++) sum[i]+=sum[i-];
q[tail++]=;
for(int i=;i<=n;i++)
{
while(head+<tail && get_up(q[head+],q[head])<=sum[i]*get_down(q[head+],q[head]))
head++;
dp[i]=get_dp(i,q[head]);
while(head+<tail && get_up(i,q[tail-])*get_down(q[tail-],q[tail-])<=get_up(q[tail-],q[tail-])*get_down(i,q[tail-]))
tail--;
q[tail++]=i;
}
printf("%d\n",dp[n]);
}
return ;
}
最新文章
- Web开发中管理ipad屏幕的方向变化
- 前端开发根据url进行页面跳转控制以及实现菜单栏手风琴效果
- 比对两个同类型的List
- MySQL乱码的几种原因
- Linux Kernel sys_call_table、Kernel Symbols Export Table Generation Principle、Difference Between System Calls Entrance In 32bit、64bit Linux
- iOS 利用self.navigationItem.backBarButtonItem修改后退按钮文字
- JavaScript学习心得(四)
- pd的django To do list教程-----(2)models模型的建立
- CDC变更数据捕获
- 项目关联不上开源项目(library)
- IPv4头部结构具体解释
- 【Node.js 自己封装的库 http_parse, libuv】
- Ural Vol1(dif>;=900)
- windows下搭建Nexus3私服和基于IDEA15的Maven学习笔记
- Java生成全局唯一ID代码演示
- 【转】使用Mybatis时遇到的延迟加载造成返回异常的问题——HttpMessageConversionException: Type definition error
- ZOJ1363 Chocolate 【生成函数】 【泰勒展开】
- F12搜索json内容
- ssm框架junit简单测试_我写
- 用js来实现那些数据结构及算法—目录
热门文章
- Tomcat8 连接池
- * 获取页面参数 * @return 参数打印
- ngFor 循环带索引
- vc++6.0创建console32之.c的应用程序详解
- 【webpack结合React开发环境配置】React开发环境配置之Webpack结合Babel8.x版本安装的正确姿势(Webpack最新版4.x结合Babel8.x环境配置步骤)
- Use emcli to delete obsolete agent targets in Oracle EM Cloud Control 12c
- poj3176-Cow Bowling【dp】
- collections、random、hashlib、configparser、logging模块
- postgres主从配置
- 【codeforces 767B】The Queue