Submission #519507

#TimeUsernameProblemLanguageResultExecution timeMemory
519507JomnoiJJOOII 2 (JOI20_ho_t2)C++17
100 / 100
1389 ms2012 KiB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

const int N = 2e5 + 10;
const int INF = 1e9 + 7;

char s[N];

int find(int x, vector <int> vec) {
    int l = 0, r = vec.size() - 1, pos = -1;
    while(l <= r) {
        int mid = (l + r) / 2;
        
        if(vec[mid] < x) {
            l = mid + 1;
        }
        else {
            pos = mid;
            r = mid - 1;
        }
    }
    return pos;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    cin >> (s + 1);
    vector <int> J, O, I;
    for(int i = 1; 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);
        }
    }

    int ans = INF;
    for(int i = 0; i + k - 1 < J.size(); i++) {
        int pos_j = i + k - 1;
        int pos_o = find(J[pos_j] + 1, O);
        if(pos_o == -1 or pos_o + k - 1 >= O.size()) {
            continue;
        }
        pos_o = pos_o + k - 1;
        int pos_i = find(O[pos_o] + 1, I);
        if(pos_i == -1 or pos_i + k - 1 >= I.size()) {
            continue;
        }
        pos_i = pos_i + k - 1;
        ans = min(ans, (I[pos_i] - O[pos_i] - k) + (O[pos_i] - J[pos_j] - k) + (J[pos_j] - J[pos_j - k + 1] - k + 1));
    }
    if(ans == INF) {
        ans = -1;
    }
    cout << ans;
    return 0;
}

Compilation message (stderr)

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:45:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for(int i = 0; i + k - 1 < J.size(); i++) {
      |                    ~~~~~~~~~~^~~~~~~~~~
ho_t2.cpp:48:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |         if(pos_o == -1 or pos_o + k - 1 >= O.size()) {
      |                           ~~~~~~~~~~~~~~^~~~~~~~~~~
ho_t2.cpp:53:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         if(pos_i == -1 or pos_i + k - 1 >= I.size()) {
      |                           ~~~~~~~~~~~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...