-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorellets.cs
More file actions
176 lines (157 loc) · 4.55 KB
/
Morellets.cs
File metadata and controls
176 lines (157 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Solution
{
static void Main(string[] args)
{
ReadPoints(out Point a, out Point b);
int n = int.Parse(Console.ReadLine());
var lines = new HashSet<Line>();
ReadLines(lines, n);
if (IsPointOnLine(a, lines) || IsPointOnLine(b, lines)) Console.WriteLine("ON A LINE");
else if (HaveSameColour(a, b, lines)) Console.WriteLine("YES");
else Console.WriteLine("NO");
Console.Read();
}
struct Point
{
public int x, y;
public Point(int X, int Y)
{
x = X;
y = Y;
}
}
struct Line
{
public int a, b, c;
public Line(int A, int B, int C)
{
a = A;
b = B;
c = C;
}
}
static int GCD(int x, int y)
{
int res;
if (x == 0) res = y;
else if (y == 0) res = x;
else if (x == y) res = x;
else if (x == 1 || y == 1) res = 1;
else
{
bool xIsEven = x >> 1 << 1 == x;
bool yIsEven = y >> 1 << 1 == y;
if (xIsEven && yIsEven) res = GCD(x >> 1, y >> 1) << 1;
else if (xIsEven) res = GCD(x >> 1, y);
else if (yIsEven) res = GCD(x, y >> 1);
else if (x > y) res = GCD((x - y) >> 1, y);
else res = GCD(x, (y - x) >> 1);
}
return res;
}
static int GCD(int a, int b, int c)
{
return GCD(GCD(Math.Abs(a), Math.Abs(b)), Math.Abs(c));
}
static void ReadPoints(out Point A, out Point B)
{
string[] inputs;
inputs = Console.ReadLine().Split(' ');
A.x = int.Parse(inputs[0]);
A.y = int.Parse(inputs[1]);
B.x = int.Parse(inputs[2]);
B.y = int.Parse(inputs[3]);
}
static void ReadLines(ISet<Line> lines, int n)
{
for (int i = 0; i < n; i++)
{
var inputs = Console.ReadLine().Split(' ');
int a = int.Parse(inputs[0]);
int b = int.Parse(inputs[1]);
int c = int.Parse(inputs[2]);
int gcd = GCD(a, b, c);
a /= gcd;
b /= gcd;
c /= gcd;
var reverseLine = new Line(-a, -b, -c);
if (!lines.Contains(reverseLine)) lines.Add(new Line(a, b, c));
}
}
static bool IsPointOnLine(Point point, ISet<Line> lines)
{
bool res = false;
foreach (var line in lines)
{
if (line.a * point.x + line.b * point.y + line.c == 0) res = true;
}
return res;
}
static bool HasSameSign(int a, int b)
{
return !(a > 0 ^ b > 0);
}
static bool PointAtLeft(Point point, Line line)
{
return line.a * point.x + line.b * point.y + line.c < 0;
}
static bool PointAtRight(Point point, Line line)
{
return line.a * point.x + line.b * point.y + line.c > 0;
}
static bool PointAtBottom(Point point, Line line)
{
bool t1 = line.a * point.x + line.b * point.y + line.c > 0;
bool t2 = HasSameSign(line.a, line.b);
return !(t1 ^ t2);
}
static bool PointAtTop(Point point, Line line)
{
bool t1 = line.a * point.x + line.b * point.y + line.c > 0;
bool t2 = HasSameSign(line.a, line.b);
return t1 ^ t2;
}
static int CountLinesBetween2Points(Point a, Point b, ISet<Line> lines)
{
int count = 0;
var visited = new HashSet<Line>();
if (a.x != b.x)
{
Point left = a.x < b.x ? a : b;
Point right = a.x > b.x ? a : b;
foreach (var line in lines)
{
if (line.a != 0 && PointAtLeft(left, line) && PointAtRight(right, line))
{
++count;
visited.Add(line);
}
}
}
if (a.y != b.y)
{
Point bottom = a.y < b.y ? a : b;
Point top = a.y > b.y ? a : b;
foreach (var line in lines.Except(visited))
{
if (line.b != 0 && PointAtBottom(bottom, line) && PointAtTop(top, line))
++count;
}
}
return count;
}
static bool HaveSameColour(Point a, Point b, ISet<Line> lines)
{
return CountLinesBetween2Points(a, b, lines) % 2 == 0;
}
}