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;
}
详细题解:很好的一道计算几何题目,考的就是想法,想到了就很简单了……
先算出每两个圆心间的距离,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;
}
相关文章推荐
- URAL - 1057 Amount of Degrees
- Ural 1099 Work Scheduling
- URAL1076.Trash 二分图完美匹配
- Ural 2036. Intersect Until You're Sick of It 计算几何
- Ural 2045 Richness of words(构造)
- Ural 2041 Nanomatryoshkas(贪心)
- URAL 2066 Simple Expression (水题,暴力)
- URAL 2067
- URAL1996 Cipher Message 3(KMP + FFT)
- URAL 1519Formula 1
- URAL 1018 二叉苹果树(简单树形dp)
- ural 1261. Tips
- ural 1104,暴力取模
- URAL 1137 Bus Routes(欧拉回路路径)
- URAL 1004 Sightseeing Trip(最小环)
- Ural1079-Maximum
- Ural1523-K-inversions
- ural 1313
- ural 1496
- [补集转化 有序化去重] Ural 1212 Battleship