# |
제출 시각 |
아이디 |
문제 |
언어 |
결과 |
실행 시간 |
메모리 |
594142 |
2022-07-12T06:57:04 Z |
박상훈(#8431) |
영역 (JOI16_ho_t4) |
C++17 |
|
95 ms |
11480 KB |
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n, k, dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1}, cc[1234];
char s[100100];
map<pair<ll, ll>, vector<pair<ll, ll>>> M;
ll a, b, d, ans;
ll myabs(ll x){
if (x<0) return -x;
return x;
}
pair<ll, ll> _convert(ll x, ll y){
ll s = ((x+b*(int)1e9)%b)/(b/d)%d;
return {a*x+b*y, s};
}
ll fx = 0, fy = 0;
void simulate(bool flag){
if (flag) M[_convert(0, 0)].emplace_back(fx, fx+(ll)k*b);
for (int i=1;i<=n;i++){
fx += dx[cc[s[i]]], fy += dy[cc[s[i]]];
if (flag){
auto p = _convert(fx, fy);
M[p].emplace_back(fx, fx + (ll)k*b);
}
//if (flag)printf(" %lld %lld\n", fx, fy);
}
}
void compress(vector<pair<ll, ll>> &V){
vector<pair<ll, ll>> ret;
sort(V.begin(), V.end());
for (auto &[s, e]:V){
if (ret.empty()) {ret.emplace_back(s, e); continue;}
if (ret.back().second>=s) ret.back().second = e;
else ret.emplace_back(s, e);
}
swap(V, ret);
}
void init(){
cc['E'] = 0, cc['S'] = 1, cc['W'] = 2, cc['N'] = 3;
simulate(0);
if (fx==0 && fy==0) {fx = 1; k = 1;}
else if (fx==0){
cc['E'] = 3, cc['N'] = 0;
cc['S'] = 2, cc['W'] = 1;
fx = 0, fy = 0;
simulate(0);
assert(fx);
}
if (fx<0){
if (cc['E'] == 0) swap(cc['E'], cc['W']);
else swap(cc['S'], cc['N']);
fx = 0, fy = 0;
simulate(0);
assert(fx>0);
}
d = myabs(__gcd(fx, fy));
a = -fy, b = fx;
fx = 0, fy = 0;
//printf(" a = %lld, b = %lld, d = %lld\n", a, b, d);
simulate(1);
for (auto &[p, V]:M) compress(V);
//printf("Init ok\n");
}
void get_event(vector<pair<ll, int>> &Event, vector<pair<ll, ll>> &V, int ofs){
for (auto &[s, e]:V){
Event.emplace_back(s+ofs, 1);
Event.emplace_back(e+ofs, -1);
}
}
bool valid(ll x, ll y){
if (M.find(_convert(x, y+1))==M.end()) return 0;
if (M.find(_convert(x+1, y))==M.end()) return 0;
if (M.find(_convert(x+1, y+1))==M.end()) return 0;
return 1;
}
void solve(){
for (auto &[p, V]:M){
//printf(" %lld: %d\n", p.first, (int)V.size());
ll x = V[0].first;
ll y = (p.first - x*a) / b;
if (!valid(x, y)) continue;
//printf("YES: %lld %lld\n", x, y);
vector<pair<ll, int>> Event;
get_event(Event, V, 0);
get_event(Event, M[_convert(x, y+1)], 0);
get_event(Event, M[_convert(x+1, y)], -1);
get_event(Event, M[_convert(x+1, y+1)], -1);
sort(Event.begin(), Event.end());
//for (auto [t, i]:Event) printf(" %lld -> %d\n", t, i);
int cnt = 0;
for (int i=0;i<(int)Event.size();){
int r = i;
for (;r<(int)Event.size();r++){
if (Event[i].first!=Event[r].first) break;
cnt += Event[r].second;
}
assert(cnt>=0 && cnt<=4);
if (cnt==4){
assert(r<(int)Event.size());
ans += (Event[r].first - Event[i].first) / b;
}
assert(i<r);
i = r;
}
//printf(" ok %lld\n", ans);
}
}
mt19937 seed(129438);
uniform_int_distribution<int> rng(0, 2147483647);
int getrand(int l, int r){return rng(seed)%(r-l+1) + l;}
namespace NAIVE{
set<pair<int, int>> visited, made;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1}, cc[1234];
char s[100100];
bool ok(int x, int y){
return visited.find(make_pair(x, y))!=visited.end();
}
int _make(int x, int y){
if (made.find(make_pair(x, y))!=made.end()) return 0;
if (ok(x, y) && ok(x+1, y) && ok(x, y+1) && ok(x+1, y+1)){
made.emplace(x, y);
return 1;
}
return 0;
}
ll solve(int n, int k, char t[]){
visited.clear();
made.clear();
for (int i=1;i<=n;i++) s[i] = t[i];
s[n+1] = 0;
cc['E'] = 0, cc['S'] = 1, cc['W'] = 2, cc['N'] = 3;
int prv = 1e9, x = 0, y = 0, c = 0;
ll ans = 0;
visited.emplace(0, 0);
while(k){
int cnt = 0;
for (int i=1;i<=n;i++){
x += dx[cc[s[i]]], y += dy[cc[s[i]]];
visited.emplace(x, y);
cnt += _make(x, y);
cnt += _make(x, y-1);
cnt += _make(x-1, y);
cnt += _make(x-1, y-1);
}
ans += cnt;
--k;
++c;
if (c>100) break;
//if (prv==cnt) break;
prv = cnt;
//printf("%d\n", cnt);
}
ans += (ll)prv * k;
return ans;
return 0;
}
}
ll solve(int N, int K, char t[]){
n = N, k = K;
fx = 0, fy = 0;
a = 0, b = 0, d = 0, ans = 0;
M.clear();
for (int i=1;i<=n;i++) s[i] = t[i];
s[n+1] = 0;
/*for (int i=1;i<=n;i++){
char t[10] = "xESWN";
s[i] = t[getrand(1, 4)];
printf("%c", s[i]);
}
printf("\n");*/
init();
solve();
return ans;
}
char t[100100];
const int MAXN = 20, MAXK = 50;
void stress(int tc){
printf("----------------------\n");
printf("Stress #%d\n", tc);
int N = getrand(1, MAXN), K = getrand(1, MAXK);
for (int i=1;i<=N;i++){
char T[10] = "xESWN";
t[i] = T[getrand(1, 4)];
}
t[N+1] = 0;
printf("Input:\n");
printf("%d %d\n", N, K);
printf("%s\n", t+1);
ll ans = NAIVE::solve(N, K, t);
ll out = solve(N, K, t);
printf("answer: %lld\n", ans);
printf("output: %lld\n", out);
assert(ans==out);
}
int main(){
int tc = 0;
//scanf("%d", &tc);
if (true){
int n, k;
scanf("%d %d", &n, &k);
scanf("%s", t+1);
printf("%lld\n", solve(n, k, t));
//printf(" ans = %lld\n", NAIVE::solve(n, k, t));
}
for (int i=1;i<=tc;i++) stress(i);
}
Compilation message
2016_ho_t4.cpp: In function 'void simulate(bool)':
2016_ho_t4.cpp:27:24: warning: array subscript has type 'char' [-Wchar-subscripts]
27 | fx += dx[cc[s[i]]], fy += dy[cc[s[i]]];
| ~~~^
2016_ho_t4.cpp:27:44: warning: array subscript has type 'char' [-Wchar-subscripts]
27 | fx += dx[cc[s[i]]], fy += dy[cc[s[i]]];
| ~~~^
2016_ho_t4.cpp: In function 'll NAIVE::solve(int, int, char*)':
2016_ho_t4.cpp:176:27: warning: array subscript has type 'char' [-Wchar-subscripts]
176 | x += dx[cc[s[i]]], y += dy[cc[s[i]]];
| ~~~^
2016_ho_t4.cpp:176:46: warning: array subscript has type 'char' [-Wchar-subscripts]
176 | x += dx[cc[s[i]]], y += dy[cc[s[i]]];
| ~~~^
2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:252:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
252 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
2016_ho_t4.cpp:253:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
253 | scanf("%s", t+1);
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
232 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
232 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
17 ms |
2644 KB |
Output is correct |
30 |
Correct |
17 ms |
3092 KB |
Output is correct |
31 |
Correct |
38 ms |
9812 KB |
Output is correct |
32 |
Correct |
10 ms |
2684 KB |
Output is correct |
33 |
Correct |
37 ms |
4748 KB |
Output is correct |
34 |
Correct |
39 ms |
4832 KB |
Output is correct |
35 |
Correct |
31 ms |
4600 KB |
Output is correct |
36 |
Correct |
24 ms |
3856 KB |
Output is correct |
37 |
Correct |
11 ms |
2900 KB |
Output is correct |
38 |
Correct |
30 ms |
4488 KB |
Output is correct |
39 |
Correct |
31 ms |
4644 KB |
Output is correct |
40 |
Correct |
9 ms |
2900 KB |
Output is correct |
41 |
Correct |
23 ms |
3284 KB |
Output is correct |
42 |
Correct |
27 ms |
3796 KB |
Output is correct |
43 |
Correct |
33 ms |
4572 KB |
Output is correct |
44 |
Correct |
33 ms |
4556 KB |
Output is correct |
45 |
Correct |
27 ms |
4268 KB |
Output is correct |
46 |
Correct |
37 ms |
5204 KB |
Output is correct |
47 |
Correct |
56 ms |
8316 KB |
Output is correct |
48 |
Correct |
6 ms |
2760 KB |
Output is correct |
49 |
Correct |
63 ms |
11408 KB |
Output is correct |
50 |
Correct |
49 ms |
11348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
232 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 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 |
212 KB |
Output is correct |
36 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
0 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
232 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
17 ms |
2644 KB |
Output is correct |
30 |
Correct |
17 ms |
3092 KB |
Output is correct |
31 |
Correct |
38 ms |
9812 KB |
Output is correct |
32 |
Correct |
10 ms |
2684 KB |
Output is correct |
33 |
Correct |
37 ms |
4748 KB |
Output is correct |
34 |
Correct |
39 ms |
4832 KB |
Output is correct |
35 |
Correct |
31 ms |
4600 KB |
Output is correct |
36 |
Correct |
24 ms |
3856 KB |
Output is correct |
37 |
Correct |
11 ms |
2900 KB |
Output is correct |
38 |
Correct |
30 ms |
4488 KB |
Output is correct |
39 |
Correct |
31 ms |
4644 KB |
Output is correct |
40 |
Correct |
9 ms |
2900 KB |
Output is correct |
41 |
Correct |
23 ms |
3284 KB |
Output is correct |
42 |
Correct |
27 ms |
3796 KB |
Output is correct |
43 |
Correct |
33 ms |
4572 KB |
Output is correct |
44 |
Correct |
33 ms |
4556 KB |
Output is correct |
45 |
Correct |
27 ms |
4268 KB |
Output is correct |
46 |
Correct |
37 ms |
5204 KB |
Output is correct |
47 |
Correct |
56 ms |
8316 KB |
Output is correct |
48 |
Correct |
6 ms |
2760 KB |
Output is correct |
49 |
Correct |
63 ms |
11408 KB |
Output is correct |
50 |
Correct |
49 ms |
11348 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 |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
0 ms |
212 KB |
Output is correct |
57 |
Correct |
0 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 |
212 KB |
Output is correct |
61 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
68 |
Correct |
0 ms |
212 KB |
Output is correct |
69 |
Correct |
1 ms |
212 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
2 ms |
468 KB |
Output is correct |
77 |
Correct |
1 ms |
468 KB |
Output is correct |
78 |
Correct |
9 ms |
1620 KB |
Output is correct |
79 |
Correct |
24 ms |
5748 KB |
Output is correct |
80 |
Correct |
20 ms |
3156 KB |
Output is correct |
81 |
Correct |
10 ms |
2772 KB |
Output is correct |
82 |
Correct |
30 ms |
4556 KB |
Output is correct |
83 |
Correct |
16 ms |
3448 KB |
Output is correct |
84 |
Correct |
29 ms |
4692 KB |
Output is correct |
85 |
Correct |
32 ms |
4708 KB |
Output is correct |
86 |
Correct |
32 ms |
5324 KB |
Output is correct |
87 |
Correct |
33 ms |
5036 KB |
Output is correct |
88 |
Correct |
33 ms |
4396 KB |
Output is correct |
89 |
Correct |
28 ms |
4448 KB |
Output is correct |
90 |
Correct |
10 ms |
2900 KB |
Output is correct |
91 |
Correct |
40 ms |
4804 KB |
Output is correct |
92 |
Correct |
34 ms |
4908 KB |
Output is correct |
93 |
Correct |
35 ms |
4832 KB |
Output is correct |
94 |
Correct |
25 ms |
4084 KB |
Output is correct |
95 |
Correct |
24 ms |
4028 KB |
Output is correct |
96 |
Correct |
32 ms |
5204 KB |
Output is correct |
97 |
Correct |
58 ms |
8376 KB |
Output is correct |
98 |
Correct |
6 ms |
2888 KB |
Output is correct |
99 |
Correct |
59 ms |
11456 KB |
Output is correct |
100 |
Correct |
95 ms |
11480 KB |
Output is correct |