[codeforces 1409]E. Two Platforms

题意:

有两块长k的板子,可以在任意整数座标水平放置,问最多有多少小球的投影在板子上
首先注意到所有小球只关系它的xx座标,与yy座标无关
因为同样的xx座标,板子的yy座标肯定无穷小(能接住所有掉下来的小球)
所以现在问题是两段长k的线段,最多能覆盖多少点?

我们可以枚举第一块板子的起始点和终点,再求后缀最大的覆盖

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define se second
#define fi first
#define debug(x) cout<<#x<<" is "<<x<<endl
const int N = 2e5 + 5;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
unordered_map<int,int> g;
int hou[N],a[N],b[N];
int t,n,k;

int main(){
    
    
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>t;
	//
	while(t--){
    
    
		int ans = 0;
		cin>>n>>k;
		for(int i = 1;i <= n;i++)
			cin>>a[i];
		for(int i = 1;i <= n;i++)
			cin>>b[i];
		sort(a + 1,a + 1 +n);
		for(int i = n;i >= 1;i--){
    
    
			int nowx = upper_bound(a + 1 ,a + 1 + n,a[i] + k) - a;
			hou[i] = max(hou[i + 1],nowx - i);
		} 
		for(int i = 1;i <= n;i++){
    
    
			int index = upper_bound(a + 1,a + 1 + n,a[i] + k) - a;
			ans = max(ans,index - i + hou[index]);
		}
		cout<<ans<<endl;
		for(int i = 1;i <= n + 1;i++)hou[i] = 0;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_20252251/article/details/108429005