Submission #99179

#TimeUsernameProblemLanguageResultExecution timeMemory
99179bharat2002Global Warming (CEOI18_glo)C++14
100 / 100
128 ms8772 KiB
/*input
9 4
5 6 7 1 2 3 11 12 13
*/
#include<bits/stdc++.h>
using namespace std;
const int N=2e5 + 100;
const int mod=1e9 + 7;
#define int long long
const int inf=1e18;
#define pii pair<int, int>
#define f first
#define s second 
#define mp make_pair
stack< pii > updatestolis;int lis[N], lds[N], arr[N], n, x, ans, l2;
void clis()
{
	int len=1;lis[0]=0;
	for(int i=1;i<=n;i++)
	{
		pii add;
		if(arr[i]<lis[1])
		{
			add=mp(1, lis[1]);
			lis[1]=arr[i];
		}
		else if(arr[i]>lis[len-1])
		{
			add=mp(len, 1e17);lis[len++]=arr[i];
		}
		else
		{
			int low=1, high=len-1;
			while(low<high)
			{
				int mid=(low+high)>>1;
				if(lis[mid]>=arr[i]) high=mid;
				else low=mid+1;
			}
			add=mp(low, lis[low]);lis[low]=arr[i];
		}
		updatestolis.push(add);
	}
	ans=len-1;l2=len;
	for(int i=len;i<=n;i++) lis[i]=1e17;
}
void clds()
{
	int len=1;lds[0]=lds[1]=1e17;
	for(int i=n;i>=1;i--)
	{
		int add;
		if(arr[i]>lds[1])
		{
			lds[1]=arr[i];add=1;
		}
		else if(arr[i]<lds[len-1])
		{
			lds[len++]=arr[i];add=len-1;
		}
		else
		{
			int low=1, high=len-1;
			while(low<high)
			{
				int mid=(low+high)>>1;
				if(lds[mid]<=arr[i]) high=mid;
				else low=mid+1;
			}
			lds[low]=arr[i];add=low;
		}
		pii temp=updatestolis.top();updatestolis.pop();
		lis[temp.f]=temp.s;
		int low=1, high=n;
		while(low<high)
		{
			int mid=(low+high+1)/2;
			if(lis[mid] - arr[i]<x) low=mid;
			else high=mid-1;
		}		
		if(lis[low] - arr[i]>=x) low=0;
//		cout<<low<<" "<<add<<endl;
		ans=max(ans, low + add);
	}
	assert(len==l2);
	for(int i=len;i<=n;i++) lis[i]=1e17;
}
signed main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	cin>>n>>x;
	for(int i=1;i<=n;i++)
	{
		cin>>arr[i];
	}
	clis();clds();cout<<ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...