决策单调性 => 二分队列:P3515

本文搬运自本人高中时期CSDN博客,若图片加载不出来,可到原文查看:https://blog.csdn.net/zhangtingxiqwq/article/details/135033351

https://www.luogu.com.cn/problem/P3515

pi=maxj=1n(aj+ij)aip_i=\max_{j=1}^n(a_j+\sqrt {|i-j|})-a_ipp 之间独立,直接拆绝对值,到时候reverse再做一遍即可。

拆绝对值后,显然具有决策单调性,因为 ij\sqrt {i-j}ii 为自变量,求导后为减函数。然后我们采用二分队列优化。

此题关键:

  1. 直接暴力表示,然后拆绝对值

  2. 看出来满足决策单调性(去根号影响!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 500010
struct node {
int x, l, r;
};
int n, m, i, j, k, T;
double a[N];
int p[N], p1[N], p2[N];
int l, r, mid;
deque<node>q;

double calc(int j, int i) {
return a[j]+sqrt((double)(i-j));
}

void work(int *p) {
while(!q.empty()) q.pop_back();
for(i=1; i<=n; ++i) {
if(!q.empty()) {
auto t = q.front(); q.pop_front();
t.l=i; if(t.l<=t.r) q.push_front(t);
}
node t; t.r=n; t.l=n+1; t.x=i;
if(i==1) t.l=i;
while(!q.empty() && calc(i, q.back().l) >= calc(q.back().x, q.back().l))
t.l=q.back().l, q.pop_back();
if(!q.empty() && calc(i, q.back().r)>=calc(q.back().x, q.back().r)) {
l=q.back().l; r=q.back().r; k=q.back().x;
while(l<r) {
mid=(l+r)>>1;
if(calc(i, mid)>=calc(k, mid)) r=mid;
else l=mid+1;
}
t.l=l;
auto t2=q.back(); q.pop_back();
t2.r=l-1; q.push_back(t2);
}
if(t.l<=t.r) q.push_back(t);
if(!q.empty()) {
p[i]=(int)ceil(calc(q.front().x, i));
// debug("%d -> %d %d\n", q.front().x, i, p[i]);
}
// debug("> %d")
}
}

signed main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// srand(time(NULL));
// T=read();
// while(T--) {
//
// }
n=read();
for(i=1; i<=n; ++i) a[i]=read();
work(p1);
reverse(a+1, a+n+1);
work(p2);
reverse(p2+1, p2+n+1);
reverse(a+1, a+n+1);
for(i=1; i<=n; ++i) p[i]=max(max(p1[i], p2[i]), 0ll)-(int)a[i];
for(i=1; i<=n; ++i) printf("%lld\n", p[i]);
return 0;
}