hdu_3507_Print Article
2016-04-21 23:18
260 查看
Print Article
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 8558 Accepted Submission(s): 2664
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
2010 ACM-ICPC Multi-University
Training Contest(7)——Host by HIT
Recommend
zhengfeng
斜率dp入门题,第一次接触,学习一下,kuangbin大神写得很好,膜拜~~~~~通过队列来维护一个下凹的图形,斜率逐渐增大。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <stack> #include <bitset> #include <queue> #include <set> #include <map> #include <string> #include <algorithm> #define Si(a) scanf("%d",&a) #define Sl(a) scanf("%lld",&a) #define Sd(a) scanf("%lf",&a) #define Ss(a) scanf("%s",a) #define Pi(a) printf("%d\n",(a)) #define Pl(a) printf("%lld\n",(a)) #define Pd(a) printf("%lf\n",(a)) #define Ps(a) printf("%s\n",(a)) #define W(a) while(a--) #define mem(a,b) memset(a,(b),sizeof(a)) #define FOP freopen("data.txt","r",stdin) #define inf 0x3f3f3f3f #define maxn 500010 #define mod 1000000007 #define PI acos(-1.0) #define LL long long using namespace std; int n,m; int sum[maxn]; int dp[maxn]; int q[maxn];//queue int getDP(int i,int j) { return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]); } 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 2*(sum[j]-sum[k]); } int main() { int i,j; while((scanf("%d%d",&n,&m))==2) { int val; sum[0]=dp[0]=0; for(i=1;i<=n;i++) { Si(val); sum[i]=sum[i-1]+val; } int head=0,tail=0; q[tail++]=0; for(i=1;i<=n;i++) { while(head+1<tail&&getUP(q[head+1],q[head])<=getDOWN(q[head+1],q[head])*sum[i]) head++; dp[i]=getDP(i,q[head]); 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])) tail--; q[tail++]=i; } Pi(dp ); } return 0; }
相关文章推荐
- Openfire安装部署及插件的开发
- 打赏最多的网红,必用这九种方法
- 【360面经】
- 小程序-随机数生成器
- React-Native系列Android——Native与Javascript通信原理(三)
- HashMap嵌套ArrayList
- SQL注入
- linux 搭建svn
- information_schema中Innodb相关表用于分析sql查询锁的使用情况介绍
- 分布式架构高可用架构篇_03-redis3集群的安装高可用测试
- R.java文件没有生成
- 软件体系结构
- Android线程的正确使用姿势
- C++第四次上机作业
- springmvc+ztree v3实现类似表单回显功能
- matlab 功率谱分析
- Python工具包安装
- 用户角色权限数据库设计实例
- Ubuntu15.10系统不能su root,认证失败
- python第三方安装包