Submission #245781

#TimeUsernameProblemLanguageResultExecution timeMemory
245781Atill83JJOOII 2 (JOI20_ho_t2)C++14
100 / 100
20 ms2084 KiB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 3e5+5;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n, k;
string s;

vector<int> j, o, I;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);

    #ifdef Local
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
    #endif

    cin>>n>>k;
    int ans = mod;
    cin>>s;
    for(int i = 0; i < n; i++){
        if(s[i] == 'J') j.push_back(i);
        else if(s[i] == 'O') o.push_back(i);
        else I.push_back(i);
    }
    for(int i = 0; i <= (int) o.size() - k; i++){
        int i1 = upper_bound(j.begin(), j.end(), o[i]) - j.begin() - 1;
        int i2 = upper_bound(I.begin(), I.end(), o[i + k - 1]) - I.begin();
        if(i1 < k - 1 || i2 > (int) I.size() - k) continue;
        int sim = (j[i1] - j[i1 - k + 1] + 1 - k);
        sim += o[i] - j[i1] - 1;
        sim += o[i + k - 1] - o[i] + 1 - k;
        sim += I[i2] - o[i + k - 1] - 1;
        sim += I[i2 + k - 1] - I[i2] + 1 - k;
        //cout<<sim<<endl;
        ans = min(ans, sim);
    }
    

    if(ans == mod)
        ans = -1;
    cout<<ans<<endl;
    

    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...