题解 CF1409E 【Two Platforms】

Solution- CF 1409 E \mathrm{CF1409E}CF1409E

Topic meaning

Topic portal

S o l \mathrm{Sol} Sol

Is it true that I use data structures for this problem? ? !

We first discretize the coordinates and then calculate iii at the beginning of the platform[i, i + k] [[i,i+k ] How many small balls can fall are recorded assi s_isi. We are si s_isi Build a line segment tree to maintain the maximum interval.

So we enumerate the starting point of the first platform st sts t , then the contribution this time issst + max ⁡ j = st + 1 n (sj) s_{st}+\max\limits_{j=st+1}^{n}(s_j)sst+j=st+1maxn(sj) , Just calculate the line segment tree directly.

Time complexity O (n log ⁡ n) O(n \log n)O ( nlogn)

C o d e \mathrm{Code} Code

int n,m,Q,x[N],y[N],a[N],gs[N],pre[N],s[N],mx[N<<2],ans;

inline void build(int x,int l,int r)
{
    
    
	if(l==r)
	{
    
    
		mx[x]=s[l];
		return;
	}
	int mid=l+r>>1;
	build(x<<1,l,mid);
	build(x<<1|1,mid+1,r);
	mx[x]=max(mx[x<<1],mx[x<<1|1]);
}

inline int ask(int x,int l,int r,int ll,int rr)
{
    
    
	if(ll>r||rr<l) return 0;
	if(ll<=l&&r<=rr) return mx[x];
	int mid=l+r>>1,ans=0;
	ans=max(ask(x<<1,l,mid,ll,rr),ask(x<<1|1,mid+1,r,ll,rr));
	return ans;
}

int main()
{
    
    
	io.read(Q);
	for (;Q--;)
	{
    
    
		io.read(n),io.read(m);
		For(i,0,n) gs[i]=0,pre[i]=s[i]=0;
		For(i,1,n) io.read(x[i]),a[i]=x[i];
		For(i,1,n) io.read(y[i]);
		sort(a+1,a+n+1);
		int tot=unique(a+1,a+n+1)-a-1;
		For(i,1,n) gs[lower_bound(a+1,a+tot+1,x[i])-a]++;
		For(i,1,n) pre[i]=pre[i-1]+gs[i];
		For(i,1,n) s[i]=pre[upper_bound(a+i+1,a+tot+1,a[i]+m)-a-1]-pre[i-1];
		build(1,1,tot);
		For(i,1,tot) ans=max(ans,s[i]+ask(1,1,tot,upper_bound(a+i+1,a+tot+1,a[i]+m)-a,tot));
		io.write(ans),puts(""),ans=0;
	}
	return 0;
}
		

Guess you like

Origin blog.csdn.net/wangyiyang2/article/details/108425300