#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i=a; i<=(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b); i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define int long long
#define ld long double
#define lb lower_bound
#define ceil(a,b) (a+(b-1))/b
#define ins insert
#define ub upper_bound
#define pb push_back
#define VI vector<int>
#define VVI vector<VI>
#define PII pair<int,int>
#define VII vector<PII>
#define size(x) x.size()
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int mod = 1e9+7;
const int N = 2e5 + 5;
// const int mod = 998244353;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << ''' << x << ''';}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
int binser(vector<int>&temp,int num){
int l =0,r=size(temp)-1,ind,mid;
while(l<=r){
mid=(l+r)/2;
if(num>=temp[mid]){
ind=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
// if(num>=temp[ind]){
return ind;
// }
// else return -1;
}
int binserans(vector<int>&temp,int num){
int l =0,r=size(temp)-1,ind=-1,mid;
while(l<=r){
mid=(l+r)/2;
if(num<=temp[mid]){
ind=mid;
l=mid+1;
}
else{
r=mid-1;
}
}
// if(num>=temp[ind]){
return ind+1;
// }
// else return -1;
}
void solve(){
int n,k;
cin>>n>>k;
int arr[n];
for(int i =0 ;i<n;i++){
cin>>arr[i];
}
int inc[n],linc[n];
inc[0]=arr[0];
linc[0]=1;
vector<int> temp;
int ans=0;
temp.push_back(arr[0]);
for (int i = 1; i < n; i++) {
if (arr[i] > temp.back()) {
temp.push_back(arr[i]);
} else {
int ind = lower_bound(temp.begin(), temp.end(), arr[i]) - temp.begin();
temp[ind] = arr[i];
}
inc[i]=temp.back();
linc[i]=size(temp);
}
ans=size(temp);
temp.clear();
temp.push_back(arr[n-1]);
for (int i = n-2; i >= 0; i--) {
// trav(it,temp){
// cout<<it<<" ";
// }
// cout<<endl;
// dbg(linc[i],inc[i],temp,inc[i]-k+1,binserans(temp,inc[i]-k+1));
ans=max(ans,(int)(linc[i]+binserans(temp,inc[i]-k+1)));
if (arr[i] < temp.back()) {
temp.push_back(arr[i]);
} else {
int ind=binser(temp,arr[i]);
temp[ind] = arr[i];
}
}
cout<<ans<<endl;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t=1;
cout<<setprecision(12)<<fixed;
// cin>>t;
while(t--){
solve();
}
}
Compilation message
glo.cpp:34:31: error: empty character constant
34 | void __print(char x) {cerr << ''' << x << ''';}
| ^~
glo.cpp:34:33: warning: character constant too long for its type
34 | void __print(char x) {cerr << ''' << x << ''';}
| ^~~~~~~~~~~
glo.cpp:34:44: error: empty character constant
34 | void __print(char x) {cerr << ''' << x << ''';}
| ^~
glo.cpp: In function 'void __print(char)':
glo.cpp:34:33: error: expected ';' before '\x203c3c20'
34 | void __print(char x) {cerr << ''' << x << ''';}
| ^~~~~~~~~~~
| ;