URAL 1775 Space Bowling

来源:互联网 发布:现货看盘软件 编辑:程序博客网 时间:2024/05/18 03:06

题目链接: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-10double dis[N][N];struct Point{double x, y;Point(double x = 0, double y = 0) : x(x), y(y) {}};Point p[N];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[N], len_r[N];                 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;}


0 0