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, 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
 
 
第一次写斜率优化,代码很丑。
斜率优化模式为dp[i]=min(a[j]*b[i]+c[j])+d[i]
则 dp[i]=a[j]*b[i]+c[j]+d[i]
  b[i]*a[j]+d[i]-dp[i]=c[j]
等价于 a*x  +    b       =y
而(x,y)已处理,可用log(n) 求出b的最值
与模板是式子本题f[i]=min(f[j]-2*sum[j]*sum[i]+sum[j]^2)+sum[i]^2+m相符。
另外,表示凸包写的还是很不熟练。
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define MAXN 510000
#define INF 0x3f3f3f3f
//AC
int n,m;
int num[MAXN];
typedef long long qword;
qword f[MAXN];
qword sum[MAXN];
inline qword sqr(int x)
{
return x*x;
}
//f[i]=min(f[j]-2*sum[j]*sum[i]+sum[j]^2)+sum[i]^2+m
//f[i]=-2*sum[j]*sum[i] + f[j]+sum[j]^2 +sum[i]^2 + m
// -2*sum[i]*sum[j] + sum[i]^2-f[i]+m == -f[j]-sum[j]^2
struct Point
{
qword x,y;
void init(qword xx,qword yy)
{
x=xx;y=yy;
}
};
Point make_point (qword x,qword y)
{
Point ret;
ret.init(x,y);
return ret;
}
qword xmul(Point p1,Point p2,Point p3)
{
return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
double get_k(Point p1,Point p2)
{
if (p1.x==p2.x)throw "error";
return (double)(p2.y-p1.y)/(p2.x-p1.x);
}
struct Convex_Hall
{
Point pl[MAXN];
double kk[MAXN];
int topl;
void clear()
{
topl=-;
} Convex_Hall()
{
topl=-;
}
void add_point(Point pp)
{
if (topl<)
{
pl[++topl]=pp;
if (topl==&&pl[topl].x==pl[topl-].x)
{
if (pl[topl].y<pl[topl-].y)
{
topl--;
return ;
}else
{
pl[topl--]=pp;
return ;
} }
if (topl==)
{
kk[topl-]=get_k(pl[topl-],pl[topl]);
}
return ;
}
while (topl>=&&xmul(pl[topl-],pl[topl],pp)>=)
{
topl--;
}
pl[++topl]=pp;
kk[topl-]=get_k(pl[topl-],pp);
}
void pm()
{
int i;
for(i=;i<=topl;i++)
{
printf("(%d,%d) ",pl[i].x,pl[i].y);
}
printf("\n");
}
double get_maxb(double k)
{
int l,r,mid;
if (topl==-)throw "Error";
if (topl==)return pl[].y-pl[].x*k;
if (k>kk[])return pl[].y-pl[].x*k;
if (k<kk[topl-])return pl[topl].y-pl[topl].x*k;
l=,r=topl;
while (l<r)
{
mid=(l+r)>>;
if (kk[mid-]>=k&&kk[mid]<=k)
{
return pl[mid].y-pl[mid].x*k;
}
if (kk[mid-]<k)
{
r=mid;
}else
{
l=mid;
}
}
}
}H; int main()
{
//freopen("input.txt","r",stdin);
int i;
while (~scanf("%d%d",&n,&m))
{
H.clear();
for (i=;i<=n;i++)
{
scanf("%d",&num[i]);
sum[i]=sum[i-]+num[i];
}
memset(f,INF,sizeof(f));
/* f[0]=0;
for (i=1;i<=n;i++)
{
for (j=0;j<i;j++)
{
if (f[j]>=INF)continue;
f[i]=min(f[i],f[j]+sqr(sum[i]-sum[j])+m);
}
}
for (i=1;i<=n;i++)cout<<f[i]<<" ";cout<<endl;
*/ f[]=;
H.add_point(make_point(sum[],-sum[]*sum[]-f[]));
double k,b;
for (i=;i<=n;i++)
{
k=-*sum[i];
b=H.get_maxb(k);
f[i]=sum[i]*sum[i]+m-ceil(b);
H.add_point(make_point(sum[i],-sum[i]*sum[i]-f[i]));
// cout<<f[i]<<" ";
}
cout<<f[n]<<endl;;
}
return ;
}

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

  1. hdu 3507 Print Article(斜率优化DP)

    题目链接:hdu 3507 Print Article 题意: 每个字有一个值,现在让你分成k段打印,每段打印需要消耗的值用那个公式计算,现在让你求最小值 题解: 设dp[i]表示前i个字符需要消耗的 ...

  2. hdu 3507 Print Article —— 斜率优化DP

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3507 设 f[i],则 f[i] = f[j] + (s[i]-s[j])*(s[i]-s[j]) + m ...

  3. HDU 3507 - Print Article - [斜率DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3507 Zero has an old printer that doesn't work well s ...

  4. HDU 3507 单调队列 斜率优化

    斜率优化的模板题 给出n个数以及M,你可以将这些数划分成几个区间,每个区间的值是里面数的和的平方+M,问所有区间值总和最小是多少. 如果不考虑平方,那么我们显然可以使用队列维护单调性,优化DP的线性方 ...

  5. ●HDU 3507 Print Article

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=3507 题解: 斜率优化DP 一个入门题,就不给题解了,网上的好讲解很多的.   这里就只提一个小问题吧( ...

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

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

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

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

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

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

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

    显然的斜率优化模型 但是单调队列维护斜率单调性的时候出现了莫名的锅orz 代码 #include <cstdio> #include <algorithm> #include ...

随机推荐

  1. qt中如果用qDebug输出彩色调试信息

    Linux:  在终端输出彩色信息有点类似于html的语法,即在要输出的文字前加上转义字符. 指令格式如下\033[*m 这里的*就是转义字符,例如我们要输出一段绿色的文字 qDebug(" ...

  2. 学习PHP时的一些总结(二)

    类中的构造方法和析构方法: 构造方法是对象创建完成后第一个被对象自动调用的方法.析构方法是对象在销毁之前最后一个被对象自动调用的方法. 如果没有显示的声明构造方法,类中都会默认存在一个没有参数列表并且 ...

  3. SOAP消息创建

    看了SOAP消息分析之后,大家对soap消息应该有了一个初步的认识,那么怎样自己编写一个soap消息呢? 先来创建一个简单的soap消息: @Test public void test1(){ try ...

  4. Java基础知识强化之IO流笔记24:FileInputStream / FileOutputStream 复制文本文件案例2

    1. 需求:把d盘下的a.txt的内容复制到f盘下的b.txt中: 代码示例: package com.himi.filecopy; import java.io.FileInputStream; i ...

  5. Android(java)学习笔记196:Android中Menu的使用(静态和动态)

    1.使用xml定义Menu(静态方法) 菜单资源文件必须放在res/menu目录中.菜单资源文件必须使用<menu>标签作为根节点.除了<menu>标签外,还有另外两个标签用于 ...

  6. DOS和UNIX文本文件之间相互转换的方法

    在Unix/Linux下可以使用file命令查看文件类型,如下: file dosfile.txt 使用dos2unix 一般Linux发行版中都带有这个小工具,只能把DOS转换为UNIX文件,命令如 ...

  7. spring 3.1.4 升 4.0.2

          来自为知笔记(Wiz)

  8. javascript通过字典思想操作数据

    作为一名前端程序猿,相对于后端操作数据的机会较少.然而,有些时候因为一些特殊的原因(如:需要构造成对应插件需要的数据格式,需要返回特定的数据格式等)而不得不对数据进行筛选.重构.相对于后端语言,我们没 ...

  9. Nico Game Studio 1.基本UI和地图编辑基础功能

    完成了基本界面. 本来想自画UI,但是考虑到工作量较大和美观程度有限,以及工具使用对象是比较初级玩家,处于性价比和最初目的,放弃了自绘.

  10. [序列化] Serialize--序列化帮助类 (转载)

    点击下载 Serialize.zip 这个类是关于加密,解密的操作,文件的一些高级操作1.序列化2.要序列化的类3.序列化例子看下面代码吧 /// <summary> /// 类说明:As ...