输入输出样例

样例1

输入

  1. 9 3
  2. 1 1 A
  3. 1 0 A
  4. 1 -1 A
  5. 2 2 B
  6. 2 3 B
  7. 0 1 A
  8. 3 1 B
  9. 1 3 B
  10. 2 0 A
  11. 0 2 -3
  12. -3 0 2
  13. -3 1 1

输出

  1. No
  2. No
  3. Yes

题解

将点的x,y坐标带入直线方程左侧式子,根据结果的正负可以知道该点在直线的上方还是下方。因此如果全部的A类点的计算结果有相同的正负性,B类点也有相同的正负性,且A类点和B类点正负性相反,说明该条直线满足条件。

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5. int pointNum = scanner.nextInt();
  6. int lineNum = scanner.nextInt();
  7. List<Point> points = new ArrayList<>(pointNum);
  8. int x, y;
  9. char type;
  10. for (int i = 0; i < pointNum; ++i) {
  11. x = scanner.nextInt();
  12. y = scanner.nextInt();
  13. type = scanner.nextLine().charAt(1);
  14. Point temp = new Point(x, y, type);
  15. points.add(temp);
  16. }
  17. for (int i = 0; i < lineNum; ++i) {
  18. int[] params = new int[3];
  19. for (int j = 0; j < 3; ++j) {
  20. params[j] = scanner.nextInt();
  21. }
  22. boolean ans = true;
  23. int flagA = 0;
  24. int flagB = 0;
  25. int sum;
  26. for (Point p : points) {
  27. sum = params[0] + p.x * params[1] + p.y * params[2];
  28. if (p.type == 'A') {
  29. if (flagA == 0) {
  30. if (sum < 0) {
  31. flagA = -1;
  32. } else {
  33. flagA = 1;
  34. }
  35. }
  36. if (flagA * sum < 0) {
  37. ans = false;
  38. break;
  39. }
  40. } else {
  41. if (flagB == 0) {
  42. if (sum < 0) {
  43. flagB = -1;
  44. } else {
  45. flagB = 1;
  46. }
  47. }
  48. if (flagB * sum < 0) {
  49. ans = false;
  50. break;
  51. }
  52. }
  53. if (flagA * flagB > 0) {
  54. ans = false;
  55. break;
  56. }
  57. }
  58. if (ans) {
  59. System.out.println("Yes");
  60. } else {
  61. System.out.println("No");
  62. }
  63. if (scanner.hasNextLine()) {
  64. scanner.nextLine();
  65. }
  66. }
  67. }
  68. }
  69. class Point {
  70. public int x;
  71. public int y;
  72. public char type;
  73. public Point(int x, int y, char type) {
  74. this.x = x;
  75. this.y = y;
  76. this.type = type;
  77. }
  78. @Override
  79. public String toString() {
  80. return "Point [x=" + x + ", y=" + y + ", type=" + type + "]";
  81. }
  82. }