UVaLive4356 Fire-Control System (扫描法)

题意:

有n个点在平面直接坐标线,给出了n个点坐标,然后问以(0,0)为圆心的扇形包含至少k个点最小面积。

分析:

贪心,先把所有点按与x轴正半轴的角度排序,然后选出一个点当半径,枚举剩下点(半径小于第一个点),更新最小面积值。
再求面积的时候,因为要保证扇形区域内有k个点,所以在把所有符合要求的半径的点选出来之后,每次选则一个有k个点的区间范围角度,然后根据这个角度求面积:
S=角度*r*r/2,这样更新最小值就行了。

代码:

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;
const int N = 5e3 + 9;
const double PI = acos (-1.0);
struct point {
    int x, y, r;
    double angle;
    bool operator < (const point& rhs) const {
        return angle < rhs.angle;
    }
};
point p[N];
double q[N];
int main() {
   // freopen ("f.txt", "r", stdin);
    int n, k, cas = 0;
    while (~scanf ("%d%d", &n, &k) && (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);
//        for(int i=n;i<2*n;i++)
//            p[i]=p[i-n];
        double ans = 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 t = 0;
                for (int j = 0, l = k - 1; j < num; j++, l++) {
                    if (l < num) t = q[l] - q[j];
                    else t = q[l - num] - q[j] + PI * 2;
                    ans = min (ans, t * p[i].r / 2);
                }
            }
        }
        printf ("Case #%d: %.2lf\n", cas, ans);
    }
    return 0;
}
/*
Sample Input

3 1 
0 1 
1 0 
-5 -6 

3 2 
0 2 
2 0 
-5 -6 

0 0

Sample Output

Case #1: 0.00 
Case #2: 3.14
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值