BZOJ2216 [Poi2011]Lightning Conductor 【决策单调性dp】
题目链接
题解
学过高中数学都应知道,我们要求\(p\)的极值,参变分离为
\]
实际上就是求\(h_j + sqrt{|i - j|} - h_i\)的最大值
就可以设\(f[i]\)表示对\(i\)最大的该式的值
绝对值通常要去掉,一般可以通过方向性,我们只需每次转移时令\(i > j\),正反转移两次即可
现在式子变为
\]
发现\(\sqrt{i - j}\)依旧无法处理,无法展开使用我们喜闻乐见的斜率优化
此时就可以考虑这个式子是否具有决策单调性
我们考虑对于\(i'<i\),我们的决策为\(h_j + sqrt{i' - j}\)
那么对于\(forall k < j\)有\(h_k + sqrt{i' - k} < h_j + sqrt{i' - j}\)
现在我们用\(i\)替换\(i'\)
式子变为\(h_k + sqrt{i - k}\)和\(h_j + sqrt{i - j}\)
\(h_k\)和\(h_j\)是没有变化的,如果\(sqrt{i - j}\)的增长比\(sqrt{i - k}\)的增长要快,我们就可认定\(i\)替换\(i'\)后,\(k\)依旧无法作为最优决策
考虑函数
\]
\]
显然当\(x\)越大增长率越慢,而\(i' - k > i' - j\),\(\sqrt{i - j}\)的增长的确比\(\sqrt{i - k}\)的增长要快
得证
所以用队列维护三元组优化即可
复杂度\(O(nlogn)\)
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 500005,maxm = 100005;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
double f[maxn],h[maxn];
int n,head,tail,ans[maxn];
struct tri{int l,r,pos;}q[maxn << 1];
inline double cal(int i,int j){
return h[j] + sqrt(i - j) - h[i];
}
inline bool check(int pos,int i,int j){
return cal(pos,i) >= cal(pos,j);
}
void work(){
q[head = tail = 0] = (tri){1,n,1};
tri u;
for (int i = 1; i <= n; i++){
ans[i] = max(ans[i],(int)ceil(cal(i,q[head].pos)));
q[head].l++;
if (q[head].l > q[head].r) head++;
while (head <= tail){
u = q[tail--];
if (!check(u.r,i,u.pos)){
q[++tail] = u;
if (u.r + 1 <= n) q[++tail] = (tri){u.r + 1,n,i};
break;
}
if (check(u.l,i,u.pos)){
if (head > tail){
q[++tail] = (tri){i + 1,n,i};
break;
}
continue;
}
else {
int l = u.l,r = u.r,mid;
while (l < r){
mid = l + r >> 1;
if (check(mid,i,u.pos)) r = mid;
else l = mid + 1;
}
q[++tail] = (tri){u.l,l - 1,u.pos};
q[++tail] = (tri){l,n,i};
break;
}
}
}
}
int main(){
n = read();
for (int i = 1; i <= n; i++) h[i] = read();
work();
reverse(h + 1,h + 1 + n);
reverse(ans + 1,ans + 1 + n);
work();
for (int i = n; i; i--)
printf("%d\n",ans[i]);
return 0;
}
BZOJ2216 [Poi2011]Lightning Conductor 【决策单调性dp】的更多相关文章
- LOJ2074/2157 JSOI2016/POI2011 Lightning Conductor 决策单调性DP
传送门 我们相当于要求出\(f_i = \max\limits_{j=1}^{n} (a_j + \sqrt{|i-j|})\).这个绝对值太烦人了,考虑对于\(i>j\)和\(i<j\) ...
- 【BZOJ2216】[Poi2011]Lightning Conductor 决策单调性
[BZOJ2216][Poi2011]Lightning Conductor Description 已知一个长度为n的序列a1,a2,...,an.对于每个1<=i<=n,找到最小的非负 ...
- P3515 [POI2011]Lightning Conductor[决策单调性优化]
给定一序列,求对于每一个$a_i$的最小非负整数$p_i$,使得$\forall j \neq i $有$ p_i>=a_j-a_i+ \sqrt{|i-j|}$. 绝对值很烦 ,先分左右情况单 ...
- 洛谷 P3515 [ POI 2011 ] Lightning Conductor —— 决策单调性DP
题目:https://www.luogu.org/problemnew/show/P3515 决策单调性... 参考TJ:https://www.cnblogs.com/CQzhangyu/p/725 ...
- BZOJ_2216_[Poi2011]Lightning Conductor_决策单调性
BZOJ_2216_[Poi2011]Lightning Conductor_决策单调性 Description 已知一个长度为n的序列a1,a2,...,an. 对于每个1<=i<=n, ...
- BZOJ2216 Poi2011 Lightning Conductor 【决策单调性优化DP】
Description 已知一个长度为n的序列a1,a2,...,an. 对于每个1<=i<=n,找到最小的非负整数p满足 对于任意的j, aj < = ai + p - sqrt( ...
- BZOJ2216: [Poi2011]Lightning Conductor(DP 决策单调性)
题意 题目链接 Sol 很nice的决策单调性题目 首先把给出的式子移项,我们要求的$P_i = max(a_j + \sqrt{|i - j|}) - a_i$. 按套路把绝对值拆掉,$p_i = ...
- bzoj2216: [Poi2011]Lightning Conductor(分治决策单调性优化)
每个pi要求 这个只需要正反DP(?)一次就行了,可以发现这个是有决策单调性的,用分治优化 #include<iostream> #include<cstring> #incl ...
- BZOJ2216 : [Poi2011]Lightning Conductor
$f[i]=\max(a[j]+\lceil\sqrt{|i-j|}\rceil)$, 拆开绝对值,考虑j<i,则决策具有单调性,j>i同理, 所以可以用分治$O(n\log n)$解决. ...
随机推荐
- HTTP 两种基本请求方法 GET和 POST的区别
GET方法 1.GET交互方式是从服务器上获取数据,而并非修改数据,所以GET交互方式是安全的.就像数据库查询一样,从数据库查询数据,并不会影响数据库的数据信息,对数据库来说,也就是安全的.2.GET ...
- Ansible开发之路
一.初识Ansible 链接:https://www.cnblogs.com/baishuchao/articles/9164083.html 二.Ansible的架构 链接:https://www. ...
- Siki_Unity_2-2_NGUI_UI插件学习(3.6.8版本)(未学)
Unity 2-2 NGUI UI插件学习(3.6.8版本)(未学)
- HWI的安装
一.安装的过程 hwi的安装过程: 1.解压src源码包:tar -zvxf apache-hive-1.2.2-src.tar.gz 2.进到HWI目录下:cd /home/bigdata/apac ...
- 如何开发一个 PyCharm 插件
PyCharm 是很多 Python 开发者优先选择的 IDE,功能强大,跨平台,提供免费社区版,非常良心.如果你想自己给PyCharm添加一些功能怎么办呢?有两个办法: 通过提需求实现,到 JetB ...
- spark的运行方式——转载
本文转载自: spark的运行方式 本文主要讲述运行spark程序的几种方式,包括:本地测试.提交到集群运行.交互式运行 等. 在以下几种执行spark程序的方式中,都请注意master的设 ...
- react native中state和ref的使用
react native中state和ref的使用 因props是只读的,页面中需要交互的情况我们就需要用到state. 一.如何使用state 1:初始化state 第一种方式: construct ...
- Opendarlight Carbon 安装
写在前面 目前最轻松的一次安装过程,感谢大翔哥的帮助. 安装过程 1.Zip包下载 找到Opendaylight官网,进入下载界面找到Carbon版本并下载. 2.Zip包解压 把这个zip压缩包解压 ...
- DFS(DP)---POJ 1014(Dividing)
原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两 ...
- 使用git提交代码的一些小心得
1.不进行push不能运行的代码,如果需要提交,可以先注释,保证其他人pull时,可以得到能够正常使用的代码 2.每做完一件事,写一条描述,一次提交.不要等写了一堆代码,然后写一堆描述,这样如果需要查 ...