题目1 : Farthest Point
-
1.000 1.000 5.000
样例输出 -
6 1
描述
Given a circle on a two-dimentional plane.
Output the integral point in or on the boundary of the circle which has the largest distance from the center.
输入
One line with three floats which are all accurate to three decimal places, indicating the coordinates of the center x, y and the radius r.
For 80% of the data: |x|,|y|<=1000, 1<=r<=1000
For 100% of the data: |x|,|y|<=100000, 1<=r<=100000
输出
One line with two integers separated by one space, indicating the answer.
If there are multiple answers, print the one with the largest x-coordinate.
If there are still multiple answers, print the one with the largest y-coordinate.
这题题意很简单,我们对于给定的一个圆,坐标为(x,y)半径为r,可以求出满足点的x轴边界,即ceil(x-r),floor(x+r)(ceil表示
取向下取整,floor表示向上取整)。遍历ceil(x-r)到floor(x+r),对于给定的横坐标dx,它的纵坐标为floor(y+sqrt(r*r-(x-dx)*(x-dx))),
ceil(y-sqrt(r*r-(x-dx)*(x-dx)))。然后就可以取最优的那个了。
#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
struct Node
{
int x,y;
double dis;
}N[200005*2];
bool cmp(Node a,Node b)
{
if(a.dis!=b.dis)
{
return a.dis>b.dis;
}
else if(a.x!=b.x)
{
return a.x>b.x;
}
else
{
return a.y>b.y;
}
}
int main()
{
double x,y,rr;
while(~scanf("%lf%lf%lf",&x,&y,&rr))
{
double left=ceil(x-rr);
double right=floor(x+rr);
int nnn=0;
for(int i=left;i<=right;i++)
{
double xx;
if(i<x)
xx=x-i;
else
xx=i-x;
double yy=sqrt(rr*rr-xx*xx);
N[nnn++].x=i;
N[nnn-1].y=floor(yy+y);
double t1,t2;
t1=x-N[nnn-1].x;
t2=y-N[nnn-1].y;
N[nnn-1].dis=sqrt(t1*t1+t2*t2);
N[nnn++].x=i;
N[nnn-1].y=ceil(y-yy);
t1=x-N[nnn-1].x;
t2=y-N[nnn-1].y;
N[nnn-1].dis=sqrt(t1*t1+t2*t2);
}
sort(N,N+nnn,cmp);
cout<<N[0].x<<" "<<N[0].y<<endl;
}
}
在给定圆的条件下,通过计算x轴边界并确定每个横坐标对应的纵坐标范围,找到最远的点。算法涉及ceil和floor函数以及平方根计算。
6916

被折叠的 条评论
为什么被折叠?



