URAL 1775 Space Bowling

题目链接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[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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值