当前位置:网站首页>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
边栏推荐
- Pytorch learning record (III): structure of neural network + using sequential and module to define the model
- 治療TensorFlow後遺症——簡單例子記錄torch.utils.data.dataset.Dataset重寫時的圖片維度問題
- MySQL best practices for creating tables
- Fact final variable and final variable
- How does MySQL convert stored seconds into dates
- 深度学习基础——简单了解meta learning(来自李宏毅课程笔记)
- Framework analysis 2 Source code - login authentication
- ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
- Fundamentals of digital image processing (Gonzalez) I
- Guaba and Computational Geometry
猜你喜欢
Contrôle automatique (version Han min)
Algèbre linéaire chapitre 1 - déterminants
20 excellent plug-ins recommended by idea
Explain of MySQL optimization
A sharp tool to improve work efficiency
检测技术与原理
Automatic control (Han min version)
Understanding and installing MySQL
In depth understanding of the relationship between dncblevel and noise denoising in the paper
自动控制原理知识点整合归纳(韩敏版)
随机推荐
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
LockSupport. Park and unpark, wait and notify
Protected (members modified by protected are visible to this package and its subclasses)
Create binary tree
自动控制原理知识点整合归纳(韩敏版)
[transfer] MySQL: how many rows of data can InnoDB store in a B + tree?
Customized communication between threads (reentrantlock)
Stability building best practices
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
lambda expressions
4. Print form
JDBC tool class encapsulation
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
Automatic control (Han min version)
C language file operation
Traitement des séquelles du flux de Tensor - exemple simple d'enregistrement de torche. Utils. Données. Dataset. Problème de dimension de l'image lors de la réécriture de l'ensemble de données
檢測技術與原理
线性代数第二章-矩阵及其运算
Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving
Example of reentrant lock thread waiting to wake up