#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> P;
const int INF = 1e18;
map<P,set<int>> M;
int dx, dy;
P r(int x, int y) {
int rx = (x%dx+dx)%dx;
int ry = y - dy * (x-rx)/dx;
return P(rx, ry);
}
int mx[4] = {0,0,1,1};
int my[4] = {0,1,1,0};
void print(priority_queue<P,vector<P>,greater<P>> PQ) {
while(!PQ.empty()) {
cout << PQ.top().first << ' ' <<PQ.top().second << '\n';
PQ.pop();
}
}
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
int K;
cin >> N >> K;
string s;
cin >> s;
int i, j;
int x = 0, y = 0;
set<P> S;
S.insert(P(0,0));
for(i=0;i<N;i++) {
if(s[i]=='E') x++;
if(s[i]=='W') x--;
if(s[i]=='N') y++;
if(s[i]=='S') y--;
S.insert(P(x,y));
}
int ans = 0;
for(P k : S) {
int x = k.first, y = k.second;
if(S.find(P(x,y+1))!=S.end()&&S.find(P(x+1,y))!=S.end()&&S.find(P(x+1,y+1))!=S.end()) {
ans++;
}
}
if(x==0&&y==0) {
cout << ans;
return 0;
}
dx = x, dy = y;
if(dx==0) {
swap(dx, dy);
for(i=0;i<N;i++) {
if(s[i]=='E') s[i]='N';
else if(s[i]=='N') s[i]='E';
else if(s[i]=='W') s[i]='S';
else if(s[i]=='S') s[i]='W';
}
}
if(dx<0) {
dx *= -1;
for(i=0;i<N;i++) {
if(s[i]=='E') s[i]='W';
else if(s[i]=='W') s[i]='E';
}
}
assert(dx>0);
x = y = 0;
M[P(0,0)].insert(0);
for(i=0;i<N;i++) {
if(s[i]=='E') x++;
if(s[i]=='W') x--;
if(s[i]=='N') y++;
if(s[i]=='S') y--;
int rx = (x%dx+dx)%dx;
int ry = y - dy * (x-rx)/dx;
M[r(x, y)].insert((x-rx)/dx);
}
/*cout << dx << ' ' << dy << '\n';
cout << s << '\n';
for(auto it = M.begin();it!=M.end();it++) {
cout << it->first.first << ' ' << it->first.second << " :\n";
for(int m : it->second) cout << m << ' ';
cout << '\n';
}*/
S = set<P> {};
x = y = 0;
S.insert(r(x,y));
S.insert(r(x,y-1));
S.insert(r(x-1,y-1));
S.insert(r(x-1,y));
for(i=0;i<N;i++) {
if(s[i]=='E') x++;
if(s[i]=='W') x--;
if(s[i]=='N') y++;
if(s[i]=='S') y--;
int rx = (x%dx+dx)%dx;
int ry = y - dy * (x-rx) / dx;
S.insert(r(x,y));
S.insert(r(x,y-1));
S.insert(r(x-1,y-1));
S.insert(r(x-1,y));
}
ans = 0;
for(P k : S) {
int x = k.first, y = k.second;
//cout << x << ' ' << y << " How Many?\n";
priority_queue<P,vector<P>,greater<P>> res;
bool isPos=true;
for(i=0;i<4;i++) {
priority_queue<P,vector<P>,greater<P>> Rang;
int x2 = x + mx[i];
int y2 = y + my[i];
//cout << x2 << ' ' << y2 << '\n';
int z = 0;
if(x2==dx) {
x2 = 0;
y2 -= dy;
z = -1;
}
if(!M.count(P(x2,y2))) {
isPos = false;
break;
}
for(int j : M[P(x2,y2)]) {
//cout << j << ' ';
Rang.push(P(j+z,j+K-1+z));
}
//cout << '\n';
//cout << "Current Res : \n";
//print(res);
//cout << "Now Rang :\n";
//print(Rang);
priority_queue<P,vector<P>,greater<P>> Res;
int st = INF;
int en = -INF;
priority_queue<P,vector<P>,greater<P>> rang;
while(!Rang.empty()) {
if(st==INF) {
st = Rang.top().first;
en = st;
}
if(Rang.top().first<=en) {
en = max(en, Rang.top().second);
Rang.pop();
}
else {
rang.push(P(st,en));
st = INF;
en = -INF;
}
}
if(st != INF) rang.push(P(st,en));
//cout << "Rang -> rang\n";
//print(rang);
if(!i) {
res = rang;
continue;
}
while(!res.empty()&&!rang.empty()) {
int x1 = res.top().first, x2 = res.top().second;
while(!rang.empty()&&rang.top().second<x1) rang.pop();
if(rang.empty()) break;
int y1 = rang.top().first, y2 = rang.top().second;
if(x2<y1) {
res.pop();
}
else {
Res.push(P(max(x1,y1),min(x2,y2)));
res.pop();
}
}
//cout << "Summation\n";
//print(Res);
res = Res;
}
if(!isPos) continue;
while(!res.empty()) {
int k1 = res.top().first, k2 = res.top().second;
ans += k2 - k1 + 1;
res.pop();
}
//cout << ans << '\n';
}
cout << ans;
}
Compilation message
2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:78:13: warning: unused variable 'ry' [-Wunused-variable]
78 | int ry = y - dy * (x-rx)/dx;
| ^~
2016_ho_t4.cpp:100:13: warning: unused variable 'ry' [-Wunused-variable]
100 | int ry = y - dy * (x-rx) / dx;
| ^~
2016_ho_t4.cpp:30:12: warning: unused variable 'j' [-Wunused-variable]
30 | int i, j;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
468 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
40 ms |
3540 KB |
Output is correct |
30 |
Correct |
50 ms |
3364 KB |
Output is correct |
31 |
Correct |
163 ms |
23368 KB |
Output is correct |
32 |
Correct |
14 ms |
1620 KB |
Output is correct |
33 |
Correct |
78 ms |
6092 KB |
Output is correct |
34 |
Correct |
78 ms |
6552 KB |
Output is correct |
35 |
Correct |
75 ms |
5672 KB |
Output is correct |
36 |
Correct |
68 ms |
4184 KB |
Output is correct |
37 |
Correct |
14 ms |
1772 KB |
Output is correct |
38 |
Correct |
79 ms |
5312 KB |
Output is correct |
39 |
Correct |
77 ms |
5788 KB |
Output is correct |
40 |
Correct |
14 ms |
1620 KB |
Output is correct |
41 |
Correct |
58 ms |
3020 KB |
Output is correct |
42 |
Correct |
56 ms |
3540 KB |
Output is correct |
43 |
Correct |
79 ms |
5672 KB |
Output is correct |
44 |
Correct |
78 ms |
5548 KB |
Output is correct |
45 |
Correct |
62 ms |
4624 KB |
Output is correct |
46 |
Correct |
103 ms |
8528 KB |
Output is correct |
47 |
Correct |
131 ms |
11364 KB |
Output is correct |
48 |
Correct |
88 ms |
11272 KB |
Output is correct |
49 |
Correct |
196 ms |
28540 KB |
Output is correct |
50 |
Correct |
191 ms |
28576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
300 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
468 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
40 ms |
3540 KB |
Output is correct |
30 |
Correct |
50 ms |
3364 KB |
Output is correct |
31 |
Correct |
163 ms |
23368 KB |
Output is correct |
32 |
Correct |
14 ms |
1620 KB |
Output is correct |
33 |
Correct |
78 ms |
6092 KB |
Output is correct |
34 |
Correct |
78 ms |
6552 KB |
Output is correct |
35 |
Correct |
75 ms |
5672 KB |
Output is correct |
36 |
Correct |
68 ms |
4184 KB |
Output is correct |
37 |
Correct |
14 ms |
1772 KB |
Output is correct |
38 |
Correct |
79 ms |
5312 KB |
Output is correct |
39 |
Correct |
77 ms |
5788 KB |
Output is correct |
40 |
Correct |
14 ms |
1620 KB |
Output is correct |
41 |
Correct |
58 ms |
3020 KB |
Output is correct |
42 |
Correct |
56 ms |
3540 KB |
Output is correct |
43 |
Correct |
79 ms |
5672 KB |
Output is correct |
44 |
Correct |
78 ms |
5548 KB |
Output is correct |
45 |
Correct |
62 ms |
4624 KB |
Output is correct |
46 |
Correct |
103 ms |
8528 KB |
Output is correct |
47 |
Correct |
131 ms |
11364 KB |
Output is correct |
48 |
Correct |
88 ms |
11272 KB |
Output is correct |
49 |
Correct |
196 ms |
28540 KB |
Output is correct |
50 |
Correct |
191 ms |
28576 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
0 ms |
212 KB |
Output is correct |
56 |
Correct |
0 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
300 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
212 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
1 ms |
212 KB |
Output is correct |
68 |
Correct |
1 ms |
212 KB |
Output is correct |
69 |
Correct |
0 ms |
212 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
5 ms |
600 KB |
Output is correct |
77 |
Correct |
2 ms |
468 KB |
Output is correct |
78 |
Correct |
25 ms |
3540 KB |
Output is correct |
79 |
Correct |
83 ms |
13620 KB |
Output is correct |
80 |
Correct |
52 ms |
3748 KB |
Output is correct |
81 |
Correct |
16 ms |
2012 KB |
Output is correct |
82 |
Correct |
78 ms |
5264 KB |
Output is correct |
83 |
Correct |
49 ms |
2764 KB |
Output is correct |
84 |
Correct |
74 ms |
5616 KB |
Output is correct |
85 |
Correct |
74 ms |
5704 KB |
Output is correct |
86 |
Correct |
124 ms |
14172 KB |
Output is correct |
87 |
Correct |
84 ms |
6904 KB |
Output is correct |
88 |
Correct |
69 ms |
4824 KB |
Output is correct |
89 |
Correct |
74 ms |
5156 KB |
Output is correct |
90 |
Correct |
15 ms |
2004 KB |
Output is correct |
91 |
Correct |
76 ms |
6180 KB |
Output is correct |
92 |
Correct |
77 ms |
6316 KB |
Output is correct |
93 |
Correct |
78 ms |
6160 KB |
Output is correct |
94 |
Correct |
62 ms |
4076 KB |
Output is correct |
95 |
Correct |
62 ms |
4348 KB |
Output is correct |
96 |
Correct |
103 ms |
8724 KB |
Output is correct |
97 |
Correct |
150 ms |
11340 KB |
Output is correct |
98 |
Correct |
79 ms |
11464 KB |
Output is correct |
99 |
Correct |
191 ms |
28656 KB |
Output is correct |
100 |
Correct |
268 ms |
22416 KB |
Output is correct |