#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N=100050;
const int inf=1e9+7;
int n,k;
char s[N];
int dx,dy;
map<pair<ll,int>,set<int>> ps,qs;
map<pair<ll,int>,set<pair<ll,ll>>> intervals;
map<pair<ll,int>,int> Y;
vector<pair<int,int>> sq;
map<pair<ll,int>,int> dp;
int md(int x, int dx){ return (x%dx+dx)%dx;}
void SQ(int x, int y)
{
ll z=(ll)y*dx-(ll)x*dy;
Y[{z,x}]=y;
qs[{z,md(x,dx)}].insert(x);
sq.pb({x,y});
}
void Add(int x, int y)
{
ll z=(ll)y*dx-(ll)x*dy;
ps[{z,md(x,dx)}].insert(x);
SQ(x,y);
SQ(x-1,y);
SQ(x,y-1);
SQ(x-1,y-1);
}
int Get(int x, int y)
{
ll z=(ll)y*dx-(ll)x*dy;
auto it=ps[{z,md(x,dx)}].lower_bound(x);
if(it==ps[{z,md(x,dx)}].end()) return inf;
assert((*it-x)%dx==0);
return (*it-x)/dx;
}
void Solve()
{
int x=0,y=0;
Add(x,y);
for(int i=1;i<=n;i++)
{
if(s[i]=='E') y++;
if(s[i]=='W') y--;
if(s[i]=='N') x++;
if(s[i]=='S') x--;
Add(x,y);
}
ll ans=0;
sort(sq.begin(),sq.end());
sq.erase(unique(sq.begin(),sq.end()),sq.end());
for(auto q:sq)
{
tie(x,y)=q;
ll z=(ll)y*dx-(ll)x*dy;
dp[q]=max({Get(x,y),Get(x+1,y),Get(x,y+1),Get(x+1,y+1)});
if(dp[q]<k) intervals[{z,md(x,dx)}].insert({x+(ll)dp[q]*dx,x+(ll)k*dx});
}
for(auto st:intervals)
{
ll r=-9e18;
for(auto i:st.second)
{
ll a,b;tie(a,b)=i;
if(r<a) ans+=(b-a)/dx,r=b;
else if(r<b) ans+=(b-r)/dx,r=b;
}
}
printf("%lld\n",ans);
}
void Brute()
{
set<pair<int,int>> pp;
int x=0,y=0;
pp.insert({x,y});
for(int i=1;i<=n;i++)
{
if(s[i]=='E') y++;
if(s[i]=='W') y--;
if(s[i]=='N') x++;
if(s[i]=='S') x--;
pp.insert({x,y});
}
int ans=0;
for(auto p:pp)
{
tie(x,y)=p;
if(pp.count({x,y}) && pp.count({x+1,y}) && pp.count({x,y+1}) && pp.count({x+1,y+1})) ans++;
}
printf("%i\n",ans);
}
int main()
{
int x=0,y=0;
scanf("%i %i",&n,&k);
scanf("%s",s+1);
for(int i=1;i<=n;i++)
{
if(s[i]=='E') y++;
if(s[i]=='W') y--;
if(s[i]=='N') x++;
if(s[i]=='S') x--;
}
if(y<0) for(int i=1;i<=n;i++){ if(s[i]=='E') s[i]='W';else if(s[i]=='W') s[i]='E';}
if(x<0) for(int i=1;i<=n;i++){ if(s[i]=='N') s[i]='S';else if(s[i]=='S') s[i]='N';}
y=abs(y);x=abs(x);
if(x==0)
{
for(int i=1;i<=n;i++)
{
if(s[i]=='E') s[i]='N';
else if(s[i]=='W') s[i]='S';
else if(s[i]=='N') s[i]='E';
else if(s[i]=='S') s[i]='W';
}
swap(x,y);
}
dx=x;dy=y;
if(dx==0) Brute();
else Solve();
return 0;
}
Compilation message
2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&k);
~~~~~^~~~~~~~~~~~~~~
2016_ho_t4.cpp:99:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",s+1);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
252 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
380 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
252 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
380 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
380 KB |
Output is correct |
27 |
Correct |
10 ms |
1396 KB |
Output is correct |
28 |
Correct |
5 ms |
536 KB |
Output is correct |
29 |
Correct |
105 ms |
11492 KB |
Output is correct |
30 |
Correct |
131 ms |
11488 KB |
Output is correct |
31 |
Correct |
595 ms |
80120 KB |
Output is correct |
32 |
Correct |
22 ms |
1528 KB |
Output is correct |
33 |
Correct |
230 ms |
20268 KB |
Output is correct |
34 |
Correct |
217 ms |
21852 KB |
Output is correct |
35 |
Correct |
218 ms |
19140 KB |
Output is correct |
36 |
Correct |
177 ms |
14556 KB |
Output is correct |
37 |
Correct |
25 ms |
1656 KB |
Output is correct |
38 |
Correct |
211 ms |
18204 KB |
Output is correct |
39 |
Correct |
210 ms |
19420 KB |
Output is correct |
40 |
Correct |
23 ms |
1452 KB |
Output is correct |
41 |
Correct |
159 ms |
11228 KB |
Output is correct |
42 |
Correct |
152 ms |
13276 KB |
Output is correct |
43 |
Correct |
297 ms |
19552 KB |
Output is correct |
44 |
Correct |
206 ms |
18652 KB |
Output is correct |
45 |
Correct |
179 ms |
16260 KB |
Output is correct |
46 |
Correct |
331 ms |
28732 KB |
Output is correct |
47 |
Correct |
360 ms |
34140 KB |
Output is correct |
48 |
Correct |
520 ms |
42892 KB |
Output is correct |
49 |
Correct |
827 ms |
97612 KB |
Output is correct |
50 |
Correct |
885 ms |
97772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
252 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
380 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
256 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
2 ms |
376 KB |
Output is correct |
35 |
Correct |
2 ms |
376 KB |
Output is correct |
36 |
Correct |
2 ms |
376 KB |
Output is correct |
37 |
Correct |
3 ms |
376 KB |
Output is correct |
38 |
Correct |
2 ms |
348 KB |
Output is correct |
39 |
Correct |
2 ms |
376 KB |
Output is correct |
40 |
Correct |
2 ms |
256 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
3 ms |
376 KB |
Output is correct |
43 |
Correct |
2 ms |
376 KB |
Output is correct |
44 |
Correct |
2 ms |
376 KB |
Output is correct |
45 |
Correct |
2 ms |
376 KB |
Output is correct |
46 |
Correct |
2 ms |
376 KB |
Output is correct |
47 |
Correct |
2 ms |
376 KB |
Output is correct |
48 |
Correct |
2 ms |
376 KB |
Output is correct |
49 |
Correct |
2 ms |
404 KB |
Output is correct |
50 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
252 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
380 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
380 KB |
Output is correct |
27 |
Correct |
10 ms |
1396 KB |
Output is correct |
28 |
Correct |
5 ms |
536 KB |
Output is correct |
29 |
Correct |
105 ms |
11492 KB |
Output is correct |
30 |
Correct |
131 ms |
11488 KB |
Output is correct |
31 |
Correct |
595 ms |
80120 KB |
Output is correct |
32 |
Correct |
22 ms |
1528 KB |
Output is correct |
33 |
Correct |
230 ms |
20268 KB |
Output is correct |
34 |
Correct |
217 ms |
21852 KB |
Output is correct |
35 |
Correct |
218 ms |
19140 KB |
Output is correct |
36 |
Correct |
177 ms |
14556 KB |
Output is correct |
37 |
Correct |
25 ms |
1656 KB |
Output is correct |
38 |
Correct |
211 ms |
18204 KB |
Output is correct |
39 |
Correct |
210 ms |
19420 KB |
Output is correct |
40 |
Correct |
23 ms |
1452 KB |
Output is correct |
41 |
Correct |
159 ms |
11228 KB |
Output is correct |
42 |
Correct |
152 ms |
13276 KB |
Output is correct |
43 |
Correct |
297 ms |
19552 KB |
Output is correct |
44 |
Correct |
206 ms |
18652 KB |
Output is correct |
45 |
Correct |
179 ms |
16260 KB |
Output is correct |
46 |
Correct |
331 ms |
28732 KB |
Output is correct |
47 |
Correct |
360 ms |
34140 KB |
Output is correct |
48 |
Correct |
520 ms |
42892 KB |
Output is correct |
49 |
Correct |
827 ms |
97612 KB |
Output is correct |
50 |
Correct |
885 ms |
97772 KB |
Output is correct |
51 |
Correct |
2 ms |
376 KB |
Output is correct |
52 |
Correct |
2 ms |
256 KB |
Output is correct |
53 |
Correct |
2 ms |
376 KB |
Output is correct |
54 |
Correct |
2 ms |
376 KB |
Output is correct |
55 |
Correct |
2 ms |
376 KB |
Output is correct |
56 |
Correct |
2 ms |
376 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
2 ms |
376 KB |
Output is correct |
59 |
Correct |
2 ms |
376 KB |
Output is correct |
60 |
Correct |
2 ms |
376 KB |
Output is correct |
61 |
Correct |
2 ms |
376 KB |
Output is correct |
62 |
Correct |
3 ms |
376 KB |
Output is correct |
63 |
Correct |
2 ms |
348 KB |
Output is correct |
64 |
Correct |
2 ms |
376 KB |
Output is correct |
65 |
Correct |
2 ms |
256 KB |
Output is correct |
66 |
Correct |
2 ms |
376 KB |
Output is correct |
67 |
Correct |
3 ms |
376 KB |
Output is correct |
68 |
Correct |
2 ms |
376 KB |
Output is correct |
69 |
Correct |
2 ms |
376 KB |
Output is correct |
70 |
Correct |
2 ms |
376 KB |
Output is correct |
71 |
Correct |
2 ms |
376 KB |
Output is correct |
72 |
Correct |
2 ms |
376 KB |
Output is correct |
73 |
Correct |
2 ms |
376 KB |
Output is correct |
74 |
Correct |
2 ms |
404 KB |
Output is correct |
75 |
Correct |
2 ms |
376 KB |
Output is correct |
76 |
Correct |
13 ms |
1396 KB |
Output is correct |
77 |
Correct |
4 ms |
376 KB |
Output is correct |
78 |
Correct |
99 ms |
13228 KB |
Output is correct |
79 |
Correct |
547 ms |
45800 KB |
Output is correct |
80 |
Correct |
137 ms |
12664 KB |
Output is correct |
81 |
Correct |
24 ms |
1656 KB |
Output is correct |
82 |
Correct |
201 ms |
17500 KB |
Output is correct |
83 |
Correct |
142 ms |
10352 KB |
Output is correct |
84 |
Correct |
218 ms |
18780 KB |
Output is correct |
85 |
Correct |
188 ms |
18908 KB |
Output is correct |
86 |
Correct |
669 ms |
60508 KB |
Output is correct |
87 |
Correct |
232 ms |
22492 KB |
Output is correct |
88 |
Correct |
188 ms |
16476 KB |
Output is correct |
89 |
Correct |
205 ms |
17520 KB |
Output is correct |
90 |
Correct |
24 ms |
1656 KB |
Output is correct |
91 |
Correct |
199 ms |
20120 KB |
Output is correct |
92 |
Correct |
217 ms |
20444 KB |
Output is correct |
93 |
Correct |
232 ms |
20828 KB |
Output is correct |
94 |
Correct |
169 ms |
14128 KB |
Output is correct |
95 |
Correct |
180 ms |
15172 KB |
Output is correct |
96 |
Correct |
367 ms |
36252 KB |
Output is correct |
97 |
Correct |
380 ms |
42832 KB |
Output is correct |
98 |
Correct |
552 ms |
52288 KB |
Output is correct |
99 |
Correct |
827 ms |
97696 KB |
Output is correct |
100 |
Correct |
778 ms |
73436 KB |
Output is correct |