-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdiff_Main.cpp
More file actions
149 lines (145 loc) · 4.51 KB
/
Copy pathdiff_Main.cpp
File metadata and controls
149 lines (145 loc) · 4.51 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
diff --git a/CodeJam/Main.cpp b/examples/2021/KS_F/C/Main.cpp
index 2150379..0e86f9d 100644
--- a/CodeJam/Main.cpp
+++ b/examples/2021/KS_F/C/Main.cpp
@@ -1,11 +1,11 @@
-// #define DEFAULT_VAL_MODE //remove comment on this line, to activate default value trigger
-#define DEFAULT_VAL_TRIGGER result.sz == 0
+#define DEFAULT_VAL_MODE //remove comment on this line, to activate default value trigger
+#define DEFAULT_VAL_TRIGGER result == 5 * 1e6
#define DEFAULT_VAL "IMPOSSIBLE"
// #define IA_MODE //remove comment on this line, to activate interactive problem mode
#define IA_ERROR_CODE -1
#define IA_COMM_LOG //add comment on this line, to deactivate the interactive communication error log
-// #define XY_NOTATION //remove commment on this line, to activate xy notation on complex numbers
-#define COMM_TYPE ll
+#define XY_NOTATION //remove commment on this line, to activate xy notation on complex numbers
+#define COMM_TYPE ld
// The maintained and empty code template can be found at:
// https://github.com/demmerichs/CodeJamTemplate
@@ -1570,11 +1570,128 @@ void init(){
cin >> T;
}
+ll N;
+v(cell) stars;
+cell blue;
+
+d(ll, s(ll)) connects_through_blue;
+
void readInput(){
+ cin >> N;
+ stars.resize(N);
+ connects_through_blue.cl;
+ ll px,py;
+ forn(i, N){
+ cin >> px >> py;
+ stars[i] = cell(px, py);
+ }
+ cin >> px >> py;
+ blue = cell(px, py);
+}
+
+bool line_contains_blue(ll s, ll e){
+ cell other_white = stars[e] - stars[s];
+ cell rel_blue = blue - stars[s];
+ ll k;
+ if(rel_blue.x != 0){
+ k = other_white.x / rel_blue.x;
+ } else {
+ k = other_white.y / rel_blue.y;
+ }
+ if(k<=1) return false;
+ if(rel_blue * k == other_white) return true;
+ return false;
+}
+
+ld length(ll s, ll e){
+ return sqrtl(norm(stars[s]-stars[e]));
+}
+
+ll sign (cell p1, cell p2, cell p3)
+{
+ return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
+}
+
+bool all_on_line (ll s1, ll s2, ll s3) {
+ return sign(stars[s1], stars[s2], stars[s3]) == 0;
+}
+
+bool blue_inside (ll s1, ll s2, ll s3)
+{
+ ll d1, d2, d3;
+ bool all_neg, all_pos;
+
+ d1 = sign(blue, stars[s1], stars[s2]);
+ d2 = sign(blue, stars[s2], stars[s3]);
+ d3 = sign(blue, stars[s3], stars[s1]);
+
+ all_neg = (d1 < 0) && (d2 < 0) && (d3 < 0);
+ all_pos = (d1 > 0) && (d2 > 0) && (d3 > 0);
+
+ return all_neg || all_pos;
}
// write to COMM_TYPE result
void calcFunction() {
+ result = 5 * 1e6;
+
+ forn(s, N){
+ fornn(e, s+1, N){
+ if(line_contains_blue(s, e)){
+ connects_through_blue[s].insert(e);
+ connects_through_blue[e].insert(s);
+ }
+ }
+ }
+
+ forn(i, N){
+ lg(i);
+ lg(connects_through_blue[i]);
+ }
+
+ forn(s1, N){
+ fornn(s2, s1+1, N){
+ fornn(s3, s2+1, N){
+ if(blue_inside(s1,s2,s3)){
+ result = min(result, length(s1, s2) + length(s2, s3) + length(s3, s1));
+ lg(s1);
+ lg(s2);
+ lg(s3);
+ }
+ if(all_on_line(s1, s2, s3)) continue;
+ if(connects_through_blue[s1].count(s2)){
+ foreach(s4, connects_through_blue[s3]){
+ result = min(result, length(s1, s3) + length(s1, s4) + length(s2, s3) + length(s2, s4));
+ lg("quad");
+ lg(s1);
+ lg(s2);
+ lg(s3);
+ lg(s4);
+ }
+ }
+ if(connects_through_blue[s1].count(s3)){
+ foreach(s4, connects_through_blue[s2]){
+ result = min(result, length(s1, s2) + length(s1, s4) + length(s3, s2) + length(s3, s4));
+ lg("quad");
+ lg(s1);
+ lg(s3);
+ lg(s2);
+ lg(s4);
+ }
+ }
+ if(connects_through_blue[s2].count(s3)){
+ foreach(s4, connects_through_blue[s1]){
+ result = min(result, length(s2, s1) + length(s2, s4) + length(s3, s1) + length(s3, s4));
+ lg("quad");
+ lg(s2);
+ lg(s3);
+ lg(s1);
+ lg(s4);
+ }
+ }
+ }
+ }
+ }
}
} // namespace task