当前位置:网站首页>1D / 1D dynamic programming learning summary
1D / 1D dynamic programming learning summary
2022-04-23 09:43:00 【Zimba_】
Preface :
Since the discovery 1D/1D After dynamically planning this new skill block , Every time I see this kind of topic, I can say hi and succeed , Then my teammates urged me to learn . Furthermore, it is found that there are many optimization methods , So take your time , Learn a little every day , Plus, I have to do some strange questions to keep my state , That's about it .
The first 0 God (1D/1D Dynamic programming )
What is? 1D/1D Dynamic programming ?
1D/1D Dynamic programming It is shaped like d p i = m a x { d p j + w ( i , j ) } dp_{i}=max\{ dp_{j}+w(i,j)\} dpi=max{
dpj+w(i,j)} Dynamic planning of , Of course m a x max max It can also be replaced by m i n min min.
It is said that people who look like this d p dp dp Generally, it can be optimized to o ( n l o g n ) o(nlogn) o(nlogn).
The idea of optimization is to j j j In the range of , Quickly find an optimal transfer point .
The learning idea is probably Learn the different types of this transfer equation , Then learn its optimization method for each type .
Learning links ( Go for more walks , In this way, the author will not feel guilty after stealing knowledge , Blog will add their own things ( Maybe ))
The first 0.5 God ( Prefixes and optimizations )
There is one Prefixes and optimizations , The author defaults to , Don't learn . If not, please study by yourself .
The first 1 God ( Monotonic queue optimization )
ah ~ New power !
It's today Monotonic queue optimization dp, Of course , Just think everyone will be in a monotonous queue .
To be honest , After learning, I feel the original d p dp dp The formula is also a little inappropriate , We put d p j dp_{j} dpj Also regarded as w ( i , j ) w(i,j) w(i,j) in j j j Part of the operation , That formula is actually d p i = m a x { w ( i , j ) } dp_{i}=max\{ w(i,j)\} dpi=max{
w(i,j)}.
Okay , Let's get to the point .
According to our learning ideas , We should be for such optimizations , There is an inductive transfer formula , Then there is .
Transfer equation
d p i = m a x { w ( j ) } dp_{i}=max\{ w(j)\} dpi=max{ w(j)}
explain
This equation means d p dp dp The transfer of is only related to j j j of . And then this j j j Well , It's usually an interval range , And with i i i The increase of , j j j The range of is shifted to the right as a whole . This ensures that it can be transferred , Use monotone queues to find the best .
Ideas :
The note mentions j j j The range of the is shifted to the right as a whole , We set the transfer d p i − 1 dp_{i-1} dpi−1 when j j j The range of is [ l i − 1 , r i − 1 ] [l_{i-1},r_{i-1}] [li−1,ri−1], Transfer d p i dp_{i} dpi when j j j The range of is [ l i , r i ] [l_{i},r_{i}] [li,ri]. Then we r r r When it gets bigger , Just add the new element to the right of the monotone queue ( If you want to add , Before that, take away some unwanted elements on the right ), Then the answer is found on the left side of the queue . Of course , It's all monotonous stuff , Let's not go into details .
Practical examples
The question
n n n tree , Each tree has a height d i d_{i} di, Small H H H In the 1 1 1 On a tree , To jump to the n n n On a tree . In the i i i On a tree , You can jump to i , i + 1 , … , i + k i,i+1,…,i+k i,i+1,…,i+k On a tree . Every time , If the height is not less than the original height , The fatigue value will increase 1 1 1. Now ask little H H H From 1 1 1 Jump up a tree to the n n n On a tree , What is the minimum fatigue value .
( 1 ≤ n ≤ k ≤ 1 0 6 ) (1\leq n\leq k\leq 10^{6}) (1≤n≤k≤106)
Ideas
We set the State d p i dp_{i} dpi Jump to the i i i The minimum amount of fatigue that needs to be spent on a tree .
So there's a transfer equation d p i = m i n { d p j + [ d i > = d j ] } dp_{i}=min\{dp_{j}+[d_{i}>=d_{j}]\} dpi=min{
dpj+[di>=dj]}, among j ∈ [ i − k , i − 1 ] j\in [i-k,i-1] j∈[i−k,i−1].
We should learn this idea .
First of all, let's observe j ∈ [ i − k , i − 1 ] j\in [i-k,i-1] j∈[i−k,i−1], We find that the interval increases with i i i Enlarge and move the whole right , establish .
then d p j + [ d i > d j ] dp_{j}+[d_{i}>d_{j}] dpj+[di>dj], It's about i i i and j j j The formula of , Er, it doesn't seem to hold water .
At this time, we can only use our intelligence , Cough .
Let's find out first d p i dp_{i} dpi Monotony doesn't go down , And each increase is an increase of 1 1 1. So for two d p j dp_{j} dpj Value , We must take the smaller one directly , Because the little one + 1 +1 +1 No more than big . Then for two identical d p j dp_{j} dpj, We must give priority to d j d_{j} dj The big one , That is to find a way to make ( − d p j , d j ) (-dp_{j},d_{j}) (−dpj,dj) maximal j j j. In this way, you can optimize with monotone queue .
Just demonstrated that the car overturned , I decided to use it. d p i = w ( i , j ) , w h e r e g ( j ) i s m a x f o r j i n r a n g e dp_{i}=w(i,j),where\;g(j)\;is\;max\;for\;j\;in\;range dpi=w(i,j),whereg(j)ismaxforjinrange As a new style .
So maybe we can generalize the model .
Code
The title has some cards , open o2 Optimization is over .
Finishing work .
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int q[1000005],fr,bk;
int d[1000005];
int dp[1000005];
int w(int i,int j)
{
return dp[j]+(d[i]>=d[j]);
}
P g(int j)
{
return P(-dp[j],d[j]);
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&d[i]);
int m;
scanf("%d",&m);
while(m--)
{
fr=bk=0;
int k;
scanf("%d",&k);
dp[1]=0;
q[bk++]=1;
for(int i=2;i<=n;i++)
{
while(q[fr]<i-k)fr++;
dp[i]=w(i,q[fr]);
while(!(fr==bk)&&g(q[bk-1])<=g(i))bk--;
q[bk++]=i;
}
printf("%d\n",dp[n]);
}
return 0;
}
the second day ( Divide and conquer optimization )
( To be continued ..)
(upd: Updated that the title should not be followed by a colon bug)
Yesterday was about j j j Independent transfer , basis j j j The monotonicity of is optimized by monotone queue .
So today is not about j j j Independent , Namely and i i i and j j j It's all about , Just learned a divide and conquer optimization , Of course, you have to divide and conquer first .
Transfer equation
d p i = m a x ( w ( i , j ) ) , j ∈ [ 1 , i − 1 ] dp_{i}=max(w(i,j)),j \in [1,i-1] dpi=max(w(i,j)),j∈[1,i−1]
Or to say d p i = w ( i , j ) , w h e r e g ( i , j ) i s m a x f o r j i n r a n g e dp_{i}=w(i,j),where\;g(i,j)\;is\;max\;for\;j\;in\;range dpi=w(i,j),whereg(i,j)ismaxforjinrange
And it also needs its transfer , With i i i Satisfy some monotonicity of decision .
explain
All in all , We need to find the optimal transfer point and i , j i,j i,j It's all about .
then , The transfer should follow i i i Satisfy the monotonicity of decision . for instance , Roughly speaking , When i i i It's getting bigger , The best decision point j j j It will get bigger . But that doesn't mean j j j Bigger is better , There are some j j j It will not be regarded as the optimal decision point at all , Just what's for everyone i i i As the optimal decision point j j j We are following i i i Bigger and bigger ( No drop ).
Ideas
The general practice of divide and conquer is , Let's find the one in the middle first d p m i d dp_{mid} dpmid The transfer point of q m i d qmid qmid. Then according to the monotonicity of the decision , When i < m i d i<mid i<mid when , Transfer point j ∈ [ 1 , q m i d ] j\in[1,qmid] j∈[1,qmid]; When i > m i d i>mid i>mid when , Transfer point j ∈ [ q m i d , n ] j\in[qmid,n] j∈[qmid,n] And j < i j<i j<i.
Then take the midpoint every time to find the optimal transfer point , Cut the interval into two halves . Recursion depth is l o g log log Class , Then when we find the transfer point, we can violently traverse the decision interval where there may be the optimal transfer point . Overall complexity o ( n l o g n ) o(nlogn) o(nlogn).
Practical examples
The question
Given a length of n n n Sequence { a n } \{a_{n}\} {
an}, For each i ∈ [ 1 , n ] i\in [1,n] i∈[1,n], Find the smallest nonnegative integer p p p, bring ∀ j ∈ [ 1 , n ] \forall j\in[1,n] ∀j∈[1,n], There are a j ≤ a i + p − ∣ i − j ∣ a_j\le a_i+p-\sqrt{|i-j|} aj≤ai+p−∣i−j∣
1 ≤ n ≤ 5 × 1 0 5 , 0 ≤ a i ≤ 1 0 9 1\leq n\leq 5\times 10^{5},0\leq a_{i}\leq 10^{9} 1≤n≤5×105,0≤ai≤109
Ideas
We make d p i dp_{i} dpi It means the first one i i i Answer .
First , Get rid of the absolute value , We can run forward d p dp dp, Run backwards again , Find a maximum .
So the forward transfer equation is d p i = m a x { a j − a i + i − j } , j ∈ [ 1 , i − 1 ] dp_{i}=max\{a_{j}-a_{i}+\sqrt{i-j}\},j\in [1,i-1] dpi=max{
aj−ai+i−j},j∈[1,i−1].
About i i i Constant first , become d p i = m a x { a j + i − j } − a i , j ∈ [ 1 , i − 1 ] dp_{i}=max\{a_{j}+\sqrt{i-j}\}-a_{i},j\in [1,i-1] dpi=max{
aj+i−j}−ai,j∈[1,i−1].
So now it's , What we call model transfer d p i = m a x { w ( i , j ) } dp_{i}=max\{w(i,j)\} dpi=max{
w(i,j)}, among w ( i , j ) = a i + i − j w(i,j)=a_{i}+\sqrt{i-j} w(i,j)=ai+i−j.
then , It should satisfy what we call decision monotonicity , namely i i i When it gets bigger , The optimal decision point we choose j j j It will get bigger . Let's observe the formula w j ( i ) = a i + i − j w_{j}(i)=a_{i}+\sqrt{i-j} wj(i)=ai+i−j It's a story about i i i Function of , Yes i − 1 i-1 i−1 Functions like this , We're going to do it for every i i i
Choose the best function w j w_{j} wj, Make the function value the highest .
Then we find that the growth rate of these functions slows down , And w j w_{j} wj Than w j + 1 w_{j+1} wj+1 stay i i i At the same time, the growth rate is slower . That is, once i i i Grow to some point w j w_{j} wj By a w k w_{k} wk More than the , And k > j k>j k>j, that w j w_{j} wj There will never be a day to turn over .
in general , Is to meet when i i i Bigger , The optimal decision point will only keep getting bigger ( Or change the question to smaller ).
Satisfy the properties we mentioned earlier , Then we can divide and conquer .
Code :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
double a[500005];
ll dp1[500005],dp2[500005];
double w(int i,int j)
{
return a[j]+sqrt(abs(i-j))-a[i];
}
void solve(int l,int r,int ql,int qr,ll *dp)
{
if(l>r||ql>qr)return ;
int mid=(l+r)/2;
int qmid=ql;
for(int i=ql;i<=qr&&i<mid;i++)if(w(mid,i)>w(mid,qmid))qmid=i;
dp[mid]=ceil(w(mid,qmid));
solve(l,mid-1,ql,qmid,dp);solve(mid+1,r,qmid,qr,dp);
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lf",&a[i]);
solve(1,n,1,n,dp1);
reverse(a+1,a+n+1);
solve(1,n,1,n,dp2);
reverse(dp2+1,dp2+n+1);
for(int i=1;i<=n;i++)printf("%lld\n",max(0ll,max(dp1[i],dp2[i])));
return 0;
}
The first N God
( To be continued ..)
Take the gold , Break it first
版权声明
本文为[Zimba_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230621424727.html
边栏推荐
- [geek challenge 2019] havefun1
- #yyds干货盘点#ubuntu18.0.4安装mysql并解决ERROR 1698: Access denied for user ''root''@''localhost''
- 重载、重写、隐藏的对比
- 论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》
- Installation of data cleaning ETL tool kettle
- 阿里云架构师解读四大主流游戏架构
- Go language learning notes - array | go language from scratch
- Two ways for flutter providers to share data
- 112. Path sum
- GUI, CLI and UNIX Philosophy
猜你喜欢
SAP RFC_ CVI_ EI_ INBOUND_ Main BP master data creation example (Demo customer only)
Secrets in buffctf file 1
Enter "net start MySQL" and "system error 5. Access denied" appears. Detailed explanation of the problem
ABAP 7.4 SQL Window Expression
Kernel PWN learning (3) -- ret2user & kernel ROP & qwb2018 core
#yyds干货盘点#ubuntu18.0.4安装mysql并解决ERROR 1698: Access denied for user ''root''@''localhost''
Base de la technologie électronique numérique 3.1 aperçu du circuit de porte, 3.2 circuit de porte à diode semi - conductrice
Go language learning notes - slice, map | go language from scratch
653. Sum of two IV - input BST
Introduction to sap pi / PO login and basic functions
随机推荐
Random neurons and random depth of dropout Technology
kernel-pwn学习(3)--ret2user&&kernel ROP&&QWB2018-core
Compile and debug mysql8 with clion under MacOS x
High paid programmer & interview question series 91 limit 20000 loading is very slow. How to solve it? How to locate slow SQL?
Exclusive thoughts and cases of JS
Kettle实验 (三)
Comparison of overloading, rewriting and hiding
Canary publishing using ingress
JS case to find the maximum value, reverse the array, bubble sort
面试官:说几个PHP常用函数,幸好我面试之前看到了这篇文章
Kettle实验 (三)
How to use SQL statement union to get another column of another table when the content of a column in a table is empty
Leetcode0587. 安装栅栏(difficult)
[ACM-ICPC 2018 Shenyang Network preliminaries] J. Ka Chang (block + DFS sequence)
Two ways for flutter providers to share data
Three ways to create objects in JS
Two methods of building Yum source warehouse locally
kettle实验
DVWA range practice
SQL used query statements