답안 #1060415

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1060415 2024-08-15T14:00:55 Z khanhtb JJOOII 2 (JOI20_ho_t2) C++14
100 / 100
20 ms 3416 KB
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 2e5 + 5;
 
int N, K;
char a[MAXN];
int cnt[3][MAXN];
 
int get(int l, int r, int c) {
    return cnt[c][r] - cnt[c][l - 1];
}
 
int find_left(int p) {
    int low = 1, high = p;
    int ans = -1;
    while(low <= high) {
        int mid = (low + high) / 2;
        if(get(mid, p, 0) == K) {
            ans = mid;
            low = mid + 1;
        } else if(get(mid, p, 0) > K)
            low = mid + 1;
        else if(get(mid, p, 0) < K)
            high = mid - 1;
    }
    return ans;
}
 
int find_right(int p) {
    int low = p, high = N;
    int ans = -1;
    while(low <= high) {
        int mid = (low + high) / 2;
        if(get(p, mid, 2) == K) {
            ans = mid;
            high = mid - 1;
        } else if(get(p, mid, 2) > K)
            high = mid - 1;
        else if(get(p, mid, 2) < K)
            low = mid + 1;
    }
    return ans;
}
 
signed main() {
#define TASK "code"
 
    if (fopen(TASK ".inp", "r")) {
        freopen(TASK ".inp", "r", stdin);
        freopen(TASK ".out", "w", stdout);
    }
 
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> N >> K;
    for(int i = 1; i <= N; i++) cin >> a[i];
    for(int i = 1; i <= N; i++) {
        for(int j = 0; j < 3; j++)
            cnt[j][i] = cnt[j][i - 1];
        if(a[i] == 'J')
            cnt[0][i]++;
        else if(a[i] == 'O')
            cnt[1][i]++;
        else if(a[i] == 'I')
            cnt[2][i]++;
    }
    int ans = N + 1;
    for(int i = 1; i <= N; i++) {
        if(a[i] != 'O') continue;
        bool ok = true;
        int res = 0;
        int low = i, high = N;
        int p = -1;
        while(low <= high) {
            int mid = (low + high) / 2;
            if(get(i, mid, 1) == K) {
                p = mid;
                high = mid - 1;
            } else if(get(i, mid, 1) > K)
                high = mid - 1;
            else if(get(i, mid, 1) < K)
                low = mid + 1;
        }
 
        if(p == -1) break;
        res += (p - i + 1) - K;
 
        {
            int x = find_left(i - 1);
            if(x == -1) ok = false;
            res += ((i - 1) - x + 1) - K;
        }
 
        {
            int x = find_right(p + 1);
            if(x == -1) ok = false;
            res += (x - (p + 1) + 1) - K;
        }
        if(ok) ans = min(ans, res);
    }
    cout << (ans == N + 1 ? -1 : ans);
}

Compilation message

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |         freopen(TASK ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |         freopen(TASK ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 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 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 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 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 464 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 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 460 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 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 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 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 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 464 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 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 460 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 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 11 ms 2904 KB Output is correct
37 Correct 12 ms 3164 KB Output is correct
38 Correct 15 ms 3196 KB Output is correct
39 Correct 20 ms 3164 KB Output is correct
40 Correct 12 ms 3416 KB Output is correct
41 Correct 17 ms 3200 KB Output is correct
42 Correct 15 ms 3164 KB Output is correct
43 Correct 10 ms 2140 KB Output is correct
44 Correct 9 ms 2396 KB Output is correct
45 Correct 16 ms 3164 KB Output is correct
46 Correct 12 ms 3164 KB Output is correct
47 Correct 11 ms 3164 KB Output is correct
48 Correct 11 ms 3080 KB Output is correct
49 Correct 8 ms 2140 KB Output is correct
50 Correct 12 ms 3164 KB Output is correct
51 Correct 11 ms 3204 KB Output is correct
52 Correct 5 ms 2908 KB Output is correct
53 Correct 7 ms 3164 KB Output is correct
54 Correct 13 ms 3196 KB Output is correct
55 Correct 6 ms 3164 KB Output is correct
56 Correct 4 ms 3164 KB Output is correct