标签:uvalive
题意:有n个点在平面直接坐标线,给出了n个点坐标,然后问以(0,0)为圆心的扇形包含至少k个点最小面积。
题解:贪心,先把所有点按与x轴正半轴的角度排序,然后选出一个点当半径,枚举剩下点(半径小于第一个点),更新最小面积值。
#include <stdio.h> #include <math.h> #include <algorithm> using namespace std; const int N = 5005; const double PI = acos(-1.0); struct P { int x, y, r; double angle; }p[N]; int n, k; double res, q[N]; bool cmp(P a, P b) { return a.angle < b.angle; } int main() { int cas = 1; while (scanf("%d%d", &n, &k) == 2 && n + k) { for (int i = 0; i < n; i++) { scanf("%d%d", &p[i].x, &p[i].y); p[i].angle = atan2(p[i].y, p[i].x); p[i].r = p[i].x * p[i].x + p[i].y * p[i].y; } if (k == 0) { printf("Case #%d: 0.00\n", cas++); continue; } sort(p, p + n, cmp); res = 1000000000.0; for (int i = 0; i < n; i++) { int num = 0; for (int j = 0; j < n; j++) if (p[j].r <= p[i].r) q[num++] = p[j].angle; if (num >= k) { double temp; for (int j = 0, l = k - 1; j < num; j++, l++) { if (l < num) temp = q[l] - q[j]; else temp = q[l - num] - q[j] + PI * 2; res = min(res, temp * p[i].r / 2); } } } printf("Case #%d: %.2lf\n", cas++, res); } return 0; }
标签:uvalive
原文地址:http://blog.csdn.net/hyczms/article/details/44733179