输入输出样例
样例1
输入
9 31 1 A1 0 A1 -1 A2 2 B2 3 B0 1 A3 1 B1 3 B2 0 A0 2 -3-3 0 2-3 1 1
输出
NoNoYes
题解
将点的x,y坐标带入直线方程左侧式子,根据结果的正负可以知道该点在直线的上方还是下方。因此如果全部的A类点的计算结果有相同的正负性,B类点也有相同的正负性,且A类点和B类点正负性相反,说明该条直线满足条件。
import java.util.*;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int pointNum = scanner.nextInt();int lineNum = scanner.nextInt();List<Point> points = new ArrayList<>(pointNum);int x, y;char type;for (int i = 0; i < pointNum; ++i) {x = scanner.nextInt();y = scanner.nextInt();type = scanner.nextLine().charAt(1);Point temp = new Point(x, y, type);points.add(temp);}for (int i = 0; i < lineNum; ++i) {int[] params = new int[3];for (int j = 0; j < 3; ++j) {params[j] = scanner.nextInt();}boolean ans = true;int flagA = 0;int flagB = 0;int sum;for (Point p : points) {sum = params[0] + p.x * params[1] + p.y * params[2];if (p.type == 'A') {if (flagA == 0) {if (sum < 0) {flagA = -1;} else {flagA = 1;}}if (flagA * sum < 0) {ans = false;break;}} else {if (flagB == 0) {if (sum < 0) {flagB = -1;} else {flagB = 1;}}if (flagB * sum < 0) {ans = false;break;}}if (flagA * flagB > 0) {ans = false;break;}}if (ans) {System.out.println("Yes");} else {System.out.println("No");}if (scanner.hasNextLine()) {scanner.nextLine();}}}}class Point {public int x;public int y;public char type;public Point(int x, int y, char type) {this.x = x;this.y = y;this.type = type;}@Overridepublic String toString() {return "Point [x=" + x + ", y=" + y + ", type=" + type + "]";}}
