답안 #963776

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
963776 2024-04-15T16:16:26 Z Akshat369 JJOOII 2 (JOI20_ho_t2) C++17
0 / 100
0 ms 344 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define endl '\n'
const int N = (int)1e5+5;


void Solve(){
    int n , k; cin>>n>>k;
    string s; cin>>s;
    deque<char> st;
    for(auto &i : s){
        st.push_back(i);
    }
    while (!st.empty() and st.front()!='J'){
        st.pop_front();
    }
    while(!st.empty() and st.back() != 'I'){
        st.pop_back();
    }
    if(st.empty()){
        cout<<-1<<endl;
        return;
    }

    s = " " + s;
 //   cout<<s<<endl;
    int ans = 1e18;
    for(int i = 1 ; i <= n ; i++){
        if (s[i]=='J'){
            int temp = 0;
            int cntj = 1 , cnto = 0 , cnti = 0;
            int starto = -1 , starti = -1; //first index from where o starts

            for(int j = i +1 ; j<= n ; j++){
                if (cntj<k and s[j]!='J'){
                    temp++;
                }
                if (s[j]=='J') cntj++;
                if (cntj==k){
                    starto = j;
                    break;
                }
            }
            //cout<<"o"<<" "<<i<<" "<<starto << " " << temp <<endl;
            if (starto==-1) continue;
            for(int j = starto+1 ; j<= n ; j++){
                if (cnto < k and s[j]!= 'O'){
                    temp++;
                }
                if (s[j]=='O') cnto++;
                if (cnto==k){
                    starti = j;
                    break;
                }
            }
            //cout<<"i"<<" "<<i<< " "<<starti<<" "<<temp<<endl;
            if(starti==-1) continue;
            for(int j = starti+1; j <= n ; j++){
                if (cnti < k and s[j]!='I'){
                    temp++;
                }
                if (s[j]=='I') cnti++;
                if (cnti==k){
                    break;
                }
            }
            if (cnti!=k) continue;
           // cout<<"last"<<" "<<i<< " "<<starti<<" "<<temp<<endl;
            ans = min(ans,temp);
        }
    }
    if (ans>=1e18){
        cout<<-1<<endl;
    }
    else{
        cout<<ans<<endl;
    }
}

int32_t main(){

    Solve();
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -