当前位置:网站首页>D. Optimal partition segment tree optimization DP
D. Optimal partition segment tree optimization DP
2022-04-23 06:22:00 【Bzdhxs_ nt】
subject link
Reference resources
Ideas
consider f [ i ] f[i] f[i] For i i i The greatest value of the ending , s [ i ] s[i] s[i] a [ ] a[ ] a[] Prefix and array
Easy to think of
f [ i ] = { f [ j ] − j + i , s [ i ] > s [ j ] f [ j ] , s [ i ] = s [ j ] f [ j ] + j − i , s [ i ] < s [ j ] f[i]= \begin{cases} f[j]-j+i,\quad s[i] > s[j]\\ f[j],\quad s[i] = s[j]\\ f[j]+j-i,\quad s[i] < s[j] \end{cases} f[i]=⎩⎪⎨⎪⎧f[j]−j+i,s[i]>s[j]f[j],s[i]=s[j]f[j]+j−i,s[i]<s[j]
The time complexity is O ( n 2 ) O(n^2) O(n2)
Consider optimizing
The above formula is the solution
f [ i ] = m a x ( f [ j ] − j ) + i , s [ i ] > s [ j ] , j < i f[i] = max(f[j]-j) + i,s[i] > s[j],j < i f[i]=max(f[j]−j)+i,s[i]>s[j],j<i
f [ i ] = m a x ( f [ j ] ) , s [ i ] = s [ j ] , j < i f[i] = max(f[j]),s[i] = s[j],j < i f[i]=max(f[j]),s[i]=s[j],j<i
f [ i ] = m a x ( f [ j ] + j ) − i , s [ i ] < s [ j ] , j < i f[i] = max(f[j]+j) - i,s[i] < s[j],j < i f[i]=max(f[j]+j)−i,s[i]<s[j],j<i
Ready to use 3 3 3 A segment tree to maintain f [ j ] + j , f [ j ] , f [ j ] − j f[j]+j, f[j] , f[j]-j f[j]+j,f[j],f[j]−j The maximum of
use s [ i ] s[i] s[i] To represent the subscript of the line segment tree array because s [ i ] s[i] s[i] Great consideration is given to discretization s [ i ] s[i] s[i]
Pay attention to the following details
except 0 0 0 Outside f f f To initialize to − i n f -inf −inf
The tree size is n + 1 n+1 n+1 Because there is a prefix and less than 0 0 0, Therefore, it is necessary to maintain s [ 0 ] = 0 s[0] = 0 s[0]=0 Value ,
const int inf = 1e18;
const int INF = ~0ULL;
const int N = 500005;
struct tree{
int l,r;
int dat; // The information to be maintained in the segment tree ;
#define ls (i<<1)
#define rs (i<<1|1)
}t[3][N<<2];
int d;
int f[500005];
void up(int i){
t[d][i].dat = max(t[d][ls].dat,t[d][rs].dat);
}
void build(int i,int l,int r){
t[d][i] = {
l,r};
if(l==r){
t[d][i].dat = -inf;
return ;
}
int mid = (l+r) >> 1;
build(ls,l,mid);
build(rs,mid+1,r);
up(i);
}
void change(int i,int p, int k){
if(t[d][i].l == t[d][i].r){
t[d][i].dat = max(k,t[d][i].dat);
return;
}
int mid = (t[d][i].l + t[d][i].r) >> 1;
if(p <= mid) change(ls,p,k);
else change(rs,p,k);
up(i);
}
int ask(int i,int l,int r){
if(l <= t[d][i].l && r >= t[d][i].r){
return t[d][i].dat;
}
int res = -inf;
int mid = (t[d][i].l+t[d][i].r) >> 1;
if(l <= mid) res = max(res,ask(ls,l,r));
if(r > mid) res = max(res,ask(rs,l,r));
return res;
}
void solve(){
int n;cin>>n;
vector<int> a(n),s(n+1,0);
forr(i,0,n-1) cin>>a[i],s[i+1] = s[i]+a[i];
forr(i,1,n) f[i] = -inf;
f[0] = 0;
auto v = s;
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
for(int i = 0;i <= n;i++) s[i] = lower_bound(v.begin(),v.end(),s[i]) - v.begin()+1;
for(int i = 0; i < 3;i++){
d = i;
build(1,1,n+1);
}
for(int i = 0; i < 3;i++){
d = i;
change(1,s[0],0);
}
int res = -inf;
forr(i,1,n){
int p = s[i];
d = 0;
f[i] = max(f[i],ask(1,1,p-1)+i);
d = 1;
f[i] = max(f[i],ask(1,p,p));
d = 2;
f[i] = max(f[i],ask(1,p+1,n+1)-i);
d = 0;
change(1,p,f[i]-i);
d = 1;
change(1,p,f[i]);
d = 2;
change(1,p,f[i]+i);
}
cout << f[n] << endl;
}
signed main()
{
_orz;
int t;cin>>t;
while(t--) solve();
return 0;
}
版权声明
本文为[Bzdhxs_ nt]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617218940.html
边栏推荐
- Remedy after postfix becomes a spam transit station
- Framework analysis 2 Source code - login authentication
- 去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
- 2. Devops sonar installation
- Consistent hash algorithm used for redis cache load balancing
- Custom exception class
- Kibana search syntax
- Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
- container
- 去噪论文——[Noise2Void,CVPR19]Noise2Void-Learning Denoising from Single Noisy Images
猜你喜欢
![去噪论文——[Noise2Void,CVPR19]Noise2Void-Learning Denoising from Single Noisy Images](/img/9d/487c77b5d25d3e37fb629164c804e2.png)
去噪论文——[Noise2Void,CVPR19]Noise2Void-Learning Denoising from Single Noisy Images

Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()

深度学习基础——简单了解meta learning(来自李宏毅课程笔记)

container

Implementation of displaying database pictures to browser tables based on thymeleaf

Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering

檢測技術與原理

卡尔曼滤波与惯性组合导航

自動控制(韓敏版)
![去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots](/img/fd/84df62c88fe90a73294886642036a0.png)
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
随机推荐
POI and easyexcel exercises
7.Domino piling
Create binary tree
Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
PyTorch笔记——实现线性回归完整代码&手动或自动计算梯度代码对比
MySQL advanced query
Linear algebra Chapter 1 - determinant
Calculation (enter the calculation formula to get the result)
Problems and solutions of database migration
Why does the subscript of the array start from 0 instead of 1?
对比学习论文——[MoCo,CVPR2020]Momentum Contrast for Unsupervised Visual Representation Learning
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
Guaba and Computational Geometry
MySQL best practices for creating tables
JDBC operation transaction
Practical operation - Nacos installation and configuration
Viewer: introduce MySQL date function
Troubleshooting of data deleted and reappeared problems
lambda expressions
On traversal of binary tree