CF1768F Wonderful Jump

Description

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

Solution

知道结论了还不会,好菜。还是没有想到根号分治。

首先考虑放到笛卡尔树上,观察到如果 $j\to i$ 转移但是中间 $\exists k\in (i,j),a_k\le \min(a_i,a_j)$,则 $j\to i$ 一定不如 $j\to k\to i$ 优。所以一个点只会从左子树和祖先转移过来,向右子树和祖先转移过去。

再观察到值域 $a_i\le n$,记 $[j,i]$ 的最小值为 $x$,如果转移点 $x(j-i)^2\ge n(j-i)$ 那么一定不会转移。有 $j-i\le \dfrac{n}{x}$。

然后考虑根号分治并结合第一个性质。对于 $x\ge B$ 的直接暴力转移。而对于 $x<B$ 的,因为第一个性质,所以每个相同的 $x$ 的转移区间不交,分析一下是均摊 $O(n\sqrt n)$ 的。

实现上不需要写单调栈,直接往前转移和往后刷表即可。复杂度 $O(n\sqrt n)$。常数很小。

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
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define maxn 400005
#define put() putchar('\n')
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
using namespace std;
Tp void read(T &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
#define fi first
#define se second
#define mk make_pair
const int mod=1e9+7;
ll power(ll x,int y=mod-2) {
ll sum=1;x%=mod;
while (y) {
if (y&1) sum=sum*x%mod;
x=x*x%mod;y>>=1;
}
return sum;
}
int n;
ll a[maxn],f[maxn];
signed main(void){
int i,j;
read(n);
memset(f,0x3f,sizeof(f));
for (i=1;i<=n;i++) read(a[i]);
for (i=1;i<=n;i++) {
if (i==1) f[i]=0;
else {
int lim=n/a[i]+5;
for (j=i-1;j>=1&&j+lim>=i;j--) {
f[i]=min(f[i],f[j]+min(a[i],a[j])*(j-i)*(j-i));
if (a[j]<=a[i]) break;
}
}
int lim=n/a[i]+5;
for (j=i+1;j<=n&&j<=i+lim;j++) {
f[j]=min(f[j],f[i]+min(a[i],a[j])*(j-i)*(j-i));
// gdb(i,j,f[j]);
if (a[j]<=a[i]) break;
}
}
for (i=1;i<=n;i++) printf("%lld ",f[i]);put();
return 0;
}
//i=begin && g++ $i.cpp -o $i -std=c++14 && ./$i