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>
using namespace std;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)(x).size()
#define ft first
#define sd second
#define pb push_back
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vll;
typedef vector<vi> vvi;
#define dbg if(1)
void printbit(int x) {string s="\n";while(x){s+=((x%2)?'1':'0');x/=2;}
reverse(all(s)); cout<<s;}
const ll MOD = 1e9+7;
const int INF = 1e9;
const int N = 2501;
void solve(){
int n, k;
cin>>n>>k;
int a[n];
for (int i =0;i<n;i++) cin>>a[i];
set<int> s;
vii pref(n+1, {0,0});
for (int i = 0;i<n;i++){
auto it = s.upper_bound(a[i]);
if (it!=s.end()) s.erase(it);
s.insert(a[i]);
pref[i+1] = {sz(s), *--s.end()};
}
s.clear();
vii suff(n+1, {0,0});
for (int i = n-1;i>=0;i--){
auto it = s.upper_bound(a[i]);
if (it!=s.begin()) s.erase(--it);
s.insert(a[i]);
suff[i] = {sz(s), *s.begin()};
}
int ans = 0;
for (int i = 1;i<=n;i++) {
if (pref[i].ft+suff[i].ft>ans && abs(pref[i].sd-suff[i].sd)<k){
ans = pref[i].ft+suff[i].ft;
// cout<<i<<endl;
}
}
cout<<ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// #ifdef ONLINE_JUDGE
// freopen("timeline.in", "r", stdin);
// freopen("timeline.out", "w", stdout);
// #endif
int t = 1;
// cin>>t;
int i = 0;
while(t--){
// cout<<"Case #"<<++i<<": ";
solve();
cout<<"\n";
}
}
Compilation message (stderr)
glo.cpp: In function 'int main()':
glo.cpp:79:9: warning: unused variable 'i' [-Wunused-variable]
79 | int i = 0;
| ^
# | 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... |