luogu P5867 【[SEERC2018]Fishermen】

这篇博客介绍了一种使用排序和二分查找优化的算法来解决钓鱼问题,其中涉及到了渔夫的位置和能钓到每条鱼的最远距离计算。博主通过手写快速排序对渔夫位置进行排序,并利用二分查找确定每条鱼的捕获范围,从而降低了时间复杂度。代码实现中包含了详细的数据结构和关键函数,如lower_bound和upper_bound。
摘要由CSDN通过智能技术生成

氵题解

初步审题,很容易就能想到枚举每条鱼,找到能钓到这一条鱼的渔夫最靠左与最靠右的渔夫的位置,直接枚举,时间复杂度为O(n^2)

但对于线性的枚举很容易就可以想到排序+二分

由于蒟蒻不会重载运算符,所以手打排序

基本思路确定了,那么怎样确定能钓到每一条鱼的渔夫最靠左与最靠右的渔夫的位置

由此,我们可以推出 若鱼的坐标为 (x , y),则可以钓到第i条鱼的渔夫最左边可能在 x ± \pm ± (l-y)

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=2*1e5;
struct node {
	int x,y;
} fish[maxn+5];//鱼的位置 
int ans[maxn+5],num[maxn+5],c[maxn+5],fisher[maxn+5];
//num[i]表示实际位置为i的点是当前在num[i] , c[i]表示i点实际编号为c[i] 
//fisher表示每一个渔夫的位置 
inline void rd(int &rex) {//读优 
	rex=0;
	char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')
	rex=rex*10+c-'0',c=getchar();
}
void swap(int &x,int &y) {//交换 
	int t=x;
	x=y,y=t;
}
void qsort(int l,int r) {//手写快排 
	int i=l,j=r,mid=fisher[(l+r)/2],p;
	while(i<=j) {
		while(fisher[i]<mid)i++;
		while(fisher[j]>mid)j--;
		if(i<=j) {
			swap(fisher[i],fisher[j]);
			swap(c[i],c[j]);
			i++,j--;
		}
	}
	if(l<j)qsort(l,j);
	if(i<r)qsort(i,r);
}
int main() {
	int n,m,l;
	rd(n);
	rd(m);
	rd(l);
	for(int i=1; i<=n; i++) {
		rd(fish[i].x);
		rd(fish[i].y);
	}
	for(int i=1; i<=m; i++)
		rd(fisher[i]),c[i]=i;
	qsort(1,m);//使渔夫的位置单调递增,满足二分的条件 
	for(int i=1; i<=n; i++) {
		if(fish[i].y>l)continue;
		int head=-(l-fish[i].y)+fish[i].x,tail=l-fish[i].y+fish[i].x;
		head=lower_bound(fisher+1,fisher+1+m,head)-fisher;
		tail=upper_bound(fisher+1,fisher+1+m,tail)-fisher;
		ans[head]++,ans[tail]--;
	}
	for(int i=1; i<=m; i++)
		ans[i]+=ans[i-1],num[c[i]]=i;
	for(int i=1; i<=m; i++)
		printf("%d\n",ans[num[i]]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值