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]   维护一个下凸包
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
using namespace std;
int dp[];
int q[];
int sum[];
int head,tail,n,m;
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 *(sum[j]-sum[k]);
}
int main()
{
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-];
head=tail=;
q[tail++]=;
for(int i=; i<=n; i++)
{
while(head+<tail && getUP(q[head+],q[head])<=sum[i]*getdown(q[head+],q[head]))
head++;
dp[i]=getDP(i,q[head]);
while(head+<tail && getUP(i,q[tail-])*getdown(q[tail-],q[tail-])<=getUP(q[tail-],q[tail-])*getdown(i,q[tail-]))
tail--;
q[tail++]=i;
}
printf("%d\n",dp[n]);
}
return ;
}
 

HDU 3507 斜率优化dp的更多相关文章

  1. Print Article HDU - 3507 -斜率优化DP

    思路 : 1,用一个单调队列来维护解集. 2,假设队列中从头到尾已经有元素a b c.那么当d要入队的时候,我们维护队列的下凸性质, 即如果g[d,c]<g[c,b],那么就将c点删除.直到找到 ...

  2. HDU 3507斜率优化dp

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  3. HDU 3507 斜率优化 DP Print Article

    在kuangbin巨巨博客上学的. #include <iostream> #include <cstdio> #include <cstring> #includ ...

  4. hdu 3507 斜率优化

    我的第一道斜率优化. 就这道题而言,写出原始的方程: dp[i] = min{ dp[j] + (sum[i]-sum[j])2  + M | j in [0,i) } O(n^2)的复杂度肯定超时, ...

  5. hdu 3669(斜率优化DP)

    Cross the Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others) ...

  6. HDU 4258 斜率优化dp

    Covered Walkway Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. HDU 2829 斜率优化DP Lawrence

    题意:n个数之间放m个障碍,分隔成m+1段.对于每段两两数相乘再求和,然后把这m+1个值加起来,让这个值最小. 设: d(i, j)表示前i个数之间放j个炸弹能得到的最小值 sum(i)为前缀和,co ...

  8. hdu 3045 斜率优化DP

    思路:dp[i]=dp[j]+sum[i]-sum[j]-(i-j)*num[j+1]; 然后就是比较斜率. 注意的时这里j+t<=i: #include<iostream> #in ...

  9. HDU 3507 Print Article(斜率优化DP)

    题目链接 题意 : 一篇文章有n个单词,如果每行打印k个单词,那这行的花费是,问你怎么安排能够得到最小花费,输出最小花费. 思路 : 一开始想的简单了以为是背包,后来才知道是斜率优化DP,然后看了网上 ...

随机推荐

  1. 【checkbox-group、checkbox】 多项选择器组件说明

    checkbox-group组件包裹checkbox组件的容器 原型: <check-group bindchange="[EventHandle]"> <che ...

  2. redis 在java中的使用

    1.首先下载jar包放到你的工程中 2.练习 package com.jianyuan.redisTest; import java.util.Iterator;import java.util.Li ...

  3. JAVA基础学习之路(一)基本概念及运算符

    JAVA基础概念: PATH: path属于操作系统的属性,是系统用来搜寻可执行文件的路径 CALSSPATH: java程序解释类文件时加载文件的路径 注释: 单行注释  // 多行注释 /*... ...

  4. ionic LoadingController 模块使用

    html 代码: <ion-header> <ion-navbar> <ion-title>Loading</ion-title> </ion-n ...

  5. dotnetframe的清理工具

    微软的产品一向不敢恭维,卸载都没有办法卸载干净,卸载又慢又不彻底,dotnet被我卸载之后还有注册表残留以至于无法重新安装. .NET Framework Cleanup Tool真的很好用,全部版本 ...

  6. php多进程单例模式下的 MySQL及Redis连接错误修复

    前几天写了个php常驻脚本,主要逻辑如下 //跑完数据后休息60秒 $sleepTime = 60; $maxWorker = 10; while (true) { $htmlModel = new ...

  7. LeetCode 142——环形链表 II

    1. 题目 2. 解答 2.1 方法 1 定义快慢两个指针,慢指针每次前进一步,快指针每次前进两步,若链表有环,则快慢指针一定会相遇. 当快慢指针相遇时,我们让慢指针指向头节点,快指针不变,然后每次快 ...

  8. MATLAB画图符号标注

    线型 说明 标记符 说明 颜色 说明 - 实线(默认) + 加号符 r 红色 -- 双划线 o 空心圆 g 绿色 : 虚线 * 星号 b 蓝色 :. 点划线 . 实心圆 c 青绿色 x 叉号符 m 洋 ...

  9. Ext JS 6学习文档-第7章-图表

    Ext JS 6学习文档-第7章-图表 使用图表 本章中将探索在 ExtJS 中使用不同类型的图表并使用一个名为费用分析的示例项目结束本章所学.以下是将要所学的内容: 图表类型 条形图 和 柱形图 图 ...

  10. iOS开发SDWebImage源码解析之SDWebImageManager的注解

    最近看了两篇博客,写得很不错,关于SDWebImage源码解析之SDWebImageManager的注解: 1.http://www.jianshu.com/p/6ae6f99b6c4c 2.http ...