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,-INF});
for (int i = 0;i<n;i++){
auto it = s.lower_bound(a[i]);
if (it!=s.end()) s.erase(it);
s.insert(a[i]);
pref[i+1] = {sz(s), *--s.end()};
}
for (int i =0 ;i<n;i++) a[i]*=-1;
vi temp;
int ans = 0;
for (int i = n-1;i>=0;i--){
auto it = lower_bound(all(temp), a[i]);
if (it!=temp.end()) *it = a[i];
else temp.pb(a[i]);
int inc = lower_bound(all(temp), -pref[i+1].sd+k)-temp.begin();
ans = max(ans, pref[i+1].ft+inc);
}
// for (int i = 0;i<=n;i++) {
// if (pref[i].ft+suff[i].ft>ans && pref[i].sd-k<suff[i].sd){
// ans = pref[i].ft+suff[i].ft;
// }
// }
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... |