Submission #474687

# Submission time Handle Problem Language Result Execution time Memory
474687 2021-09-19T11:57:47 Z vincent97198 JJOOII 2 (JOI20_ho_t2) C++14
100 / 100
9 ms 3028 KB
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);   cout.tie(nullptr);
    
    int n,k;    cin >> n >> k;
    string s;   cin >> s;
    vector<int> pos(n),near_O(n),near_I(n);
    int J_r=-1,J_cnt=0,O_r=-1,O_cnt=0,I_r=-1,I_cnt=0;
    for(int i=0;i<n;++i){
        while(J_cnt<k && J_r<n){
            ++J_r;
            if(s[J_r]=='J') ++J_cnt;
        }
        while(O_cnt<k && O_r<n){
            ++O_r;
            if(s[O_r]=='O') ++O_cnt;
        }
        while(I_cnt<k && I_r<n){
            ++I_r;
            if(s[I_r]=='I') ++I_cnt;
        }
        
        if(s[i]=='J'){
            if(J_cnt==k)    pos[i]=J_r;
            else pos[i]=-1;
            --J_cnt;
        }
        else if(s[i]=='O'){
            if(O_cnt==k)    pos[i]=O_r;
            else pos[i]=-1;
            --O_cnt;
        }
        else{
            if(I_cnt==k)    pos[i]=I_r;
            else pos[i]=-1;
            --I_cnt;
        }
    }
    
    int O_is_here=-1,I_is_here=-1;
    for(int i=n-1;i>=0;--i){
        if(s[i]=='O')   O_is_here=i;
        near_O[i]=O_is_here;
    }
    for(int i=n-1;i>=0;--i){
        if(s[i]=='I')   I_is_here=i;
        near_I[i]=I_is_here;
    }
    
    // for(int i=0;i<n;++i)    cout << "pos" << i << ": " << pos[i] << endl;
    
    int ans=(1<<30);
    for(int i=0;i<n;++i){
        int tmp=0;
        if(s[i]=='J'){
            if(pos[i]==-1)  continue;
            tmp+=pos[i]-i+1-k;
            if(near_O[pos[i]]==-1 || pos[near_O[pos[i]]]==-1)  continue;
            tmp+=pos[near_O[pos[i]]]-near_O[pos[i]]+1-k+near_O[pos[i]]-pos[i]-1;
            if(near_I[pos[near_O[pos[i]]]]==-1 || pos[near_I[pos[near_O[pos[i]]]]]==-1) continue;
            tmp+=pos[near_I[pos[near_O[pos[i]]]]]-near_I[pos[near_O[pos[i]]]]+1-k+near_I[pos[near_O[pos[i]]]]-pos[near_O[pos[i]]]-1;
            /*
            cout << i << " " << near_O[pos[i]] << " " << near_I[pos[near_O[pos[i]]]] << endl;
            cout << "ww\n";
            cout << pos[i] << " " << pos[near_O[pos[i]]] << " " << pos[near_I[pos[near_O[pos[i]]]]] << endl;
            */
            ans=min(ans,tmp);
        }
        
    }
    if(ans!=(1<<30))    cout << ans << '\n';
    else    cout << -1 << '\n';
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 0 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 0 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 7 ms 2776 KB Output is correct
37 Correct 8 ms 2996 KB Output is correct
38 Correct 8 ms 2904 KB Output is correct
39 Correct 9 ms 2904 KB Output is correct
40 Correct 8 ms 3028 KB Output is correct
41 Correct 9 ms 2904 KB Output is correct
42 Correct 9 ms 2992 KB Output is correct
43 Correct 5 ms 1996 KB Output is correct
44 Correct 6 ms 2520 KB Output is correct
45 Correct 9 ms 2996 KB Output is correct
46 Correct 8 ms 2996 KB Output is correct
47 Correct 8 ms 2904 KB Output is correct
48 Correct 8 ms 3008 KB Output is correct
49 Correct 6 ms 2044 KB Output is correct
50 Correct 8 ms 2904 KB Output is correct
51 Correct 8 ms 2904 KB Output is correct
52 Correct 6 ms 2776 KB Output is correct
53 Correct 7 ms 2904 KB Output is correct
54 Correct 4 ms 3000 KB Output is correct
55 Correct 4 ms 2904 KB Output is correct
56 Correct 4 ms 2904 KB Output is correct