#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 100005
#define fi first
#define se second
typedef long long lint;
typedef pair<lint, lint> pll;
const lint LINF = 1ll << 50;
const int mm[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
char S[MAXN];
vector<pll> dot;
vector<pll> ty, ty2;
vector<lint> ds[MAXN];
vector<pll> cell, cs[4 * MAXN];
vector<lint> done[4 * MAXN];
lint A, B, ABs;
pll f(pll a) {
return make_pair(B * a.fi - A * a.se, ((A * a.fi + B * a.se) % ABs + ABs) % ABs);
}
lint apbdis(pll a, pll b) { //a < b
return (A * (b.fi - a.fi) + B * (b.se - a.se)) / ABs;
}
int main() {
int N, K;
lint ans = 0ll;
scanf("%d%d%s", &N, &K, S);
dot.push_back(make_pair(0ll, 0ll));
for(int i = 0; i < N; i++) {
dot.push_back(dot.back());
if(S[i] == 'W') dot[i + 1].fi--;
else if(S[i] == 'E') dot[i + 1].fi++;
else if(S[i] == 'N') dot[i + 1].se++;
else dot[i + 1].se--;
}
for(int i = 0; i <= N; i++) for(int j = 0; j < 4; j++)
cell.push_back(make_pair(dot[i].fi - mm[j][0], dot[i].se - mm[j][1]));
A = dot[N].fi;
B = dot[N].se;
ABs = A * A + B * B;
if(A == 0 && B == 0) {
sort(dot.begin(), dot.end());
dot.resize(unique(dot.begin(), dot.end()) - dot.begin());
sort(cell.begin(), cell.end());
cell.resize(unique(cell.begin(), cell.end()) - cell.begin());
int ans = 0;
for(auto a : cell) {
bool b = true;
for(int j = 0; j < 4; j++) b &= binary_search(dot.begin(), dot.end(), make_pair(a.fi + mm[j][0], a.se + mm[j][1]));
if(b) ans++;
}
printf("%d", ans);
return 0;
}
for(auto a : dot) ty.push_back(f(a));
sort(ty.begin(), ty.end());
ty.resize(unique(ty.begin(), ty.end()) - ty.begin());
for(auto a : dot) {
int l = lower_bound(ty.begin(), ty.end(), f(a)) - ty.begin();
ds[l].push_back(A * a.fi + B * a.se);
}
for(int i = 0; i < ty.size(); i++) {
sort(ds[i].begin(), ds[i].end());
ds[i].resize(unique(ds[i].begin(), ds[i].end()) - ds[i].begin());
}
for(auto a : cell) ty2.push_back(f(a));
sort(ty2.begin(), ty2.end());
ty2.resize(unique(ty2.begin(), ty2.end()) - ty2.begin());
for(auto a : cell) {
int l = lower_bound(ty2.begin(), ty2.end(), f(a)) - ty2.begin();
cs[l].push_back(a);
}
for(int i = 0; i < ty2.size(); i++) {
sort(cs[i].begin(), cs[i].end(), [&](pll a, pll b) { return A * a.fi + B * a.se < A * b.fi + B * b.se; } );
cs[i].resize(unique(cs[i].begin(), cs[i].end()) - cs[i].begin());
}
for(int i = 0; i < ty2.size(); i++) for(int j = 0; j < cs[i].size(); j++) {
done[i].push_back(0ll);
for(int k = 0; k < 4; k++) {
lint x = cs[i][j].fi + mm[k][0], y = cs[i][j].se + mm[k][1];
pll nd = make_pair(x, y);
int l = lower_bound(ty.begin(), ty.end(), f(nd)) - ty.begin();
if(l == ty.size() || ty[l] != f(nd)) {
done[i][j] = K;
break;
}
lint ApB = A * x + B * y;
int ll = upper_bound(ds[l].begin(), ds[l].end(), ApB) - ds[l].begin();
if(ll == 0) {
done[i][j] = K;
break;
}
done[i][j] = max(done[i][j], (ApB - ds[l][ll - 1]) / ABs);
}
if(j > 0) done[i][j] = min(done[i][j - 1] + apbdis(cs[i][j - 1], cs[i][j]), done[i][j]);
done[i][j] = min(done[i][j], (lint) K);
ans += (j == 0 ? K : min(done[i][j - 1] + apbdis(cs[i][j - 1], cs[i][j]), (lint) K)) - done[i][j];
}
printf("%lld", ans);
return 0;
}
Compilation message
2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:75:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ty.size(); i++) {
~~^~~~~~~~~~~
2016_ho_t4.cpp:87:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ty2.size(); i++) {
~~^~~~~~~~~~~~
2016_ho_t4.cpp:92:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ty2.size(); i++) for(int j = 0; j < cs[i].size(); j++) {
~~^~~~~~~~~~~~
2016_ho_t4.cpp:92:55: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ty2.size(); i++) for(int j = 0; j < cs[i].size(); j++) {
~~^~~~~~~~~~~~~~
2016_ho_t4.cpp:98:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(l == ty.size() || ty[l] != f(nd)) {
~~^~~~~~~~~~~~
2016_ho_t4.cpp:36:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%s", &N, &K, S);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21496 KB |
Output is correct |
2 |
Correct |
22 ms |
21368 KB |
Output is correct |
3 |
Correct |
21 ms |
21496 KB |
Output is correct |
4 |
Correct |
21 ms |
21496 KB |
Output is correct |
5 |
Correct |
22 ms |
21496 KB |
Output is correct |
6 |
Correct |
21 ms |
21500 KB |
Output is correct |
7 |
Correct |
21 ms |
21500 KB |
Output is correct |
8 |
Correct |
21 ms |
21496 KB |
Output is correct |
9 |
Correct |
21 ms |
21496 KB |
Output is correct |
10 |
Correct |
26 ms |
21496 KB |
Output is correct |
11 |
Correct |
35 ms |
21624 KB |
Output is correct |
12 |
Correct |
21 ms |
21388 KB |
Output is correct |
13 |
Correct |
21 ms |
21496 KB |
Output is correct |
14 |
Correct |
21 ms |
21496 KB |
Output is correct |
15 |
Correct |
21 ms |
21496 KB |
Output is correct |
16 |
Correct |
22 ms |
21400 KB |
Output is correct |
17 |
Correct |
21 ms |
21464 KB |
Output is correct |
18 |
Correct |
28 ms |
21496 KB |
Output is correct |
19 |
Correct |
26 ms |
21496 KB |
Output is correct |
20 |
Correct |
27 ms |
21500 KB |
Output is correct |
21 |
Correct |
22 ms |
21496 KB |
Output is correct |
22 |
Correct |
21 ms |
21496 KB |
Output is correct |
23 |
Correct |
21 ms |
21496 KB |
Output is correct |
24 |
Correct |
21 ms |
21380 KB |
Output is correct |
25 |
Correct |
21 ms |
21496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21496 KB |
Output is correct |
2 |
Correct |
22 ms |
21368 KB |
Output is correct |
3 |
Correct |
21 ms |
21496 KB |
Output is correct |
4 |
Correct |
21 ms |
21496 KB |
Output is correct |
5 |
Correct |
22 ms |
21496 KB |
Output is correct |
6 |
Correct |
21 ms |
21500 KB |
Output is correct |
7 |
Correct |
21 ms |
21500 KB |
Output is correct |
8 |
Correct |
21 ms |
21496 KB |
Output is correct |
9 |
Correct |
21 ms |
21496 KB |
Output is correct |
10 |
Correct |
26 ms |
21496 KB |
Output is correct |
11 |
Correct |
35 ms |
21624 KB |
Output is correct |
12 |
Correct |
21 ms |
21388 KB |
Output is correct |
13 |
Correct |
21 ms |
21496 KB |
Output is correct |
14 |
Correct |
21 ms |
21496 KB |
Output is correct |
15 |
Correct |
21 ms |
21496 KB |
Output is correct |
16 |
Correct |
22 ms |
21400 KB |
Output is correct |
17 |
Correct |
21 ms |
21464 KB |
Output is correct |
18 |
Correct |
28 ms |
21496 KB |
Output is correct |
19 |
Correct |
26 ms |
21496 KB |
Output is correct |
20 |
Correct |
27 ms |
21500 KB |
Output is correct |
21 |
Correct |
22 ms |
21496 KB |
Output is correct |
22 |
Correct |
21 ms |
21496 KB |
Output is correct |
23 |
Correct |
21 ms |
21496 KB |
Output is correct |
24 |
Correct |
21 ms |
21380 KB |
Output is correct |
25 |
Correct |
21 ms |
21496 KB |
Output is correct |
26 |
Correct |
22 ms |
21604 KB |
Output is correct |
27 |
Correct |
30 ms |
22800 KB |
Output is correct |
28 |
Correct |
31 ms |
22896 KB |
Output is correct |
29 |
Correct |
118 ms |
35292 KB |
Output is correct |
30 |
Correct |
148 ms |
41936 KB |
Output is correct |
31 |
Correct |
269 ms |
54416 KB |
Output is correct |
32 |
Correct |
88 ms |
31708 KB |
Output is correct |
33 |
Correct |
213 ms |
48944 KB |
Output is correct |
34 |
Correct |
212 ms |
49104 KB |
Output is correct |
35 |
Correct |
215 ms |
48720 KB |
Output is correct |
36 |
Correct |
196 ms |
48328 KB |
Output is correct |
37 |
Correct |
86 ms |
31580 KB |
Output is correct |
38 |
Correct |
207 ms |
48652 KB |
Output is correct |
39 |
Correct |
202 ms |
48848 KB |
Output is correct |
40 |
Correct |
86 ms |
31580 KB |
Output is correct |
41 |
Correct |
180 ms |
48012 KB |
Output is correct |
42 |
Correct |
192 ms |
48316 KB |
Output is correct |
43 |
Correct |
214 ms |
48796 KB |
Output is correct |
44 |
Correct |
207 ms |
48780 KB |
Output is correct |
45 |
Correct |
196 ms |
48596 KB |
Output is correct |
46 |
Correct |
214 ms |
47164 KB |
Output is correct |
47 |
Correct |
234 ms |
47568 KB |
Output is correct |
48 |
Correct |
336 ms |
49752 KB |
Output is correct |
49 |
Correct |
264 ms |
56152 KB |
Output is correct |
50 |
Correct |
291 ms |
56128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21496 KB |
Output is correct |
2 |
Correct |
22 ms |
21368 KB |
Output is correct |
3 |
Correct |
21 ms |
21496 KB |
Output is correct |
4 |
Correct |
21 ms |
21496 KB |
Output is correct |
5 |
Correct |
22 ms |
21496 KB |
Output is correct |
6 |
Correct |
21 ms |
21500 KB |
Output is correct |
7 |
Correct |
21 ms |
21500 KB |
Output is correct |
8 |
Correct |
21 ms |
21496 KB |
Output is correct |
9 |
Correct |
21 ms |
21496 KB |
Output is correct |
10 |
Correct |
26 ms |
21496 KB |
Output is correct |
11 |
Correct |
35 ms |
21624 KB |
Output is correct |
12 |
Correct |
21 ms |
21388 KB |
Output is correct |
13 |
Correct |
21 ms |
21496 KB |
Output is correct |
14 |
Correct |
21 ms |
21496 KB |
Output is correct |
15 |
Correct |
21 ms |
21496 KB |
Output is correct |
16 |
Correct |
22 ms |
21400 KB |
Output is correct |
17 |
Correct |
21 ms |
21464 KB |
Output is correct |
18 |
Correct |
28 ms |
21496 KB |
Output is correct |
19 |
Correct |
26 ms |
21496 KB |
Output is correct |
20 |
Correct |
27 ms |
21500 KB |
Output is correct |
21 |
Correct |
22 ms |
21496 KB |
Output is correct |
22 |
Correct |
21 ms |
21496 KB |
Output is correct |
23 |
Correct |
21 ms |
21496 KB |
Output is correct |
24 |
Correct |
21 ms |
21380 KB |
Output is correct |
25 |
Correct |
21 ms |
21496 KB |
Output is correct |
26 |
Correct |
21 ms |
21496 KB |
Output is correct |
27 |
Correct |
21 ms |
21496 KB |
Output is correct |
28 |
Correct |
22 ms |
21496 KB |
Output is correct |
29 |
Correct |
21 ms |
21496 KB |
Output is correct |
30 |
Correct |
22 ms |
21496 KB |
Output is correct |
31 |
Correct |
22 ms |
21484 KB |
Output is correct |
32 |
Correct |
21 ms |
21496 KB |
Output is correct |
33 |
Correct |
22 ms |
21496 KB |
Output is correct |
34 |
Correct |
22 ms |
21496 KB |
Output is correct |
35 |
Correct |
27 ms |
21496 KB |
Output is correct |
36 |
Correct |
21 ms |
21496 KB |
Output is correct |
37 |
Correct |
26 ms |
21496 KB |
Output is correct |
38 |
Correct |
22 ms |
21496 KB |
Output is correct |
39 |
Correct |
22 ms |
21496 KB |
Output is correct |
40 |
Correct |
21 ms |
21496 KB |
Output is correct |
41 |
Correct |
22 ms |
21496 KB |
Output is correct |
42 |
Correct |
21 ms |
21496 KB |
Output is correct |
43 |
Correct |
21 ms |
21496 KB |
Output is correct |
44 |
Correct |
21 ms |
21496 KB |
Output is correct |
45 |
Correct |
22 ms |
21496 KB |
Output is correct |
46 |
Correct |
22 ms |
21496 KB |
Output is correct |
47 |
Correct |
21 ms |
21496 KB |
Output is correct |
48 |
Correct |
21 ms |
21500 KB |
Output is correct |
49 |
Correct |
21 ms |
21496 KB |
Output is correct |
50 |
Correct |
21 ms |
21496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21496 KB |
Output is correct |
2 |
Correct |
22 ms |
21368 KB |
Output is correct |
3 |
Correct |
21 ms |
21496 KB |
Output is correct |
4 |
Correct |
21 ms |
21496 KB |
Output is correct |
5 |
Correct |
22 ms |
21496 KB |
Output is correct |
6 |
Correct |
21 ms |
21500 KB |
Output is correct |
7 |
Correct |
21 ms |
21500 KB |
Output is correct |
8 |
Correct |
21 ms |
21496 KB |
Output is correct |
9 |
Correct |
21 ms |
21496 KB |
Output is correct |
10 |
Correct |
26 ms |
21496 KB |
Output is correct |
11 |
Correct |
35 ms |
21624 KB |
Output is correct |
12 |
Correct |
21 ms |
21388 KB |
Output is correct |
13 |
Correct |
21 ms |
21496 KB |
Output is correct |
14 |
Correct |
21 ms |
21496 KB |
Output is correct |
15 |
Correct |
21 ms |
21496 KB |
Output is correct |
16 |
Correct |
22 ms |
21400 KB |
Output is correct |
17 |
Correct |
21 ms |
21464 KB |
Output is correct |
18 |
Correct |
28 ms |
21496 KB |
Output is correct |
19 |
Correct |
26 ms |
21496 KB |
Output is correct |
20 |
Correct |
27 ms |
21500 KB |
Output is correct |
21 |
Correct |
22 ms |
21496 KB |
Output is correct |
22 |
Correct |
21 ms |
21496 KB |
Output is correct |
23 |
Correct |
21 ms |
21496 KB |
Output is correct |
24 |
Correct |
21 ms |
21380 KB |
Output is correct |
25 |
Correct |
21 ms |
21496 KB |
Output is correct |
26 |
Correct |
22 ms |
21604 KB |
Output is correct |
27 |
Correct |
30 ms |
22800 KB |
Output is correct |
28 |
Correct |
31 ms |
22896 KB |
Output is correct |
29 |
Correct |
118 ms |
35292 KB |
Output is correct |
30 |
Correct |
148 ms |
41936 KB |
Output is correct |
31 |
Correct |
269 ms |
54416 KB |
Output is correct |
32 |
Correct |
88 ms |
31708 KB |
Output is correct |
33 |
Correct |
213 ms |
48944 KB |
Output is correct |
34 |
Correct |
212 ms |
49104 KB |
Output is correct |
35 |
Correct |
215 ms |
48720 KB |
Output is correct |
36 |
Correct |
196 ms |
48328 KB |
Output is correct |
37 |
Correct |
86 ms |
31580 KB |
Output is correct |
38 |
Correct |
207 ms |
48652 KB |
Output is correct |
39 |
Correct |
202 ms |
48848 KB |
Output is correct |
40 |
Correct |
86 ms |
31580 KB |
Output is correct |
41 |
Correct |
180 ms |
48012 KB |
Output is correct |
42 |
Correct |
192 ms |
48316 KB |
Output is correct |
43 |
Correct |
214 ms |
48796 KB |
Output is correct |
44 |
Correct |
207 ms |
48780 KB |
Output is correct |
45 |
Correct |
196 ms |
48596 KB |
Output is correct |
46 |
Correct |
214 ms |
47164 KB |
Output is correct |
47 |
Correct |
234 ms |
47568 KB |
Output is correct |
48 |
Correct |
336 ms |
49752 KB |
Output is correct |
49 |
Correct |
264 ms |
56152 KB |
Output is correct |
50 |
Correct |
291 ms |
56128 KB |
Output is correct |
51 |
Correct |
21 ms |
21496 KB |
Output is correct |
52 |
Correct |
21 ms |
21496 KB |
Output is correct |
53 |
Correct |
22 ms |
21496 KB |
Output is correct |
54 |
Correct |
21 ms |
21496 KB |
Output is correct |
55 |
Correct |
22 ms |
21496 KB |
Output is correct |
56 |
Correct |
22 ms |
21484 KB |
Output is correct |
57 |
Correct |
21 ms |
21496 KB |
Output is correct |
58 |
Correct |
22 ms |
21496 KB |
Output is correct |
59 |
Correct |
22 ms |
21496 KB |
Output is correct |
60 |
Correct |
27 ms |
21496 KB |
Output is correct |
61 |
Correct |
21 ms |
21496 KB |
Output is correct |
62 |
Correct |
26 ms |
21496 KB |
Output is correct |
63 |
Correct |
22 ms |
21496 KB |
Output is correct |
64 |
Correct |
22 ms |
21496 KB |
Output is correct |
65 |
Correct |
21 ms |
21496 KB |
Output is correct |
66 |
Correct |
22 ms |
21496 KB |
Output is correct |
67 |
Correct |
21 ms |
21496 KB |
Output is correct |
68 |
Correct |
21 ms |
21496 KB |
Output is correct |
69 |
Correct |
21 ms |
21496 KB |
Output is correct |
70 |
Correct |
22 ms |
21496 KB |
Output is correct |
71 |
Correct |
22 ms |
21496 KB |
Output is correct |
72 |
Correct |
21 ms |
21496 KB |
Output is correct |
73 |
Correct |
21 ms |
21500 KB |
Output is correct |
74 |
Correct |
21 ms |
21496 KB |
Output is correct |
75 |
Correct |
21 ms |
21496 KB |
Output is correct |
76 |
Correct |
34 ms |
23548 KB |
Output is correct |
77 |
Correct |
26 ms |
22236 KB |
Output is correct |
78 |
Correct |
73 ms |
27228 KB |
Output is correct |
79 |
Correct |
149 ms |
40032 KB |
Output is correct |
80 |
Correct |
160 ms |
43632 KB |
Output is correct |
81 |
Correct |
91 ms |
31584 KB |
Output is correct |
82 |
Correct |
215 ms |
48552 KB |
Output is correct |
83 |
Correct |
180 ms |
48208 KB |
Output is correct |
84 |
Correct |
223 ms |
48764 KB |
Output is correct |
85 |
Correct |
215 ms |
48780 KB |
Output is correct |
86 |
Correct |
293 ms |
48568 KB |
Output is correct |
87 |
Correct |
221 ms |
49104 KB |
Output is correct |
88 |
Correct |
201 ms |
48592 KB |
Output is correct |
89 |
Correct |
206 ms |
48568 KB |
Output is correct |
90 |
Correct |
87 ms |
31580 KB |
Output is correct |
91 |
Correct |
206 ms |
48820 KB |
Output is correct |
92 |
Correct |
231 ms |
49108 KB |
Output is correct |
93 |
Correct |
226 ms |
48848 KB |
Output is correct |
94 |
Correct |
217 ms |
48328 KB |
Output is correct |
95 |
Correct |
211 ms |
48396 KB |
Output is correct |
96 |
Correct |
216 ms |
47312 KB |
Output is correct |
97 |
Correct |
237 ms |
47632 KB |
Output is correct |
98 |
Correct |
213 ms |
49744 KB |
Output is correct |
99 |
Correct |
270 ms |
56164 KB |
Output is correct |
100 |
Correct |
308 ms |
51512 KB |
Output is correct |