在kuangbin巨巨博客上学的。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn = + ;
int d[maxn], Q[maxn], sum[maxn]; int head, tail;
int n, M; int inline dx(int i, int j)
{
return sum[j] - sum[i];
} int inline dy(int i, int j)
{
return d[j] + (sum[j]*sum[j]) - d[i] - (sum[i]*sum[i]);
} int inline DP(int i, int j)
{
return d[i] + (sum[j]-sum[i])*(sum[j]-sum[i]) + M;
} int main()
{
while(scanf("%d%d", &n, &M) == && n)
{
for(int i = ; i <= n; i++) scanf("%d", sum + i);
for(int i = ; i <= n; i++) sum[i] += sum[i - ]; head = tail = ;
Q[tail++] = ;
for(int i = ; i <= n; i++)
{
while(head + < tail && dy(Q[head], Q[head + ]) <= * sum[i] * dx(Q[head], Q[head + ])) head++;
d[i] = DP(Q[head], i);
while(head + < tail && dy(Q[tail-], i) * dx(Q[tail-], Q[tail-]) <= dy(Q[tail-], Q[tail-]) * dx(Q[tail-], i)) tail--;
Q[tail++] = i;
} printf("%d\n", d[n]);
} return ;
}

代码君

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

  1. HDU 3507 斜率优化dp

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

  2. HDU 3507斜率优化dp

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

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

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

  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. centos下svn的ldap认证配置

    前提:完成svn的基本安装 一.安装sasl相关组件 #yum install -y cyrus-sasl cyrus-sasl-lib cyrus-sasl-plain 二.查看SASL版本和提供的 ...

  2. how browser supports https

    1. pre-installed certificate authorities 2. ssl/tls encription ssl/tls handshake flow: 1. exchange d ...

  3. On the way to the park Gym - 101147I 几何

    http://codeforces.com/gym/101147/problem/I I. On the way to the park time limit per test 5 seconds m ...

  4. PHP使用Socket发送字节流

    例如,需要发送以下数据 struct header { int  type; // 消息类型 int  length; // 消息长度 } struct MSG_Q2R2DB_PAYRESULT { ...

  5. php出现Warning: file_put_contents,failed to open stream

    Warning: file_put_contents(D:/wwwroot/jinxiongdi/web/temp/caches/f/index_40F756F0.php) [function.fil ...

  6. 响应式Spring Cloud初探

    响应式Spring Cloud初探 分类:工程原文链接:The Road to Reactive Spring Cloud作者:  JOSH LONG译者: helloworldtang日期:JUNE ...

  7. Linux-软件安装(一) —— jdk/tomact 安装(普通安装)

    Linux-软件安装(一) -- jdk/tomact 安装(普通安装) 1. 可使用 FinalShell 上传至 Linux 服务器 2. 解压 cd /usr/local #解压命令 tar - ...

  8. Todolist总结

    一.组件类里面的函数尽可能写成箭头函数的形式,方便绑定this 上面的箭头函数是好的,写面的不好,他需要在用的时候绑定this,或者在constructor绑定,如下: 如上用的时候绑定this是不好 ...

  9. CF1066B Heaters

    思路: 从左向右贪心选择能覆盖当前位置的最靠右的那个heater即可,和poj radar installation类似. 实现: #include <iostream> #include ...

  10. Android Theme.Dialog 到光 AppCompatDialog

    我用在我的 style.xml 作为主要应用程序主题 <style name="AppTheme" parent="Theme.AppCompat.Light&qu ...