답안 #1086824

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1086824 2024-09-11T14:33:32 Z Icelast JJOOII 2 (JOI20_ho_t2) C++17
100 / 100
13 ms 6464 KB
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 1e8+9;
void solve(){
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    s = ' ' + s;
    vector<int> J(n+2, 0), O(n+2, n+1), I(n+2, n+1);
    for(int i = n; i >= 1; i--){
        if(s[i] == 'O'){
            O[i] = i;
        }else{
            O[i] = O[i+1];
        }
    }
    for(int i = 1; i <= n; i++){
        if(s[i] == 'O'){
            O[i] = O[i+1];
        }
    }
    int j = -1, cnt = 0;
    for(int i = 1; i <= n; i++){
        if(s[i] == 'O'){
            cnt++;
            if(cnt == k){
                j = i;
                break;
            }
        }
    }
    if(j == -1){
        cout << -1;
        return;
    }
    vector<int> pfO(n+2, INF), OO(n+2, n+1);
    for(int i = 1; i <= n; i++){
        if(s[i] == 'O'){
            pfO[i] = j-i+1 - k;
            OO[i] = j;
            j = O[j];
        }

        if(j == n+1) break;
    }

    for(int i = n; i >= 1; i--){
        if(s[i] == 'I'){
            I[i] = i;
        }else{
            I[i] = I[i+1];
        }
    }
    for(int i = 1; i <= n; i++){
        if(s[i] == 'I'){
            I[i] = I[i+1];
        }
    }
    cnt = 0;
    j = -1;
    for(int i = 1; i <= n; i++){
        if(s[i] == 'I'){
            cnt++;
            if(cnt == k){
                j = i;
                break;
            }
        }
    }
    if(j == -1){
        cout << -1;
        return;
    }
    vector<int> pfI(n+2, INF);
    for(int i = 1; i <= n; i++){
        if(s[i] == 'I'){
            pfI[i] = j-i+1 - k;
            j = I[j];
        }
        if(j == n+1) break;
    }

    for(int i = 1; i <= n; i++){
        if(s[i] == 'J'){
            J[i] = i;
        }else{
            J[i] = J[i-1];
        }
    }
    for(int i = n; i >= 1; i--){
        if(s[i] == 'J'){
            J[i] = J[i-1];
        }
    }
    cnt = 0;
    j = -1;
    for(int i = n; i >= 1; i--){
        if(s[i] == 'J'){
            cnt++;
            if(cnt == k){
                j = i;
                break;
            }
        }
    }
    if(j == -1){
        cout << -1;
        return;
    }
    vector<int> pfJ(n+2, INF);
    for(int i = n; i >= 1; i--){
        if(s[i] == 'J'){
            pfJ[i] = i-j+1 - k;
            j = J[j];
        }
        if(j == 0) break;
    }
    int ans = INF;
    for(int i = 1; i <= n; i++){
        if(s[i] == 'O'){
            if(pfO[i] == INF) continue;
            ans = min(ans, pfJ[J[i]]+(i-J[i]-1) + pfO[i] + pfI[I[OO[i]]]+(I[OO[i]]-OO[i]-1));
            //cout << i << " " << pfJ[J[i]]+(i-J[i]-1) << " " << pfO[i] << " " << pfI[I[OO[i]]]+(I[OO[i]]-i-1)<< "s\n" ;
        }
    }
    if(ans == INF) ans = -1;
    cout << ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 448 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 448 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 456 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 460 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 448 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 456 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 460 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 11 ms 5932 KB Output is correct
37 Correct 13 ms 6436 KB Output is correct
38 Correct 10 ms 6436 KB Output is correct
39 Correct 12 ms 6408 KB Output is correct
40 Correct 10 ms 6460 KB Output is correct
41 Correct 10 ms 6436 KB Output is correct
42 Correct 10 ms 6244 KB Output is correct
43 Correct 6 ms 4188 KB Output is correct
44 Correct 9 ms 4960 KB Output is correct
45 Correct 10 ms 6456 KB Output is correct
46 Correct 10 ms 6436 KB Output is correct
47 Correct 10 ms 6464 KB Output is correct
48 Correct 11 ms 6300 KB Output is correct
49 Correct 7 ms 4384 KB Output is correct
50 Correct 10 ms 6444 KB Output is correct
51 Correct 11 ms 6432 KB Output is correct
52 Correct 10 ms 6188 KB Output is correct
53 Correct 11 ms 6432 KB Output is correct
54 Correct 5 ms 6432 KB Output is correct
55 Correct 5 ms 6436 KB Output is correct
56 Correct 4 ms 6436 KB Output is correct