# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1007680 | makanhulia | Global Warming (CEOI18_glo) | C++17 | 0 ms | 0 KiB |
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 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;
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;
}
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);
}
// for(int i =0;i<n;i++){
// cout<<inc[i]<<" "<<linc[i]<<endl;
// }
// trav(it,temp){
// cout<<it<<" ";
// }
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;
ans=max(ans,linc[i]+(size(temp)-binser(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();
}
}