#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 2e5 + 69, LOG_N = 18;
const int INF = 1e9 + 69;
int nxt[3][LOG_N][N];
int lift(int j, int i, int d){
for(int t = 0; t < LOG_N; ++t) if (GETBIT(d, t))
i = nxt[j][t][i];
return i;
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, k; cin >> n >> k;
string s; cin >> s;
for(int j = 0; j<3; ++j) nxt[j][0][n] = n;
for(int i = n-1; i>=0; --i){
for(int j = 0; j < 3; ++j) nxt[j][0][i] = nxt[j][0][i+1];
if (i < n - 1){
if (s[i + 1] == 'J') nxt[0][0][i] = i + 1;
else if (s[i+1] == 'O') nxt[1][0][i] = i + 1;
else nxt[2][0][i] = i + 1;
}
}
for(int j = 0; j < 3; ++j){
for(int t = 1; t < LOG_N; ++ t){
for(int i = 0; i<=n; ++i)
nxt[j][t][i] = nxt[j][t-1][nxt[j][t-1][i]];
}
}
int ans = INF;
for(int i = 0; i<n; ++i) if (s[i] == 'J'){
int j = i;
j = lift(0, j, k-1);
j = lift(1, j, k);
j = lift(2, j, k);
if (j == n) break;
minimize(ans, j - i + 1 - 3 * k);
}
if (ans == INF) cout << -1 << "\n";
else cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
41304 KB |
Output is correct |
2 |
Correct |
5 ms |
41308 KB |
Output is correct |
3 |
Correct |
5 ms |
41308 KB |
Output is correct |
4 |
Correct |
5 ms |
41308 KB |
Output is correct |
5 |
Correct |
5 ms |
41308 KB |
Output is correct |
6 |
Correct |
5 ms |
41308 KB |
Output is correct |
7 |
Correct |
5 ms |
41308 KB |
Output is correct |
8 |
Correct |
5 ms |
41308 KB |
Output is correct |
9 |
Correct |
5 ms |
41312 KB |
Output is correct |
10 |
Correct |
4 ms |
41308 KB |
Output is correct |
11 |
Correct |
5 ms |
41504 KB |
Output is correct |
12 |
Correct |
5 ms |
41304 KB |
Output is correct |
13 |
Correct |
5 ms |
41476 KB |
Output is correct |
14 |
Correct |
5 ms |
41308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
41304 KB |
Output is correct |
2 |
Correct |
5 ms |
41308 KB |
Output is correct |
3 |
Correct |
5 ms |
41308 KB |
Output is correct |
4 |
Correct |
5 ms |
41308 KB |
Output is correct |
5 |
Correct |
5 ms |
41308 KB |
Output is correct |
6 |
Correct |
5 ms |
41308 KB |
Output is correct |
7 |
Correct |
5 ms |
41308 KB |
Output is correct |
8 |
Correct |
5 ms |
41308 KB |
Output is correct |
9 |
Correct |
5 ms |
41312 KB |
Output is correct |
10 |
Correct |
4 ms |
41308 KB |
Output is correct |
11 |
Correct |
5 ms |
41504 KB |
Output is correct |
12 |
Correct |
5 ms |
41304 KB |
Output is correct |
13 |
Correct |
5 ms |
41476 KB |
Output is correct |
14 |
Correct |
5 ms |
41308 KB |
Output is correct |
15 |
Correct |
6 ms |
41312 KB |
Output is correct |
16 |
Correct |
5 ms |
41472 KB |
Output is correct |
17 |
Correct |
5 ms |
41308 KB |
Output is correct |
18 |
Correct |
5 ms |
41508 KB |
Output is correct |
19 |
Correct |
5 ms |
41312 KB |
Output is correct |
20 |
Correct |
5 ms |
41420 KB |
Output is correct |
21 |
Correct |
5 ms |
41304 KB |
Output is correct |
22 |
Correct |
5 ms |
41308 KB |
Output is correct |
23 |
Correct |
5 ms |
41308 KB |
Output is correct |
24 |
Correct |
5 ms |
41308 KB |
Output is correct |
25 |
Correct |
5 ms |
41456 KB |
Output is correct |
26 |
Correct |
5 ms |
41308 KB |
Output is correct |
27 |
Correct |
5 ms |
41308 KB |
Output is correct |
28 |
Correct |
5 ms |
41308 KB |
Output is correct |
29 |
Correct |
5 ms |
41304 KB |
Output is correct |
30 |
Correct |
5 ms |
41308 KB |
Output is correct |
31 |
Correct |
5 ms |
41312 KB |
Output is correct |
32 |
Correct |
5 ms |
41452 KB |
Output is correct |
33 |
Correct |
5 ms |
41312 KB |
Output is correct |
34 |
Correct |
5 ms |
41312 KB |
Output is correct |
35 |
Correct |
5 ms |
41564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
41304 KB |
Output is correct |
2 |
Correct |
5 ms |
41308 KB |
Output is correct |
3 |
Correct |
5 ms |
41308 KB |
Output is correct |
4 |
Correct |
5 ms |
41308 KB |
Output is correct |
5 |
Correct |
5 ms |
41308 KB |
Output is correct |
6 |
Correct |
5 ms |
41308 KB |
Output is correct |
7 |
Correct |
5 ms |
41308 KB |
Output is correct |
8 |
Correct |
5 ms |
41308 KB |
Output is correct |
9 |
Correct |
5 ms |
41312 KB |
Output is correct |
10 |
Correct |
4 ms |
41308 KB |
Output is correct |
11 |
Correct |
5 ms |
41504 KB |
Output is correct |
12 |
Correct |
5 ms |
41304 KB |
Output is correct |
13 |
Correct |
5 ms |
41476 KB |
Output is correct |
14 |
Correct |
5 ms |
41308 KB |
Output is correct |
15 |
Correct |
6 ms |
41312 KB |
Output is correct |
16 |
Correct |
5 ms |
41472 KB |
Output is correct |
17 |
Correct |
5 ms |
41308 KB |
Output is correct |
18 |
Correct |
5 ms |
41508 KB |
Output is correct |
19 |
Correct |
5 ms |
41312 KB |
Output is correct |
20 |
Correct |
5 ms |
41420 KB |
Output is correct |
21 |
Correct |
5 ms |
41304 KB |
Output is correct |
22 |
Correct |
5 ms |
41308 KB |
Output is correct |
23 |
Correct |
5 ms |
41308 KB |
Output is correct |
24 |
Correct |
5 ms |
41308 KB |
Output is correct |
25 |
Correct |
5 ms |
41456 KB |
Output is correct |
26 |
Correct |
5 ms |
41308 KB |
Output is correct |
27 |
Correct |
5 ms |
41308 KB |
Output is correct |
28 |
Correct |
5 ms |
41308 KB |
Output is correct |
29 |
Correct |
5 ms |
41304 KB |
Output is correct |
30 |
Correct |
5 ms |
41308 KB |
Output is correct |
31 |
Correct |
5 ms |
41312 KB |
Output is correct |
32 |
Correct |
5 ms |
41452 KB |
Output is correct |
33 |
Correct |
5 ms |
41312 KB |
Output is correct |
34 |
Correct |
5 ms |
41312 KB |
Output is correct |
35 |
Correct |
5 ms |
41564 KB |
Output is correct |
36 |
Correct |
16 ms |
43180 KB |
Output is correct |
37 |
Correct |
17 ms |
43240 KB |
Output is correct |
38 |
Correct |
17 ms |
43212 KB |
Output is correct |
39 |
Correct |
17 ms |
43024 KB |
Output is correct |
40 |
Correct |
15 ms |
43112 KB |
Output is correct |
41 |
Correct |
16 ms |
43240 KB |
Output is correct |
42 |
Correct |
18 ms |
43240 KB |
Output is correct |
43 |
Correct |
10 ms |
42332 KB |
Output is correct |
44 |
Correct |
13 ms |
42728 KB |
Output is correct |
45 |
Correct |
13 ms |
43120 KB |
Output is correct |
46 |
Correct |
15 ms |
43236 KB |
Output is correct |
47 |
Correct |
14 ms |
43012 KB |
Output is correct |
48 |
Correct |
15 ms |
43240 KB |
Output is correct |
49 |
Correct |
11 ms |
42544 KB |
Output is correct |
50 |
Correct |
13 ms |
43240 KB |
Output is correct |
51 |
Correct |
13 ms |
43184 KB |
Output is correct |
52 |
Correct |
13 ms |
42984 KB |
Output is correct |
53 |
Correct |
13 ms |
43240 KB |
Output is correct |
54 |
Correct |
15 ms |
43224 KB |
Output is correct |
55 |
Correct |
14 ms |
43240 KB |
Output is correct |
56 |
Correct |
12 ms |
43236 KB |
Output is correct |