codeforces 617C Watering Flowers(暴力)

题目链接:【codeforces 617C】

有两个水源,分别位于(x1, y1),(x2, y2),分别可以喷灌以r1,r2为半径的圆,有n多花分别位于(xi,yi),一朵花可以同时被两个水源喷灌,问所有的花都能被喷灌到时,r1^2+r2^2的最小值是多少

先求每朵花到水源1的距离,用dis[]来储存,dis[i]表示第i多花到水源1的距离

再求出每朵花到水源2的距离,用结构体ds[]储存,ds[i].id=x,ds[i].len=y表示第x多花到水源2的距离是y

将花到水源2的距离从大到小排序

枚举水源1的r1,求r2,记录最小值

wa了两次,原因是没有考虑到r1=0||r2=0这两种情况

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define ll __int64
ll x1, y1, x2, y2;
ll dis[2010];
ll x, y;
struct node
{
	int id;
	ll len;
	friend bool operator < (const node n1, const node n2)
	{
		return n1.len>n2.len;
	}
}ds[2010];
int main()
{
	int n; 
	scanf("%d", &n);
	scanf("%I64d%I64d%I64d%I64d", &x1, &y1, &x2, &y2);
	for(int i=1; i<=n; i++)
	{
		scanf("%I64d%I64d", &x, &y);
		dis[i] = (x1-x)*(x1-x)+(y1-y)*(y1-y);
		ds[i].len = (x2-x)*(x2-x)+(y2-y)*(y2-y);
		ds[i].id=i;
	}
	ll minx=1e16;

	sort(ds+1, ds+n+1);
	for(int i=0; i<=n; i++)//i=0是r1=0 
	{
		int flag=0; 
		for(int j=1; j<=n; j++)
		{
			if(dis[ds[j].id]>dis[i])//表示第ds[j].id朵花 不在水源1的喷射范围内 
			{
				flag=1;
				minx = min(minx, dis[i]+ds[j].len);
				break;
			}		
		}
		if(!flag) minx = min(minx, dis[i]);//这种情况下r2=0 
	}
	printf("%I64d\n", minx);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值