Poj 1106 Transmitters

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();
	}
}
posted @ 2017-05-02 17:38  江南何采莲  阅读(141)  评论(0编辑  收藏  举报