题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3507

题目大意:给你n个数,然后问你怎么分割当前的这n个数位那几组,使得每一组的权值加起来最大。每一组权值的计算方法在题目上说了。

具体思路: 一开始打了一波两重for循环的,一直求一个tile,但是wa了。然后就学了斜率优化的方法

https://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html

就是把表达式转换成斜率类型的表达式,然后对于当前的节点,通过判断斜率的方式来决定当前的点是不是该选择,这个过程可以用单调队列维护。

AC代码:

 #include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
# define ll long long
const int maxn = 6e5+;
int sum[maxn];
int q[maxn];
int dp[maxn];
int n,m;
int getdp(int i,int j){
return dp[i]+m+(sum[j]-sum[i])*(sum[j]-sum[i]);
}
int getup(int i,int j){
return dp[i]+sum[i]*sum[i]-(dp[j]+sum[j]*sum[j]);
}
int getdown(int i,int j){
return *(sum[i]-sum[j]);
}
int main(){
while(~scanf("%d %d",&n,&m)){
for(int i=;i<=n;i++){
scanf("%d",&sum[i]);
sum[i]+=sum[i-];
}
int tail,head;
tail=head=;
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(q[head],i);
while(head+<tail&&getdown(q[tail-],q[tail-])*getup(q[tail-],i)<=getdown(q[tail-],i)*getup(q[tail-],q[tail-]))tail--;
q[tail++]=i;
}
printf("%d\n",dp[n]);
}
return ;
}

Print Article(斜率DP入门+单调队列)的更多相关文章

  1. hdu3507 Print Article(斜率DP优化)

    Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it ...

  2. 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 ...

  3. 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 题目大意: 给一棵树,n个节点,每条边有个权值,从每个点i出发有个不经过自己走过的点的最远距离 ...

  4. NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!

                                         Largest Rectangle in a Histogram 这么经典的题硬是等今天碰到了原题现场懵逼两小时才会去补题.. ...

  5. hdu3507 Print Article[斜率优化dp入门题]

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

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

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

  7. HDU3507 Print Article —— 斜率优化DP

    题目链接:https://vjudge.net/problem/HDU-3507 Print Article Time Limit: 9000/3000 MS (Java/Others)    Mem ...

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

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

  9. POJ 1180 斜率优化DP(单调队列)

    Batch Scheduling Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4347   Accepted: 1992 ...

随机推荐

  1. C++ template一些体悟(3)

    其实没啥体悟,因为还没有感受到这些例子的作用,记一下先 #include <iostream> using namespace std; class alloc { }; template ...

  2. Good Bye 2018 C. New Year and the Sphere Transmission

    传送门 https://www.cnblogs.com/violet-acmer/p/10201535.html 题意: n 个people,编号1~n,按顺时针方向围城一圈: 初始,编号为1的peo ...

  3. react一看就会的简单路由设置

    不管是vue还是react  这种单页面的框架一定都少不了路由 下面给大家讲讲在实际项目中react的路由设置 第一步: 在src目录下新建一个目录route  在该目录下新建一个index.js用于 ...

  4. echart折线区域图

    在引入echart区域折线图时,没有出现对应的区域图 当发现引入下面代码到自己的代码中并没有对应的区域图 option = { xAxis: { type: 'category', boundaryG ...

  5. maven+testng+eclipse

    1.安装maven 2.安装testng 3.配置maven的dependency,和build <project xmlns="http://maven.apache.org/POM ...

  6. 「Python」6种python中执行shell命令方法

    用Python调用Shell命令有如下几种方式: 第一种: os.system("The command you want"). 这个调用相当直接,且是同步进行的,程序需要阻塞并等 ...

  7. docker 基础之网络管理

    docker网络基础. docker使用到的与linux网络有关的主要技术 Network Namespace(网络命名空间) Veth设备对 Iptables/NetFilter 网桥 路由 标准的 ...

  8. Presto JVM.config

    Presto 如果启动时候 指定 CMS,那么 launcher run 会提示 G1 回收算法是 推荐的垃圾回收算法,针对 Presto 大内存 回收,G1 暂时 应该是最稳妥的选择,调整之后大约如 ...

  9. 开源实时消息推送系统 MPush

    系统介绍 mpush,是一款开源的实时消息推送系统,采用java语言开发,服务端采用模块化设计,具有协议简洁,传输安全,接口流畅,实时高效,扩展性强,可配置化,部署方便,监控完善等特点.同时也是少有的 ...

  10. JAVA核心技术I---JAVA基础知识(知识回顾)

    一:多态问题 class Father { public void hello() { System.out.println("Father says hello."); } } ...