This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define pii pair<int, int>
#define xx first
#define yy second
#define MAXN 200010
using namespace std;
int n, x, a[MAXN], b[MAXN], dp[MAXN], lvd[MAXN], ress=-1; ///lvd=lis veci desno
int binarna(int cnt, int k)
{
int l=0, r=cnt-1;
int ret=-1;
while (l<=r)
{
int mid=l+(r-l)/2;
if (dp[mid]<k)
{
ret=mid;
l=mid+1;
}
else
r=mid-1;
}
return ret;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>x;
int maxx=-1;
for (int i=0; i<n; i++) { cin>>a[i]; maxx=max(maxx, a[i]); }
for (int i=0; i<n; i++) b[i]=maxx-a[i];
dp[0]=b[n-1];
int cnt=1;
lvd[n-1]=0; lvd[n-2]=0;
if (a[n-1]>(a[n-2]-x)) lvd[n-2]=1;
for (int i=n-2; i>/*=*/0; i--)
{
if (b[i]<dp[0]) dp[0]=b[i];
else if (b[i]>dp[cnt-1]) dp[cnt++]=b[i];
else
{
if (find(dp, dp+cnt, b[i])==dp+cnt)
{
auto gde=upper_bound(dp, dp+cnt, b[i])-dp;
dp[gde]=b[i];
}
}
int kolko=a[i-1]-x;
//cout<<i-1<<" "<<maxx-kolko<<" "<<cnt<<endl;
lvd[i-1]=binarna(cnt, maxx-kolko)+1;
}
//for (int i=0; i<n; i++) cout<<lvd[i]<<" ";
//cout<<endl;
a[0]-=x;
dp[0]=a[0];
ress=max(ress, lvd[0]+1);
cnt=1;
for (int i=1; i<n; i++)
{
a[i]-=x;
if (a[i]<dp[0]) dp[0]=a[i];
else if (a[i]>dp[cnt-1]) dp[cnt++]=a[i];
else
{
if (find(dp, dp+cnt, a[i])==dp+cnt)
{
auto gde=upper_bound(dp, dp+cnt, a[i])-dp;
dp[gde]=a[i];
}
}
int kolko=binarna(cnt, a[i]);
kolko++;
ress=max(ress, (kolko+lvd[i]+1));
}
ress=max(ress, cnt);
cout<<ress;
}
/*
15
20
1 7 3 20 8 3 17 10 3 27 14 17 13 9 15
5
10
1 4 2 7 10
5
1
1 4 2 7 10
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |