寒假集训5 B.牛牛战队的比赛地(三分)

题目链接:https://ac.nowcoder.com/acm/contest/3006/B
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
三分做法讲解:https://blog.csdn.net/huzujun/article/details/81187455
https://www.cnblogs.com/sugewud/p/9819304.html
在这里插入图片描述
本题可以这么理解,有唯一一个距离最小值(也就是我们要找的那个点),其左右都比它大,所以是一个凹陷下去的抛物线。
两种三分写法:
在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
#define eps 1e-6
struct node
{
	int x;
	int y;
}a[110000];
int n;

double f(double x)
{
	double maxdis=0;
	for(int i=0;i<n;i++)
	{
		maxdis=max(maxdis,sqrt(1.0*(a[i].x-x)*(a[i].x-x)+1.0*a[i].y*a[i].y));
	}
	return maxdis;
}
double sanfen()
{
	double l=-10000,r=10000,mid,rmid;
	while(l+eps<r)
	{
		mid=(l+r)/2;
	    rmid=(r+mid)/2;
		if(f(mid)<f(rmid))
			r=rmid;
		else
			l=mid;
	}
	return f(l);
}


int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
		scanf("%d%d",&a[i].x,&a[i].y);
	double ans=sanfen();
	cout<<ans<<endl;
	return 0;
}


三分法例题:HDU4355
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4355
本题注意防止三分超时,取eps为1e-6,同时三分的范围取minpos,maxpos,而不是直接题目给的范围。

#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
#define eps 1e-6
struct node
{
	double pos;
	double w;
}a[55000];
int n;
double min(double a,double b)
{
	return a<b?a:b;
}
double max(double a,double b)
{
	return a>b?a:b;
}
double f(double x)
{
	double sum=0;
	for(int i=0;i<n;i++)
	{
		sum+=pow(fabs(x-a[i].pos),3)*a[i].w;
	}
	return sum;
}
double sanfen(double ll ,double rr)
{
	double l=ll,r=rr;
	double mid,rmid;
	while(l+eps<r)
	{
		mid=(l+r)/2;
		rmid=(mid+r)/2;
		if(f(mid)<f(rmid))
			r=rmid;
		else
			l=mid;
	}
	return f(l);
}
int main()
{
	int t;
	cin>>t;
	int tt=1;
	while(t--)
	{
		scanf("%d",&n);
		double minp=1000000,maxp=-1000000;
		for(int i=0;i<n;i++)
		{
			scanf("%lf%lf",&a[i].pos,&a[i].w);
			minp=min(a[i].pos,minp);
			maxp=max(a[i].pos,maxp);
		}
		double ans=sanfen(minp,maxp);
		printf("Case #%d: %d\n",tt++,(int)(floor(ans+0.5)));
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Buyi.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值