// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;
bool check(vector<int>&arr,int j,int i)
{
int mxEle = -1;
for(int k=j;k<=i;k++)
mxEle = max(mxEle,arr[k]);
if(arr[i]!=mxEle)
return true;
else
return false;
}
vector<int> findLeft(vector<int>&arr,int n)
{
vector<int> left(n,-1);
stack<int>s;
left[0]=-1;
for(int i=1;i<n;i++)
{
while(!s.empty() && arr[s.top()]<=arr[i])
s.pop();
if(s.empty())
left[i] = -1;
else
left[i] = s.top();
s.push(i);
}
return left;
}
int solve(vector<int>&arr,int n)
{
int ans=0;
vector<int> dp(n,0);
dp[0]=0;//as single element won;t constitute any shipments
vector<int>left = findLeft(arr,n);
vector<int> pref(n,0);
for(int i=1;i<n;i++)
{
// for(int j=i-1;j>=0;j--)
// {
// if(check(arr,j,i))
// {
// if(j==0)
// dp[i] = max(dp[i],1);//whole 0 to i single shipment
// else
// dp[i] = max(dp[i],dp[j-1]+1);
// }
// }
// ans = max(ans,dp[i]);
int j = left[i];
if(j==-1)//no one on left greater then himself
dp[i]=0;
else if(j==0)//whole 0 to i be part of single shipment
dp[i] = max(dp[i],1);
else if(j+1==i)//edge case say 0,1,4,6,5 a valid and last 2 but 2 itself is not valid so here 5,2 and would combine it and from 6 (i.e j-1)
dp[i] = max(dp[i],pref[j-1]+1);
else
{
dp[i] = max(dp[i],pref[j]+1);
}
cout<<pref[i-1]<<" "<<pref[i]<<" "<<pref[j]<<" "<<dp[i]<<endl;
pref[i] = max(pref[i-1],dp[i]);
ans = max(ans,dp[i]);
}
return ans;
}
int main() {
int n;
vector<int> arr={0, 1, 4, 6, 5, 2};
n = arr.size();
cout<<solve(arr,n);
return 0;
}
Ly8gT25saW5lIEMrKyBjb21waWxlciB0byBydW4gQysrIHByb2dyYW0gb25saW5lCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpib29sIGNoZWNrKHZlY3RvcjxpbnQ+JmFycixpbnQgaixpbnQgaSkKewogICAgaW50IG14RWxlID0gLTE7CiAgICBmb3IoaW50IGs9ajtrPD1pO2srKykKICAgICAgICBteEVsZSA9IG1heChteEVsZSxhcnJba10pOwogICAgaWYoYXJyW2ldIT1teEVsZSkKICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIGVsc2UKICAgICAgICByZXR1cm4gZmFsc2U7Cn0KdmVjdG9yPGludD4gZmluZExlZnQodmVjdG9yPGludD4mYXJyLGludCBuKQp7CiAgICB2ZWN0b3I8aW50PiBsZWZ0KG4sLTEpOwogICAgc3RhY2s8aW50PnM7CiAgICBsZWZ0WzBdPS0xOwogICAgZm9yKGludCBpPTE7aTxuO2krKykKICAgIHsKICAgICAgICB3aGlsZSghcy5lbXB0eSgpICYmIGFycltzLnRvcCgpXTw9YXJyW2ldKQogICAgICAgICAgICBzLnBvcCgpOwogICAgICAgIGlmKHMuZW1wdHkoKSkKICAgICAgICAgICAgbGVmdFtpXSA9IC0xOwogICAgICAgIGVsc2UKICAgICAgICBsZWZ0W2ldID0gcy50b3AoKTsKICAgICAgICBzLnB1c2goaSk7CiAgICB9CiAgICByZXR1cm4gbGVmdDsKfQppbnQgc29sdmUodmVjdG9yPGludD4mYXJyLGludCBuKQp7CiAgICBpbnQgYW5zPTA7CiAgICB2ZWN0b3I8aW50PiBkcChuLDApOwogICAgZHBbMF09MDsvL2FzIHNpbmdsZSBlbGVtZW50IHdvbjt0IGNvbnN0aXR1dGUgYW55IHNoaXBtZW50cwogICAgdmVjdG9yPGludD5sZWZ0ID0gZmluZExlZnQoYXJyLG4pOwogICAgdmVjdG9yPGludD4gcHJlZihuLDApOwogICAgCiAgICBmb3IoaW50IGk9MTtpPG47aSsrKQogICAgewogICAgICAgIC8vIGZvcihpbnQgaj1pLTE7aj49MDtqLS0pCiAgICAgICAgLy8gewogICAgICAgIC8vICAgICBpZihjaGVjayhhcnIsaixpKSkKICAgICAgICAvLyAgICAgewogICAgICAgIC8vICAgICAgICAgaWYoaj09MCkKICAgICAgICAvLyAgICAgICAgICAgICBkcFtpXSA9IG1heChkcFtpXSwxKTsvL3dob2xlIDAgdG8gaSBzaW5nbGUgc2hpcG1lbnQKICAgICAgICAvLyAgICAgICAgIGVsc2UKICAgICAgICAvLyAgICAgICAgICAgICBkcFtpXSA9IG1heChkcFtpXSxkcFtqLTFdKzEpOwogICAgICAgIC8vICAgICB9CiAgICAgICAgLy8gfQogICAgICAgIC8vIGFucyA9IG1heChhbnMsZHBbaV0pOwogICAgICAgIGludCBqID0gbGVmdFtpXTsKICAgICAgICBpZihqPT0tMSkvL25vIG9uZSBvbiBsZWZ0IGdyZWF0ZXIgdGhlbiBoaW1zZWxmCiAgICAgICAgICAgIGRwW2ldPTA7CiAgICAgICAgZWxzZSBpZihqPT0wKS8vd2hvbGUgMCB0byBpIGJlIHBhcnQgb2Ygc2luZ2xlIHNoaXBtZW50CiAgICAgICAgICAgIGRwW2ldID0gbWF4KGRwW2ldLDEpOwogICAgICAgIGVsc2UgaWYoaisxPT1pKS8vZWRnZSBjYXNlIHNheSAwLDEsNCw2LDUgYSB2YWxpZCBhbmQgbGFzdCAyIGJ1dCAyIGl0c2VsZiBpcyBub3QgdmFsaWQgc28gaGVyZSA1LDIgYW5kIHdvdWxkIGNvbWJpbmUgaXQgYW5kIGZyb20gNiAoaS5lIGotMSkKICAgICAgICAgICAgZHBbaV0gPSBtYXgoZHBbaV0scHJlZltqLTFdKzEpOwogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIGRwW2ldID0gbWF4KGRwW2ldLHByZWZbal0rMSk7CiAgICAgICAgfQogICAgICAgIGNvdXQ8PHByZWZbaS0xXTw8IiAiPDxwcmVmW2ldPDwiICI8PHByZWZbal08PCIgIjw8ZHBbaV08PGVuZGw7CiAgICAgICAgcHJlZltpXSA9IG1heChwcmVmW2ktMV0sZHBbaV0pOwogICAgICAgIGFucyA9IG1heChhbnMsZHBbaV0pOwogICAgfQogICAgcmV0dXJuIGFuczsKfQppbnQgbWFpbigpIHsKICAgIGludCBuOwogICAgdmVjdG9yPGludD4gYXJyPXswLCAxLCA0LCA2LCA1LCAyfTsKICAgIG4gPSBhcnIuc2l6ZSgpOwogICAKICAgIGNvdXQ8PHNvbHZlKGFycixuKTsKICAgIHJldHVybiAwOwp9