#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 201010
#define MAXS 20
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
pii arr[MAX];
int fdiv(int x, int y) {
if (!y) return 0;
if (x % y == 0) return x / y;
if (x < 0) return x / y - 1;
return x / y;
}
int fmod(int x, int y) {
if (!y) return x;
x %= y;
x += y;
x %= y;
return x;
}
bool valid(vector<pii>& v, pii p) {
int ind = lower_bound(v.begin(), v.end(), p) - v.begin();
if (ind >= v.size()) return false;
return v[ind] == p;
}
bool exist(set<pii>& st, pii p) {
return st.find(p) != st.end();
}
int chkv[3][3];
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int N, K;
cin >> N >> K;
int i;
string s;
cin >> s;
for (i = 1; i <= N; i++) {
arr[i] = arr[i - 1];
if (s[i - 1] == 'N') arr[i].first++;
if (s[i - 1] == 'S') arr[i].first--;
if (s[i - 1] == 'E') arr[i].second++;
if (s[i - 1] == 'W') arr[i].second--;
}
if (arr[N].first > 0) for (i = 1; i <= N; i++) arr[i].first *= -1;
if (arr[N].second > 0) for (i = 1; i <= N; i++) arr[i].second *= -1;
vector<pii> v;
set<pii> real;
for (i = 1; i <= N; i++) real.insert(arr[i]);
int X, Y;
X = -arr[N].first;
Y = -arr[N].second;
if (!X && !Y) K = 1;
if (K == 1) {
for (i = 0; i <= N; i++) {
int a, b;
for (a = arr[i].first - 1; a <= arr[i].first + 1; a++) for (b = arr[i].second - 1; b <= arr[i].second + 1; b++) v.emplace_back(a, b);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int ans = 0;
real.emplace(0, 0);
for (auto& [x, y] : v) {
if (exist(real, pii(x, y)) && exist(real, pii(x, y + 1)) && exist(real, pii(x + 1, y)) && exist(real, pii(x + 1, y + 1))) ans++;
}
cout << ans << ln;
return 0;
}
for (i = 1; i <= N; i++) {
int a, b;
for (a = arr[i].first - 1; a <= arr[i].first + 1; a++) for (b = arr[i].second - 1; b <= arr[i].second + 1; b++) v.emplace_back(a, b);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
map<tuple<int, int, int>, int> chk;
map<pii, int> mp;
for (auto& [x, y] : v) {
auto t = make_tuple(fmod(x, X), fmod(y, Y), fdiv(x, X) - fdiv(y, Y));
if (!X) t = make_tuple(fmod(x, X), fmod(y, Y), 0);
if (!Y) t = make_tuple(fmod(x, X), fmod(y, Y), 0);
int res = -1;
int v;
if (!Y) v = fdiv(x, X);
else v = fdiv(y, Y);
if (chk.find(t) != chk.end()) res = v - chk[t];
mp[pii(x, y)] = res;
if (exist(real, pii(x, y))) chk[t] = v;
}
ll ans = 0;
for (auto& [x, y] : v) {
int mx0, mx1;
mx0 = mx1 = -1;
int c = 1;
int cnt = 0;
int r = 0;
for (auto xx : { x, x + 1 }) {
for (auto yy : { y, y + 1 }) {
if (real.find(pii(xx, yy)) != real.end()) {
cnt++;
if (!~mp[pii(xx, yy)]) r = 1;
mx1 = max(mx1, mp[pii(xx, yy)]);
}
else {
if (!~mp[pii(xx, yy)]) {
c = 0;
break;
}
mx0 = max(mx0, mp[pii(xx, yy)]);
}
if (!valid(v, pii(xx, yy))) {
c = 0;
break;
}
}
if (!c) break;
}
if (!c) continue;
if (!cnt) continue;
if (cnt == 4) {
ans += K;
continue;
}
if (r) mx1 = K;
if (!~mx0 && cnt < 4) continue;
mx1 = min(K, mx1);
ans += max(0, mx1 - mx0);
}
auto findmin = [&](int x, int y) {
int mn = K;
for (int t = 1; t <= N; t++) {
int dx = arr[t].first - x;
int dy = arr[t].second - y;
if ((!X || dx % X == 0) && (!Y || dy % Y == 0)) {
int t1, t2;
t1 = t2 = -1;
if (dx < 0 || dy < 0) continue;
if (X) t1 = dx / X;
if (Y) t2 = dy / Y;
if (!~t1) t1 = t2;
if (!~t2) t2 = t1;
if (t1 == t2) mn = min(mn, t1);
}
}
return mn;
};
int j;
if (findmin(0, 0) >= K) {
for (i = -1; i <= 1; i++) for (j = -1; j <= 1; j++) {
chkv[i + 1][j + 1] = findmin(i, j);
}
if (chkv[0][0] < K && chkv[0][1] < K && chkv[1][0] < K) ans++;
if (chkv[1][0] < K && chkv[2][0] < K && chkv[2][1] < K) ans++;
if (chkv[0][1] < K && chkv[0][2] < K && chkv[1][2] < K) ans++;
if (chkv[2][2] < K && chkv[2][1] < K && chkv[1][2] < K) ans++;
}
cout << ans << ln;
}
Compilation message
2016_ho_t4.cpp: In function 'bool valid(std::vector<std::pair<int, int> >&, pii)':
2016_ho_t4.cpp:32:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | if (ind >= v.size()) return false;
| ~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
224 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
224 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
1104 KB |
Output is correct |
28 |
Correct |
12 ms |
2760 KB |
Output is correct |
29 |
Correct |
38 ms |
5700 KB |
Output is correct |
30 |
Correct |
53 ms |
10052 KB |
Output is correct |
31 |
Correct |
133 ms |
13632 KB |
Output is correct |
32 |
Correct |
82 ms |
10556 KB |
Output is correct |
33 |
Correct |
78 ms |
10752 KB |
Output is correct |
34 |
Correct |
80 ms |
10812 KB |
Output is correct |
35 |
Correct |
79 ms |
10816 KB |
Output is correct |
36 |
Correct |
81 ms |
10664 KB |
Output is correct |
37 |
Correct |
83 ms |
10672 KB |
Output is correct |
38 |
Correct |
76 ms |
10696 KB |
Output is correct |
39 |
Correct |
78 ms |
10780 KB |
Output is correct |
40 |
Correct |
75 ms |
10564 KB |
Output is correct |
41 |
Correct |
93 ms |
10576 KB |
Output is correct |
42 |
Correct |
74 ms |
10308 KB |
Output is correct |
43 |
Correct |
75 ms |
10820 KB |
Output is correct |
44 |
Correct |
75 ms |
10696 KB |
Output is correct |
45 |
Correct |
76 ms |
10440 KB |
Output is correct |
46 |
Correct |
118 ms |
11876 KB |
Output is correct |
47 |
Correct |
102 ms |
11968 KB |
Output is correct |
48 |
Correct |
112 ms |
14308 KB |
Output is correct |
49 |
Correct |
84 ms |
14296 KB |
Output is correct |
50 |
Correct |
87 ms |
14328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
224 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
224 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
1104 KB |
Output is correct |
28 |
Correct |
12 ms |
2760 KB |
Output is correct |
29 |
Correct |
38 ms |
5700 KB |
Output is correct |
30 |
Correct |
53 ms |
10052 KB |
Output is correct |
31 |
Correct |
133 ms |
13632 KB |
Output is correct |
32 |
Correct |
82 ms |
10556 KB |
Output is correct |
33 |
Correct |
78 ms |
10752 KB |
Output is correct |
34 |
Correct |
80 ms |
10812 KB |
Output is correct |
35 |
Correct |
79 ms |
10816 KB |
Output is correct |
36 |
Correct |
81 ms |
10664 KB |
Output is correct |
37 |
Correct |
83 ms |
10672 KB |
Output is correct |
38 |
Correct |
76 ms |
10696 KB |
Output is correct |
39 |
Correct |
78 ms |
10780 KB |
Output is correct |
40 |
Correct |
75 ms |
10564 KB |
Output is correct |
41 |
Correct |
93 ms |
10576 KB |
Output is correct |
42 |
Correct |
74 ms |
10308 KB |
Output is correct |
43 |
Correct |
75 ms |
10820 KB |
Output is correct |
44 |
Correct |
75 ms |
10696 KB |
Output is correct |
45 |
Correct |
76 ms |
10440 KB |
Output is correct |
46 |
Correct |
118 ms |
11876 KB |
Output is correct |
47 |
Correct |
102 ms |
11968 KB |
Output is correct |
48 |
Correct |
112 ms |
14308 KB |
Output is correct |
49 |
Correct |
84 ms |
14296 KB |
Output is correct |
50 |
Correct |
87 ms |
14328 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |