Poj 1106 Transmitters

本文介绍了一种解决PoJ1106题目的算法——如何找到一个可旋转的半圆能覆盖的最大点数。通过枚举每个点作为必选点,并利用叉积来判断其它点是否能在同一半圆内,从而求得最优解。

Poj 1106 Transmitters

传送门

给出一个半圆,可以任意旋转,问这个半圆能够覆盖的最多点数。
我们枚举每一个点作为必然覆盖点,那么使用叉积看极角关系即可判断其余的点是否能够与其存在一个半圆内



import java.io.*;
import java.util.*;

public class Main {
    static class Point implements Comparable<Point> {
        double x, y;

        @Override
        public int compareTo(Point a) {
            return (int) cross(a);
        }

        public double cross(Point a) {
            return a.x * y - x * a.y;
        }
    }

    static double cross(Point st, Point a, Point b) {
        return (a.x - st.x) * (b.y - st.y) - (b.x - st.x) * (a.y - st.y);
    }

    static double dist(Point a1, Point a2) {
        return (a1.x - a2.x) * (a1.x - a2.x) + (a1.y - a2.y) * (a1.y - a2.y);
    }

    static final int N = 10005;
    static final int inf = 0x3f3f3f3f;
    static final double eps = 1e-9;
    static Point a[] = new Point[N];

    public static void main(String[] args) {
        Scanner cin = new Scanner(new InputStreamReader(System.in));
        while (cin.hasNext()) {
            Point st = new Point(), tmp = new Point();
            st.x = cin.nextDouble();
            st.y = cin.nextDouble();
            double r = cin.nextDouble();
            double d;
            int n = cin.nextInt();
            int cnt = 0;
            for (int i = 0; i < n; i++) {
                tmp.x = cin.nextDouble();
                tmp.y = cin.nextDouble();
                d = dist(st, tmp);
                if (d < r * r || Math.abs(d - r * r) < eps) {
                    cnt++;
                    if (a[cnt] == null)
                        a[cnt] = new Point();
                    a[cnt].x = tmp.x;
                    a[cnt].y = tmp.y;
                }
            }
            int ans = 0;
            for (int i = 1; i <= cnt; i++) {
                int count = 1;
                for (int j = 1; j <= cnt; j++) {
                    if (i != j && cross(st, a[i], a[j]) <= 0)
                        count++;
                }
                if (count > ans)
                    ans = count;
            }
            System.out.println(ans);
        }
        cin.close();
    }
}

转载于:https://www.cnblogs.com/zsyacm666666/p/6797289.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值