您的位置:首页 > 其它

URAL 1775 Space Bowling

2014-05-03 19:00 127 查看
题目链接http://acm.timus.ru/problem.aspx?space=1&num=1775

详细题解:很好的一道计算几何题目,考的就是想法,想到了就很简单了……

先算出每两个圆心间的距离,dis[i][j]表示第i和第j个圆心的距离

然后再以每条线段ij为边,求出以这样的线段为直线的左右两边的点的个数cnt和距离len[cnt],直线上的点要同时属于两边。 然后就可以排下len数组的序,ans = min(ans, len[m]);就ok了; 还有就是注意一下ans可能会小于0的情况。

G++不能用%lf真是个深坑……

#include<cstdio>
#include<cmath>
#include<climits>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 210
#define eps 1e-10
double dis

;
struct Point
{
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
};
Point p
;
typedef Point Vector;

Vector operator - (Point A, Point B) {return Vector(A.x-B.x, A.y-B.y);}
double Dot(Vector A, Vector B){return A.x*B.x + A.y*B.y;}
double Length(Vector A){return sqrt(Dot(A, A));}
double Cross(Vector A, Vector B){return A.x*B.y - A.y*B.x; }
int dcmp(double x)
{
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
int main ()
{
int n, m;
while(scanf("%d %d", &n, &m) != EOF)
{
for(int i = 1; i <= n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y);

if(m <= 2) { printf("0.000000\n"); continue;}

for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
dis[i][j] = Length(p[i] - p[j]);

double ans = INT_MAX;
for(int i = 1; i <= n; i++)
for(int j = i+1; j <= n; j++)
{
double len_l
, len_r
;
int cnt_l = 0, cnt_r = 0;
for(int k = 1; k <= n; k++)
{
double tmp = Cross(p[j]-p[i], p[k]-p[i]);
int ha = dcmp(tmp);
if(ha == 0)
{
cnt_l++;cnt_r++;
len_l[cnt_l] = len_r[cnt_r] = 0;

}
else if(ha > 0)
{
cnt_l++;
len_l[cnt_l] = fabs(tmp) / dis[i][j];
}
else{
cnt_r++;
len_r[cnt_r] = fabs(tmp)/ dis[i][j];

}

}

sort(len_l+1, len_l+cnt_l+1);
sort(len_r+1, len_r+cnt_r+1);
if(cnt_l >= m) ans = min(ans, len_l[m]);
if(cnt_r >= m) ans = min(ans, len_r[m]);
}

ans--;
if(ans <= 0) ans = 0;
printf("%.10f\n", ans);

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  几何