题意: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. vue-cli3运行本地项目后,端口不随设置的随便变化

    今天群里有个端友说到了这个,没当回事,devserver中虽然设置了端口,但是启动本地项目后,端口还是随便更换,网上回去初始化了一下项目,结果也遇到这情况了,刚好,我们只需要 npm install ...

  2. UVALive 3716 DNA Regions ——(式子变形)

    一开始直接想到了二分,写了一发然后过了全部样例就交了,果断WA.因为这个问题显然是不满足单调性的. 然后想之前刚做的斜率优化DP,但是那个是求斜率最大值,不是求满足斜率大于一定值的最大长度的.也构造不 ...

  3. 2016"百度之星" - 初赛(Astar Round2A)1001 All X(HDU5690)——找循环节|快速幂

    一个由m个数字x组成的新数字,问其能否mod k等于c. 先提供第一种思路,找循环节.因为每次多一位数都是进行(t*10+x)mod k(这里是同余模的体现),因为x,k都确定,只要t再一样得到的答案 ...

  4. 为win10下的linux子系统终端添加powerline

    一切按照上一篇完成,如果成功了,你厉害了我的哥,如果不成功,win10安装powerline字体才可以,才可以,才可以 sudo apt install build-essential cmake g ...

  5. IdentityServer4入门二

    在 IdentityServer4入门一 我们准备好了一个认证的服务端,这里做一个需要保护的API服务 首先,向解决方案新增一个项目.我们同样使用入门一的方式新增一个asp.net core Web程 ...

  6. vue 中引入第三方js库

    以 jQuery 为例 一.绝对路径直接引入,全局可用 主入口页面 index.html 中用 script 标签引入: <script src="./static/jquery-1. ...

  7. Vue生命周期钩子函数加载顺序的理解

    Vue实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom.渲染→更新→渲染.卸载等一系列过程,我们称这是Vue的生命周期.通俗说就是Vue实例从创建到销毁的过程,就是生命周期 ...

  8. linux中read,write和recv,send的区别

    linux中read,write和recv,send的区别 1.recv和send函数提供了和read和write差不多的功能.但是他们提供了第四个参数来控制读写操作. int recv(int so ...

  9. iOS 11适配

    1.http://www.cocoachina.com/ios/20170915/20580.html   简书App适配iOS 11   2.http://www.jianshu.com/p/efb ...

  10. xampp 启动mysql 显示busy

    1.端口被占用问题:2.默认3306端口并没有被占用:3.删除xampp\mysql\data\下的ibdata1再重启:4.没装其他mysql.