#include <vector>
#include <algorithm>
#include "Anna.h"
namespace {
int encode(int dr, int dc) {
if(dc >= +2) return 2;
if(dc <= -2) return 3;
if(dr >= +2) return 4;
if(dr <= -2) return 5;
return (dr + 1) * 3 + (dc + 1) + 6;
}
}
void Anna(int N, int K, std::vector<int> R, std::vector<int> C) {
// Step #1 : Decide placement of "flags with number 1"
int optflag = 14; int optdelta = -1;
for(int delta = 0; delta < 9; ++delta) {
int maxflag = 1;
for(int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j) {
int precinct = ((i - delta / 3 + 3) % 3) * 3 + ((j - delta % 3 + 3) % 3);
if(precinct <= 6) {
maxflag = std::max(maxflag, encode(R[precinct] - i, C[precinct] - j));
}
}
}
if(optflag > maxflag) {
optflag = maxflag;
optdelta = delta;
}
}
// Step #2 : Make a provisional plan
std::vector<std::vector<int> > F(N, std::vector<int>(N, -1));
for(int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j) {
int precinct = ((i - optdelta / 3 + 3) % 3) * 3 + ((j - optdelta % 3 + 3) % 3);
if(precinct <= 6) {
F[i][j] = encode(R[precinct] - i, C[precinct] - j);
}
if(precinct == 8) {
F[i][j] = 1;
}
}
}
// Step #3. Apply "decrease-by-1" operation and finalize the plan
std::vector<bool> used(14, false);
for(int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j) {
if(F[i][j] != -1) {
used[F[i][j]] = true;
}
}
}
int non = std::find(used.begin() + 1, used.end(), false) - used.begin();
for(int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j) {
if(F[i][j] == -1) {
F[i][j] = non - 1;
}
else if(F[i][j] > non) {
--F[i][j];
}
SetFlag(i, j, F[i][j]);
}
}
}
#include <vector>
#include <utility>
#include <algorithm>
#include "Bruno.h"
std::vector<int> Bruno(int K, std::vector<int> value) {
// Step #1. Restore the plan before "decrease-by-1" operation
int center = std::find(value.begin(), value.end(), 1) - value.begin();
int rp = (center / 3 + 1) % 3, cp = (center % 3 + 1) % 3;
std::vector<int> precinct(9);
for(int i = 0; i < 9; ++i) {
precinct[i] = ((i / 3 - rp + 3) % 3) * 3 + ((i % 3 - cp + 3) % 3);
}
int seventh = std::find(precinct.begin(), precinct.end(), 7) - precinct.begin();
int non = value[seventh] + 1;
for(int i = 0; i < 9; ++i) {
if(value[i] >= non) {
++value[i];
}
}
// Step #2. Determine the next actions
std::vector<int> res(K, -1);
for(int i = 0; i < K; ++i) {
int ptr = std::find(precinct.begin(), precinct.end(), i) - precinct.begin();
int id = value[ptr] - 2;
if(id <= 3) {
res[i] = id;
}
else {
int isor = ((id - 4) / 3 - 1) + (ptr / 3 - 1);
int isoc = ((id - 4) % 3 - 1) + (ptr % 3 - 1);
if(isoc > 0) res[i] = 0;
else if(isoc < 0) res[i] = 1;
else if(isor > 0) res[i] = 2;
else if(isor < 0) res[i] = 3;
else res[i] = 4;
}
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
512 KB |
Output is correct |
2 |
Correct |
13 ms |
512 KB |
Output is correct |
3 |
Correct |
13 ms |
512 KB |
Output is correct |
4 |
Correct |
12 ms |
512 KB |
Output is correct |
5 |
Correct |
13 ms |
512 KB |
Output is correct |
6 |
Correct |
14 ms |
512 KB |
Output is correct |
7 |
Correct |
18 ms |
620 KB |
Output is correct |
8 |
Correct |
17 ms |
512 KB |
Output is correct |
9 |
Correct |
16 ms |
512 KB |
Output is correct |
10 |
Correct |
16 ms |
512 KB |
Output is correct |
11 |
Correct |
13 ms |
512 KB |
Output is correct |
12 |
Correct |
16 ms |
512 KB |
Output is correct |
13 |
Correct |
13 ms |
512 KB |
Output is correct |
14 |
Correct |
13 ms |
512 KB |
Output is correct |
15 |
Correct |
16 ms |
512 KB |
Output is correct |
16 |
Correct |
18 ms |
512 KB |
Output is correct |
17 |
Correct |
15 ms |
512 KB |
Output is correct |
18 |
Correct |
14 ms |
512 KB |
Output is correct |
19 |
Correct |
13 ms |
512 KB |
Output is correct |
20 |
Correct |
14 ms |
512 KB |
Output is correct |
21 |
Correct |
13 ms |
512 KB |
Output is correct |
22 |
Correct |
16 ms |
512 KB |
Output is correct |
23 |
Correct |
13 ms |
512 KB |
Output is correct |
24 |
Correct |
15 ms |
512 KB |
Output is correct |
25 |
Correct |
14 ms |
512 KB |
Output is correct |
26 |
Correct |
14 ms |
512 KB |
Output is correct |
27 |
Correct |
13 ms |
512 KB |
Output is correct |
28 |
Correct |
14 ms |
512 KB |
Output is correct |
29 |
Correct |
18 ms |
512 KB |
Output is correct |
30 |
Correct |
14 ms |
512 KB |
Output is correct |
31 |
Correct |
13 ms |
512 KB |
Output is correct |
32 |
Correct |
16 ms |
640 KB |
Output is correct |
33 |
Correct |
13 ms |
512 KB |
Output is correct |
34 |
Correct |
13 ms |
512 KB |
Output is correct |
35 |
Correct |
15 ms |
620 KB |
Output is correct |
36 |
Correct |
17 ms |
512 KB |
Output is correct |
37 |
Correct |
14 ms |
512 KB |
Output is correct |
38 |
Correct |
16 ms |
624 KB |
Output is correct |
39 |
Correct |
15 ms |
512 KB |
Output is correct |
40 |
Correct |
17 ms |
512 KB |
Output is correct |
41 |
Correct |
17 ms |
512 KB |
Output is correct |
42 |
Correct |
14 ms |
512 KB |
Output is correct |
43 |
Correct |
15 ms |
620 KB |
Output is correct |
44 |
Correct |
15 ms |
728 KB |
Output is correct |
45 |
Correct |
18 ms |
728 KB |
Output is correct |
46 |
Correct |
16 ms |
728 KB |
Output is correct |
47 |
Correct |
19 ms |
728 KB |
Output is correct |
48 |
Correct |
15 ms |
512 KB |
Output is correct |
49 |
Correct |
20 ms |
512 KB |
Output is correct |
50 |
Correct |
20 ms |
512 KB |
Output is correct |
51 |
Correct |
19 ms |
512 KB |
Output is correct |
52 |
Correct |
20 ms |
512 KB |
Output is correct |
53 |
Correct |
18 ms |
620 KB |
Output is correct |
54 |
Correct |
19 ms |
512 KB |
Output is correct |
55 |
Correct |
20 ms |
512 KB |
Output is correct |
56 |
Correct |
20 ms |
512 KB |
Output is correct |
57 |
Correct |
24 ms |
512 KB |
Output is correct |
58 |
Correct |
23 ms |
512 KB |
Output is correct |
59 |
Correct |
20 ms |
512 KB |
Output is correct |
60 |
Correct |
22 ms |
512 KB |
Output is correct |
61 |
Correct |
26 ms |
512 KB |
Output is correct |
62 |
Correct |
19 ms |
612 KB |
Output is correct |
63 |
Correct |
20 ms |
512 KB |
Output is correct |
64 |
Correct |
19 ms |
512 KB |
Output is correct |
65 |
Correct |
23 ms |
512 KB |
Output is correct |
66 |
Correct |
22 ms |
512 KB |
Output is correct |
67 |
Correct |
20 ms |
512 KB |
Output is correct |
68 |
Correct |
23 ms |
512 KB |
Output is correct |
69 |
Correct |
18 ms |
612 KB |
Output is correct |
70 |
Correct |
26 ms |
512 KB |
Output is correct |
71 |
Correct |
21 ms |
512 KB |
Output is correct |
72 |
Correct |
20 ms |
512 KB |
Output is correct |
73 |
Correct |
23 ms |
512 KB |
Output is correct |
74 |
Correct |
24 ms |
640 KB |
Output is correct |
75 |
Correct |
17 ms |
512 KB |
Output is correct |
76 |
Correct |
19 ms |
612 KB |
Output is correct |
77 |
Correct |
19 ms |
512 KB |
Output is correct |
78 |
Correct |
19 ms |
512 KB |
Output is correct |
79 |
Correct |
21 ms |
512 KB |
Output is correct |
80 |
Correct |
23 ms |
512 KB |
Output is correct |
81 |
Correct |
20 ms |
512 KB |
Output is correct |
82 |
Correct |
22 ms |
512 KB |
Output is correct |
83 |
Correct |
21 ms |
512 KB |
Output is correct |
84 |
Correct |
19 ms |
624 KB |
Output is correct |
85 |
Correct |
20 ms |
512 KB |
Output is correct |
86 |
Correct |
21 ms |
512 KB |
Output is correct |
87 |
Correct |
20 ms |
512 KB |
Output is correct |
88 |
Correct |
19 ms |
512 KB |
Output is correct |
89 |
Correct |
25 ms |
612 KB |
Output is correct |
90 |
Correct |
24 ms |
612 KB |
Output is correct |
91 |
Correct |
23 ms |
512 KB |
Output is correct |
92 |
Correct |
21 ms |
636 KB |
Output is correct |
93 |
Correct |
26 ms |
512 KB |
Output is correct |
94 |
Correct |
20 ms |
512 KB |
Output is correct |
95 |
Correct |
23 ms |
512 KB |
Output is correct |
96 |
Correct |
18 ms |
512 KB |
Output is correct |
97 |
Correct |
22 ms |
512 KB |
Output is correct |
98 |
Correct |
24 ms |
512 KB |
Output is correct |
99 |
Correct |
26 ms |
512 KB |
Output is correct |
100 |
Correct |
20 ms |
512 KB |
Output is correct |
101 |
Correct |
22 ms |
512 KB |
Output is correct |
102 |
Correct |
24 ms |
512 KB |
Output is correct |
103 |
Correct |
25 ms |
612 KB |
Output is correct |
104 |
Correct |
27 ms |
640 KB |
Output is correct |
105 |
Correct |
23 ms |
512 KB |
Output is correct |
106 |
Correct |
20 ms |
512 KB |
Output is correct |
107 |
Correct |
23 ms |
512 KB |
Output is correct |
108 |
Correct |
20 ms |
512 KB |
Output is correct |
109 |
Correct |
22 ms |
512 KB |
Output is correct |
110 |
Correct |
20 ms |
612 KB |
Output is correct |
111 |
Correct |
27 ms |
512 KB |
Output is correct |
112 |
Correct |
21 ms |
512 KB |
Output is correct |
113 |
Correct |
24 ms |
512 KB |
Output is correct |
114 |
Correct |
20 ms |
512 KB |
Output is correct |
115 |
Correct |
23 ms |
512 KB |
Output is correct |
116 |
Correct |
26 ms |
512 KB |
Output is correct |
117 |
Correct |
21 ms |
512 KB |
Output is correct |
118 |
Correct |
19 ms |
612 KB |
Output is correct |
119 |
Correct |
22 ms |
512 KB |
Output is correct |
120 |
Correct |
848 ms |
764 KB |
Output is correct |
121 |
Correct |
846 ms |
1024 KB |
Output is correct |
122 |
Correct |
849 ms |
788 KB |
Output is correct |
123 |
Correct |
908 ms |
948 KB |
Output is correct |
124 |
Correct |
894 ms |
928 KB |
Output is correct |
125 |
Correct |
839 ms |
824 KB |
Output is correct |
126 |
Correct |
851 ms |
812 KB |
Output is correct |
127 |
Correct |
825 ms |
860 KB |
Output is correct |
128 |
Correct |
970 ms |
912 KB |
Output is correct |
129 |
Correct |
842 ms |
1008 KB |
Output is correct |
130 |
Correct |
816 ms |
800 KB |
Output is correct |
131 |
Correct |
838 ms |
764 KB |
Output is correct |
132 |
Correct |
853 ms |
804 KB |
Output is correct |
133 |
Correct |
879 ms |
784 KB |
Output is correct |
134 |
Correct |
831 ms |
912 KB |
Output is correct |
135 |
Correct |
832 ms |
796 KB |
Output is correct |
136 |
Correct |
818 ms |
892 KB |
Output is correct |
137 |
Correct |
848 ms |
920 KB |
Output is correct |
138 |
Correct |
858 ms |
804 KB |
Output is correct |
139 |
Correct |
866 ms |
1036 KB |
Output is correct |
140 |
Correct |
864 ms |
908 KB |
Output is correct |
141 |
Correct |
856 ms |
888 KB |
Output is correct |
142 |
Correct |
860 ms |
1000 KB |
Output is correct |
143 |
Correct |
860 ms |
900 KB |
Output is correct |
144 |
Correct |
858 ms |
1032 KB |
Output is correct |
145 |
Correct |
821 ms |
820 KB |
Output is correct |
146 |
Correct |
834 ms |
1124 KB |
Output is correct |
147 |
Correct |
843 ms |
860 KB |
Output is correct |
148 |
Correct |
855 ms |
984 KB |
Output is correct |
149 |
Correct |
856 ms |
780 KB |
Output is correct |
150 |
Correct |
862 ms |
1012 KB |
Output is correct |
151 |
Correct |
824 ms |
828 KB |
Output is correct |
152 |
Correct |
884 ms |
900 KB |
Output is correct |
153 |
Correct |
833 ms |
828 KB |
Output is correct |
154 |
Correct |
946 ms |
872 KB |
Output is correct |
155 |
Correct |
918 ms |
864 KB |
Output is correct |
156 |
Correct |
923 ms |
868 KB |
Output is correct |
157 |
Correct |
842 ms |
744 KB |
Output is correct |
158 |
Correct |
974 ms |
804 KB |
Output is correct |
159 |
Correct |
857 ms |
932 KB |
Output is correct |
160 |
Correct |
904 ms |
844 KB |
Output is correct |
161 |
Correct |
885 ms |
1060 KB |
Output is correct |
162 |
Correct |
901 ms |
792 KB |
Output is correct |
163 |
Correct |
870 ms |
760 KB |
Output is correct |
164 |
Correct |
955 ms |
764 KB |
Output is correct |
165 |
Correct |
855 ms |
808 KB |
Output is correct |
166 |
Correct |
861 ms |
988 KB |
Output is correct |
167 |
Correct |
897 ms |
848 KB |
Output is correct |
168 |
Correct |
848 ms |
736 KB |
Output is correct |
169 |
Correct |
875 ms |
1016 KB |
Output is correct |
170 |
Correct |
850 ms |
776 KB |
Output is correct |
171 |
Correct |
879 ms |
804 KB |
Output is correct |
172 |
Correct |
833 ms |
780 KB |
Output is correct |
173 |
Correct |
904 ms |
792 KB |
Output is correct |
174 |
Correct |
885 ms |
960 KB |
Output is correct |
175 |
Correct |
938 ms |
744 KB |
Output is correct |
176 |
Correct |
854 ms |
932 KB |
Output is correct |
177 |
Correct |
817 ms |
948 KB |
Output is correct |
178 |
Correct |
837 ms |
816 KB |
Output is correct |
179 |
Correct |
832 ms |
776 KB |
Output is correct |
180 |
Correct |
819 ms |
1112 KB |
Output is correct |
181 |
Correct |
817 ms |
952 KB |
Output is correct |
182 |
Correct |
825 ms |
832 KB |
Output is correct |
183 |
Correct |
827 ms |
788 KB |
Output is correct |
184 |
Correct |
836 ms |
760 KB |
Output is correct |
185 |
Correct |
829 ms |
892 KB |
Output is correct |
186 |
Correct |
834 ms |
720 KB |
Output is correct |
187 |
Correct |
844 ms |
940 KB |
Output is correct |
188 |
Correct |
822 ms |
912 KB |
Output is correct |
189 |
Correct |
823 ms |
960 KB |
Output is correct |
190 |
Correct |
818 ms |
896 KB |
Output is correct |
191 |
Correct |
834 ms |
776 KB |
Output is correct |
192 |
Correct |
843 ms |
1020 KB |
Output is correct |
193 |
Correct |
868 ms |
848 KB |
Output is correct |
194 |
Correct |
812 ms |
804 KB |
Output is correct |
195 |
Correct |
899 ms |
752 KB |
Output is correct |
196 |
Correct |
884 ms |
736 KB |
Output is correct |
197 |
Correct |
818 ms |
912 KB |
Output is correct |
198 |
Correct |
924 ms |
708 KB |
Output is correct |
199 |
Correct |
824 ms |
896 KB |
Output is correct |
200 |
Correct |
825 ms |
988 KB |
Output is correct |
201 |
Correct |
889 ms |
788 KB |
Output is correct |
202 |
Correct |
873 ms |
868 KB |
Output is correct |
203 |
Correct |
819 ms |
788 KB |
Output is correct |
204 |
Correct |
886 ms |
892 KB |
Output is correct |
205 |
Correct |
859 ms |
860 KB |
Output is correct |
206 |
Correct |
856 ms |
892 KB |
Output is correct |
207 |
Correct |
896 ms |
840 KB |
Output is correct |
208 |
Correct |
815 ms |
888 KB |
Output is correct |
209 |
Correct |
881 ms |
912 KB |
Output is correct |
210 |
Correct |
625 ms |
940 KB |
Output is correct |
211 |
Correct |
628 ms |
924 KB |
Output is correct |
212 |
Correct |
619 ms |
952 KB |
Output is correct |
213 |
Correct |
667 ms |
696 KB |
Output is correct |
214 |
Correct |
644 ms |
912 KB |
Output is correct |
215 |
Correct |
679 ms |
796 KB |
Output is correct |
216 |
Correct |
676 ms |
904 KB |
Output is correct |
217 |
Correct |
703 ms |
1084 KB |
Output is correct |
218 |
Correct |
668 ms |
860 KB |
Output is correct |
219 |
Correct |
644 ms |
1032 KB |
Output is correct |
220 |
Correct |
714 ms |
880 KB |
Output is correct |
221 |
Correct |
644 ms |
964 KB |
Output is correct |
222 |
Correct |
630 ms |
724 KB |
Output is correct |
223 |
Correct |
682 ms |
792 KB |
Output is correct |
224 |
Correct |
638 ms |
928 KB |
Output is correct |
225 |
Correct |
637 ms |
892 KB |
Output is correct |
226 |
Correct |
639 ms |
876 KB |
Output is correct |