#include <bits/stdc++.h>
#define pb push_back
#define all(v) ((v).begin(), (v).end())
#define sortv(v) sort(all(v))
#define sz(v) ((int)(v).size())
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define umax(a, b) (a)=max((a), (b))
#define umin(a, b) (a)=min((a), (b))
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define rep(i,n) FOR(i,1,n)
#define rep0(i,n) FOR(i,0,(int)(n)-1)
#define FI first
#define SE second
#define INF 2000000000
#define INFLL 1000000000000000000LL
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int N, K;
string str;
vector<pii> vt;
map<pii, vector<int> > mp;
vector<pii> mrg;
vector<int> tmp;
vector<pii> mrg2;
vector<pii> mrg3;
ll ans;
void merge(){
//cout<<"****************"<<endl;
mrg2.clear();
for(int i=0; i<tmp.size(); i++){
mrg2.pb({tmp[i], tmp[i] + K-1});
// cout<<tmp[i]<<" "<<tmp[i]+K-1<<endl;
}
if(mrg.empty() || mrg2.empty()){
mrg.clear(); return;
}
//cout<<"========="<<endl;
//for(int i=0; i<mrg.size(); i++){
// cout<<mrg[i].first<<" "<<mrg[i].second<<endl;
//}
//cout<<"========="<<endl;
int t1 = mrg[0].first; int t2 = mrg2[0].first;
int n1 = 0, n2 = 0;
int i1=0, i2=0;
int cnt1=0, cnt2=0;
int now = -INF, prv=-INF;
while(1){
//cout<<"n "<<t1<<" "<<t2<<endl;
if(t1<t2 || (t1==t2 && n1==0)){
now = max(now, t1);
if(n1!=1){
cnt1++;
t1 = mrg[i1].second;
n1 = 1;
}else{
n1 = 0;
cnt1--;
i1++;
if(i1>=mrg.size()){
if(prv!=-INF){
mrg3.pb({prv, now});
}
break;
}
t1 = mrg[i1].first;
}
}else{
now = max(now, t2);
if(n2!=1){
cnt2++;
while(i2+1<mrg2.size() && mrg2[i2+1].first<=mrg2[i2].second){
i2++;
}
t2 = mrg2[i2].second;
n2 = 1;
}else{
n2 = 0;
cnt2--;
i2++;
if(i2>=mrg2.size()){
if(prv!=-INF){
mrg3.pb({prv, now});
}
break;
}
t2 = mrg2[i2].first;
}
}
if(cnt1>0 && cnt2>0){
if(prv==-INF){
prv = now;
}
}else{
if(prv!=-INF){
mrg3.pb({prv, now});
prv = -INF;
}
}
}
mrg = mrg3;
mrg3.clear();
//for(int i=0; i<mrg.size(); i++){
// cout<<mrg[i].first<<" "<<mrg[i].second<<endl;
//}
return;
}
int main(){
scanf("%d%d", &N, &K);
cin>>str;
int x = 0, y = 0;
for(int i=1; i<=N+1; i++){
vt.pb({x, y});
if(i==N+1) break;
if(str[i-1]=='N'){
y++;
}else if(str[i-1]=='E'){
x++;
}else if(str[i-1]=='W'){
x--;
}else{
y--;
}
}
if(x==0){
int tmp = x; x = y; y = tmp;
for(int i=0; i<vt.size(); i++){
tmp = vt[i].first; vt[i].first = vt[i].second; vt[i].second = tmp;
}
}
//cout<<x<<" "<<y<<endl;
for(int i=0; i<vt.size(); i++){
int t;
if(x==0){
t = 0;
}else if(x<0){
if(vt[i].first==0){
t = 0;
}
else if(vt[i].first>0){
t = vt[i].first / x;
}else{
t = vt[i].first / x + (((vt[i].first/x) * x ==vt[i].first)?0:1);
}
}else{
if(vt[i].first==0){
t = 0;
}else if(vt[i].first>0){
t = vt[i].first/x;
}else{
t = vt[i].first / x - (((vt[i].first/x) * x ==vt[i].first)?0:1);
}
}
vt[i].first -= x * t;
vt[i].second -= y * t;
//cout<<"a "<<vt[i].first<<" "<<vt[i].second<<" "<<t<<endl;
mp[vt[i]].pb(t);
if(vt[i].first==0){
if(x==0) continue;
if(x>0){
vt[i].first+=x; vt[i].second+=y;
//cout<<"a "<<vt[i].first<<" "<<vt[i].second<<" "<<t-1<<endl;
mp[vt[i]].pb(t-1);
}else{
vt[i].first-=x; vt[i].second-=y;
//cout<<"a "<<vt[i].first<<" "<<vt[i].second<<" "<<t+1<<endl;
mp[vt[i]].pb(t+1);
}
}
}
//cout<<"**"<<endl;
for(map<pii, vector<int> >::iterator it = mp.begin(); it!=mp.end(); it++){
tmp = it->second;
sort(tmp.begin(), tmp.end());
mp[it->first] = tmp;
}
for(map<pii, vector<int> >::iterator it = mp.begin(); it!=mp.end(); it++){
pii p = it->first;
//if(p.first==x) continue;
//cout<<"+"<<p.first<<" "<<p.second<<endl;
//if(x==0){
if(mp.find({(p.first+1), p.second})==mp.end() || mp.find({p.first, p.second+1})==mp.end() || mp.find({(p.first+1), p.second+1})==mp.end()) continue;
mrg.clear(); mrg2.clear();
//cout<<p.first<<" "<<p.second<<endl;
mrg.pb({-INF, INF}); tmp = mp[p]; merge();
tmp = mp[{p.first+1, p.second}]; merge();
tmp = mp[{p.first, p.second+1}]; merge();
tmp = mp[{p.first+1, p.second+1}]; merge();
if(x==0){
if(!mrg.empty()){
ans++;
}
continue;
}
for(int i=0; i<mrg.size(); i++){
ans += mrg[i].second - mrg[i].first + 1;
//cout<<'*'<<mrg[i].first<<" "<<mrg[i].second<<endl;
}
/*}else{
if(mp.find({p.first+1, p.second})==mp.end() || mp.find({p.first, p.second+1})==mp.end() || mp.find({p.first+1, p.second+1})==mp.end()) continue;
mrg.clear(); mrg2.clear();
mrg.pb({1, K}); tmp = mp[p]; merge();
tmp = mp[{(p.first+1)%x, p.second}]; merge();
tmp = mp[{p.first, p.second+1}]; merge();
tmp = mp[{(p.first+1)%x, p.second+1}]; merge();
for(int i=0; i<mrg.size(); i++){
ans += mrg[i].second - mrg[i].first;
cout<<'*'<<mrg[i].first<<" "<<mrg[i].second<<endl;
}
}*/
}
cout<<ans;
return 0;
}
Compilation message
2016_ho_t4.cpp: In function 'void merge()':
2016_ho_t4.cpp:42:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<tmp.size(); i++){
~^~~~~~~~~~~
2016_ho_t4.cpp:71:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i1>=mrg.size()){
~~^~~~~~~~~~~~
2016_ho_t4.cpp:83:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(i2+1<mrg2.size() && mrg2[i2+1].first<=mrg2[i2].second){
~~~~^~~~~~~~~~~~
2016_ho_t4.cpp:92:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i2>=mrg2.size()){
~~^~~~~~~~~~~~~
2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:139:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<vt.size(); i++){
~^~~~~~~~~~
2016_ho_t4.cpp:144:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<vt.size(); i++){
~^~~~~~~~~~
2016_ho_t4.cpp:207:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<mrg.size(); i++){
~^~~~~~~~~~~
2016_ho_t4.cpp:121:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 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 |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
256 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 |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 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 |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
256 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 |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
4 ms |
504 KB |
Output is correct |
28 |
Correct |
8 ms |
888 KB |
Output is correct |
29 |
Correct |
26 ms |
2320 KB |
Output is correct |
30 |
Correct |
32 ms |
2536 KB |
Output is correct |
31 |
Correct |
110 ms |
13036 KB |
Output is correct |
32 |
Correct |
48 ms |
3820 KB |
Output is correct |
33 |
Correct |
52 ms |
4072 KB |
Output is correct |
34 |
Correct |
53 ms |
4460 KB |
Output is correct |
35 |
Correct |
50 ms |
3948 KB |
Output is correct |
36 |
Correct |
42 ms |
3088 KB |
Output is correct |
37 |
Correct |
51 ms |
4068 KB |
Output is correct |
38 |
Correct |
59 ms |
3864 KB |
Output is correct |
39 |
Correct |
49 ms |
3948 KB |
Output is correct |
40 |
Correct |
47 ms |
3820 KB |
Output is correct |
41 |
Correct |
34 ms |
2408 KB |
Output is correct |
42 |
Correct |
41 ms |
3140 KB |
Output is correct |
43 |
Correct |
50 ms |
3816 KB |
Output is correct |
44 |
Correct |
50 ms |
3940 KB |
Output is correct |
45 |
Correct |
44 ms |
3688 KB |
Output is correct |
46 |
Correct |
84 ms |
6728 KB |
Output is correct |
47 |
Correct |
154 ms |
12212 KB |
Output is correct |
48 |
Correct |
22 ms |
4076 KB |
Output is correct |
49 |
Correct |
101 ms |
12208 KB |
Output is correct |
50 |
Correct |
100 ms |
12208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 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 |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
256 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 |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 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 |
252 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
256 KB |
Output is correct |
30 |
Correct |
2 ms |
256 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
292 KB |
Output is correct |
33 |
Correct |
2 ms |
396 KB |
Output is correct |
34 |
Correct |
2 ms |
256 KB |
Output is correct |
35 |
Correct |
2 ms |
376 KB |
Output is correct |
36 |
Correct |
2 ms |
256 KB |
Output is correct |
37 |
Correct |
2 ms |
256 KB |
Output is correct |
38 |
Correct |
2 ms |
380 KB |
Output is correct |
39 |
Correct |
2 ms |
296 KB |
Output is correct |
40 |
Correct |
2 ms |
256 KB |
Output is correct |
41 |
Correct |
2 ms |
256 KB |
Output is correct |
42 |
Correct |
2 ms |
256 KB |
Output is correct |
43 |
Correct |
2 ms |
256 KB |
Output is correct |
44 |
Correct |
2 ms |
376 KB |
Output is correct |
45 |
Correct |
2 ms |
252 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 |
252 KB |
Output is correct |
49 |
Correct |
2 ms |
256 KB |
Output is correct |
50 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 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 |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
256 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 |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
4 ms |
504 KB |
Output is correct |
28 |
Correct |
8 ms |
888 KB |
Output is correct |
29 |
Correct |
26 ms |
2320 KB |
Output is correct |
30 |
Correct |
32 ms |
2536 KB |
Output is correct |
31 |
Correct |
110 ms |
13036 KB |
Output is correct |
32 |
Correct |
48 ms |
3820 KB |
Output is correct |
33 |
Correct |
52 ms |
4072 KB |
Output is correct |
34 |
Correct |
53 ms |
4460 KB |
Output is correct |
35 |
Correct |
50 ms |
3948 KB |
Output is correct |
36 |
Correct |
42 ms |
3088 KB |
Output is correct |
37 |
Correct |
51 ms |
4068 KB |
Output is correct |
38 |
Correct |
59 ms |
3864 KB |
Output is correct |
39 |
Correct |
49 ms |
3948 KB |
Output is correct |
40 |
Correct |
47 ms |
3820 KB |
Output is correct |
41 |
Correct |
34 ms |
2408 KB |
Output is correct |
42 |
Correct |
41 ms |
3140 KB |
Output is correct |
43 |
Correct |
50 ms |
3816 KB |
Output is correct |
44 |
Correct |
50 ms |
3940 KB |
Output is correct |
45 |
Correct |
44 ms |
3688 KB |
Output is correct |
46 |
Correct |
84 ms |
6728 KB |
Output is correct |
47 |
Correct |
154 ms |
12212 KB |
Output is correct |
48 |
Correct |
22 ms |
4076 KB |
Output is correct |
49 |
Correct |
101 ms |
12208 KB |
Output is correct |
50 |
Correct |
100 ms |
12208 KB |
Output is correct |
51 |
Correct |
2 ms |
376 KB |
Output is correct |
52 |
Correct |
2 ms |
252 KB |
Output is correct |
53 |
Correct |
2 ms |
376 KB |
Output is correct |
54 |
Correct |
2 ms |
256 KB |
Output is correct |
55 |
Correct |
2 ms |
256 KB |
Output is correct |
56 |
Correct |
2 ms |
376 KB |
Output is correct |
57 |
Correct |
2 ms |
292 KB |
Output is correct |
58 |
Correct |
2 ms |
396 KB |
Output is correct |
59 |
Correct |
2 ms |
256 KB |
Output is correct |
60 |
Correct |
2 ms |
376 KB |
Output is correct |
61 |
Correct |
2 ms |
256 KB |
Output is correct |
62 |
Correct |
2 ms |
256 KB |
Output is correct |
63 |
Correct |
2 ms |
380 KB |
Output is correct |
64 |
Correct |
2 ms |
296 KB |
Output is correct |
65 |
Correct |
2 ms |
256 KB |
Output is correct |
66 |
Correct |
2 ms |
256 KB |
Output is correct |
67 |
Correct |
2 ms |
256 KB |
Output is correct |
68 |
Correct |
2 ms |
256 KB |
Output is correct |
69 |
Correct |
2 ms |
376 KB |
Output is correct |
70 |
Correct |
2 ms |
252 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 |
252 KB |
Output is correct |
74 |
Correct |
2 ms |
256 KB |
Output is correct |
75 |
Correct |
2 ms |
376 KB |
Output is correct |
76 |
Correct |
5 ms |
504 KB |
Output is correct |
77 |
Correct |
6 ms |
632 KB |
Output is correct |
78 |
Correct |
20 ms |
2292 KB |
Output is correct |
79 |
Correct |
53 ms |
7360 KB |
Output is correct |
80 |
Correct |
34 ms |
2540 KB |
Output is correct |
81 |
Correct |
52 ms |
4332 KB |
Output is correct |
82 |
Correct |
48 ms |
3820 KB |
Output is correct |
83 |
Correct |
32 ms |
2536 KB |
Output is correct |
84 |
Correct |
48 ms |
3944 KB |
Output is correct |
85 |
Correct |
51 ms |
4076 KB |
Output is correct |
86 |
Correct |
91 ms |
7788 KB |
Output is correct |
87 |
Correct |
52 ms |
4460 KB |
Output is correct |
88 |
Correct |
45 ms |
3688 KB |
Output is correct |
89 |
Correct |
48 ms |
3760 KB |
Output is correct |
90 |
Correct |
51 ms |
4252 KB |
Output is correct |
91 |
Correct |
50 ms |
4204 KB |
Output is correct |
92 |
Correct |
50 ms |
4200 KB |
Output is correct |
93 |
Correct |
49 ms |
4332 KB |
Output is correct |
94 |
Correct |
42 ms |
3308 KB |
Output is correct |
95 |
Correct |
41 ms |
3356 KB |
Output is correct |
96 |
Correct |
84 ms |
6892 KB |
Output is correct |
97 |
Correct |
153 ms |
12268 KB |
Output is correct |
98 |
Correct |
20 ms |
3308 KB |
Output is correct |
99 |
Correct |
101 ms |
12320 KB |
Output is correct |
100 |
Correct |
225 ms |
17768 KB |
Output is correct |