답안 #996705

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
996705 2024-06-11T06:09:21 Z nikaa123 JJOOII 2 (JOI20_ho_t2) C++14
100 / 100
13 ms 5668 KB
// #pragma GCC diagnostic warnig "-std=c++11"
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("O3")
// #pragma GCC optimization ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define pp pop_back
#define endl '\n'
#define ff first
#define ss second
#define stop exit(0)
#define sz(x) (int)x.size()
#define pause system("pause")
#define all(x) (x).begin(),(x).end()
#define deb(x) cout << #x << "-" << x  << endl
 
typedef char chr;
typedef string str;
// typedef long long ll;
typedef vector <int> vii;
typedef pair <int,int> pii;
 
const long long INF = LLONG_MAX;
const int inf = INT_MAX;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int dx[] = {0,0,-1,1};
const int dy[] = {-1,1,0,0};
const double PI = 2 * acos(0.0);

const int N = 2e5 + 5;

int n,k;
int ll,rr;  
string s;
int l,r,pos1,pos2,cnt,ans;
int pref1[N],pref2[N],pref3[N];

inline void test_case () {  
    
    cin >> n >> k;
    cin >> s;
    s = " " + s;
    for (int i = 1; i <= n; i++) {
        pref1[i] = pref1[i-1] + (s[i] == 'J');
        pref2[i] = pref2[i-1] + (s[i] == 'O');
        pref3[i] = pref3[i-1] + (s[i] == 'I');
    }
    ans = inf;
    for (int l = 1; l <= n; l++) {
        if (s[l] != 'J') continue;
        

        pos1 = inf;
        ll = 1; rr = l;
        while (ll <= rr) {
            int mid = (ll + rr)/2;
            if (pref1[l]-pref1[mid-1] >= k) {
                pos1 = mid;
                ll = mid + 1;
            } else {
                rr = mid - 1;
            }
        }
        if (pos1 == inf) continue;
        

        r = inf;
        ll = l; rr = n;
        while (ll <= rr) {
            int mid = (ll + rr)/2;
            if (pref2[mid]-pref2[l-1] >= k) {
                r = mid;
                rr = mid - 1;
            } else {
                ll = mid + 1;
            }
        }
        if (r == inf) continue;

        
        pos2 = inf;
        ll = r; rr = n;
        while (ll <= rr) {
            int mid = (ll + rr)/2;
            if (pref3[mid]-pref3[r-1] >= k) {
                pos2 = mid;
                rr = mid - 1;
            } else {
                ll = mid + 1;
            }
        }
        if (pos2 == inf) continue;

        // cout << pos1 << " " << l << " " << r << " " << pos2 << " " << pos2 - pos1 + 1 - 3*k << endl;
        ans = min(ans,pos2-pos1+1-3*k);

    }

    if (ans == inf) {
        cout << -1 << endl;
        return;
    }
    cout << ans << endl;
    
}
 
signed main () {
 
    ios_base :: sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    
    int T = 1;
    // cin >> T;
    while(T--) {
        test_case();
    }
    
    return 0;
 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 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 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 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 0 ms 348 KB Output is correct
12 Correct 0 ms 348 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 0 ms 344 KB Output is correct
2 Correct 0 ms 344 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 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 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 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 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 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 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 0 ms 344 KB Output is correct
2 Correct 0 ms 344 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 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 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 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 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 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 7 ms 4912 KB Output is correct
37 Correct 8 ms 5412 KB Output is correct
38 Correct 8 ms 5412 KB Output is correct
39 Correct 11 ms 5412 KB Output is correct
40 Correct 9 ms 5668 KB Output is correct
41 Correct 10 ms 5660 KB Output is correct
42 Correct 13 ms 5668 KB Output is correct
43 Correct 6 ms 3676 KB Output is correct
44 Correct 6 ms 4456 KB Output is correct
45 Correct 7 ms 5664 KB Output is correct
46 Correct 7 ms 5440 KB Output is correct
47 Correct 7 ms 5668 KB Output is correct
48 Correct 8 ms 5668 KB Output is correct
49 Correct 5 ms 3684 KB Output is correct
50 Correct 7 ms 5668 KB Output is correct
51 Correct 8 ms 5444 KB Output is correct
52 Correct 5 ms 5416 KB Output is correct
53 Correct 7 ms 5668 KB Output is correct
54 Correct 7 ms 5668 KB Output is correct
55 Correct 6 ms 5668 KB Output is correct
56 Correct 4 ms 5668 KB Output is correct