题意:https://www.luogu.com.cn/problem/P3195

思路:https://www.luogu.com.cn/problemnew/solution/P3195

 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr strcat
#include <string>
#include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
#include <cassert>
#include <iomanip>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
clock_t __START,__END;
double __TOTALTIME;
void _MS(){__START=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
const double E=2.718281828;
const double PI=acos(-1.0);
const ll INF=(1LL<<);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; int n,L;
int a[N];
db dp[N],sum[N];
db A(int x)
{
return sum[x]+x;
}
db B(int x)
{
return sum[x]+x+L+;
}
db X(int x)
{
return B(x);
}
db Y(int x)
{
return dp[x]+B(x)*B(x);
}
double slope(int a,int b)
{
return 1.0*(Y(a)-Y(b))/(1.0*(X(a)-X(b)));
} class mydeque{
public:
int len=;
int p[N];
int l,r,tot;
void init()
{
l=;r=;
tot=;
}
int size()
{
return tot;
}
int pos(int x)
{
return (x+len)%len;
}
int f()
{
return p[l];
}
int b()
{
return p[r];
}
void pf(int x)
{
tot++;
l=pos(l-);
p[l]=x;
}
void pb(int x)
{
tot++;
r=pos(r+);
p[r]=x;
}
void popf()
{
p[l]=;
l=pos(l+);
tot--;
}
void popb()
{
p[r]=;
r=pos(r-);
tot--;
}
}q; int main()
{
q.init();//初始化有dp[0](l=r=1而不是l=1,r=0,tot=0),因为要用到
sc("%d%d",&n,&L);
for(int i=;i<=n;++i)sc("%d",&a[i]),sum[i]=a[i]+sum[i-];
for(int i=;i<=n;++i)
{
while(q.l<q.r&&slope(q.f(),q.p[q.pos(q.l+)])<2.0*A(i))q.popf();
dp[i]=dp[q.f()]+(A(i)-B(q.f()))*(A(i)-B(q.f()));
while(q.l<q.r&&slope(q.p[q.pos(q.r-)],q.b())>slope(q.p[q.pos(q.r-)],i))q.popb();
q.pb(i);
}
pr("%lld\n",(ll)dp[n]);
return ;
} /**************************************************************************************/

DP单调队列--斜率优化P3195的更多相关文章

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

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

  2. 单调队列 && 斜率优化dp 专题

    首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...

  3. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)

    DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...

  4. 3622 假期(DP+单调队列优化)

    3622 假期 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 经过几个月辛勤的工作,FJ决定让奶牛放假.假期可以在1-N天内任意选择 ...

  5. DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)

    codevs 1748 瑰丽华尔兹 2005年NOI全国竞赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Descripti ...

  6. [acmm week12]二分+dp+单调队列

    1004 抄作业         Time Limit: 1sec    Memory Limit:256MB Description Zfree虽然平时很爱学习,但是他迫于生活所迫(比如设计cpu实 ...

  7. 习题:烽火传递(DP+单调队列)

    烽火传递[题目描述]烽火台又称烽燧,是重要的防御设施,一般建在险要处或交通要道上.一旦有敌情发生,白天燃烧柴草,通过浓烟表达信息:夜晚燃烧干柴,以火光传递军情.在某两座城市之间有n个烽火台,每个烽火台 ...

  8. DP+单调队列详解+题目

    介绍: 单调队列优化的原理   先回顾单调队列的概念,它有以下特征:   (1)单调队列的实现.用双端队列实现,队头和队尾都能插入和弹出.手写双端队列很简单.   (2)单调队列的单调性.队列内的元素 ...

  9. (noip模拟二十一)【BZOJ2500】幸福的道路-树形DP+单调队列

    Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的小T发现可以用树来描绘这个草图. ...

随机推荐

  1. centos7下用kubeadm安装k8s集群并使用ipvs做高可用方案

    1.准备 1.1系统配置 在安装之前,需要先做如下准备.三台CentOS主机如下: 配置yum源(使用腾讯云的) 替换之前先备份旧配置 mv /etc/yum.repos.d/CentOS-Base. ...

  2. Po类设计

    0.承接MySQL 表设计,同样地,这篇博客中一部分内容是Deolin的个人观点和习惯. 1.一般Po类的域是和DB表字段一一对应的, 而由于每个信息表和关联表都有id.insert_time.upd ...

  3. python 安装 pyHook

    下载网站:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyhook 查看python版本 C:\Users\k\Desktop\file_trans> ...

  4. JavaWeb_(Mybatis框架)输入和输出参数_五

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  5. radio自带回显和默认选中

    <input type="radio" name="state" <c:if test="${empty model.state || m ...

  6. 谈谈Java对象的强引用,软引用,弱引用,虚引用分别是什么

    整体结构 java提供了4中引用类型,在垃圾回收的时候,都有自己的各自特点. 为什么要区分这么多引用呢,其实这和Java的GC有密切关系. 强引用(默认支持模式) 把一个对象赋给一个引用变量,这个引用 ...

  7. Leetcode题目283.移动零(简单)

    题目描述: 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原 ...

  8. Win10 更新出现问题,建议完全重置系统

    语言包引起问题.   文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论

  9. 指定版本下载yum离线安装包

    #!/bin/bash releasever=7 for i in salt-minion salt-api salt-master docker-ce docker-ce-cli docker-co ...

  10. 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_17-异常处理-可预知异常处理-异常处理测试

    ResultCode有很多的实现类 cmsCode有很多类型的错误异常 pgeService内当CmsPage这个对象为空的时候,异常类型就可以抛出CmsCode里面的错误代码 if(cmsPage1 ...