This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <iterator>
using namespace std;
struct pos
{
int x, y;
bool operator <(const pos &p) const
{
return x != p.x ? x < p.x : y < p.y;
}
bool operator ==(const pos &p) const
{
return x == p.x && y == p.y;
}
};
struct eve
{
int x, e;
bool operator <(const eve &a) const
{
return x != a.x ? x < a.x : e < a.e;
}
};
inline int irem(int x, int y)
{
assert(y != 0);
y = abs(y);
return (x % y + y) % y;
}
int kx, ky;
pair<pos, int> decom(pos p)
{
if(kx != 0)
{
pos q;
q.x = irem(p.x, kx);
int t = (q.x - p.x) / kx;
q.y = p.y + t * ky;
return { q, t };
}
if(ky != 0)
{
pos q;
q.y = irem(p.y, ky);
int t = (q.y - p.y) / ky;
q.x = p.x + t * kx;
return { q, t };
}
return { p, 0 };
}
vector<pos> com;
inline int idx(pos p)
{
return lower_bound(com.begin(), com.end(), p) - com.begin();
}
char str[100010];
pos arr[100010];
pos base[100010];
int off[100010];
vector<int> lis[100010];
const int dx[4] = { 0, 0, 1, 1 };
const int dy[4] = { 0, 1, 0, 1 };
int main()
{
//freopen("in", "r", stdin);
//freopen("out", "w", stdout);
int n, k, i;
scanf("%d%d%s", &n, &k, str);
kx = ky = 0;
for(i = 0; i <= n; i++)
{
arr[i] = { kx, ky };
if(i == n)
break;
if(str[i] == 'E')
kx++;
else if(str[i] == 'N')
ky++;
else if(str[i] == 'W')
kx--;
else
ky--;
}
if(kx == 0 && ky == 0)
k = 1;
for(i = 0; i <= n; i++)
{
tie(base[i], off[i]) = decom(arr[i]);
com.push_back(base[i]);
}
sort(com.begin(), com.end());
com.erase(unique(com.begin(), com.end()), com.end());
int sz = com.size();
for(i = 0; i <= n; i++)
lis[idx(base[i])].push_back(off[i]);
long long res = 0;
for(i = 0; i < sz; i++)
{
int a[4];
int b[4];
bool ok = 1;
for(int d = 0; d < 4; d++)
{
pos p;
tie(p, b[d]) = decom({ com[i].x + dx[d], com[i].y + dy[d] });
a[d] = idx(p);
if(!(a[d] < com.size() && com[a[d]] == p))
{
ok = 0;
break;
}
}
if(!ok)
continue;
vector<eve> v;
for(int d = 0; d < 4; d++)
{
for(int l : lis[a[d]])
{
v.push_back({ l - b[d], d + 1 });
v.push_back({ l - b[d] + k, -(d + 1) });
}
}
sort(v.begin(), v.end());
int s[4];
memset(s, 0, sizeof s);
int cnt = 0;
bool inr = 0;
int sta;
for(int j = 0; j < v.size(); j++)
{
eve t = v[j];
if(t.e > 0)
{
t.e--;
s[t.e]++;
if(s[t.e] == 1)
cnt++;
}
else
{
t.e = -t.e - 1;
s[t.e]--;
if(s[t.e] == 0)
cnt--;
}
if(j == (int)v.size() - 1 || t.x != v[j + 1].x)
{
if(!inr && cnt == 4)
{
inr = 1;
sta = t.x;
}
else if(inr && cnt != 4)
{
inr = 0;
res += t.x - sta;
}
}
}
}
printf("%lld\n", res);
return 0;
}
Compilation message (stderr)
2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:143:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(!(a[d] < com.size() && com[a[d]] == p))
~~~~~^~~~~~~~~~~~
2016_ho_t4.cpp:169:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < v.size(); j++)
~~^~~~~~~~~~
2016_ho_t4.cpp:97:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%s", &n, &k, str);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
2016_ho_t4.cpp:197:32: warning: 'sta' may be used uninitialized in this function [-Wmaybe-uninitialized]
res += t.x - sta;
~~~~^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |