답안 #244860

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
244860 2020-07-05T07:43:55 Z Nightlight JJOOII 2 (JOI20_ho_t2) C++14
100 / 100
36 ms 2944 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;

int N, K;
int sumJ[200005];
int sumO[200005];
int sumI[200005];
int cntJ, cntO, cntI;

void calc(int l, int r) {
  cntJ = sumJ[r] - sumJ[l - 1];
  cntO = sumO[r] - sumO[l - 1];
  cntI = sumI[r] - sumI[l - 1];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin >> N >> K;
  char ch;
  for(int i = 1; i <= N; i++) {
    cin >> ch;
    if(ch == 'J') {
      sumJ[i] = 1;
    }else if(ch == 'O') {
      sumO[i] = 1;
    }else {
      sumI[i] = 1;
    }
    sumJ[i] += sumJ[i - 1];
    sumO[i] += sumO[i - 1];
    sumI[i] += sumI[i - 1];
  }
  int ans = 1e9;
  for(int i = 1; i <= N; i++) {
    int l = i, r = N, res = -1;
//    cout << i << "\n";
    //binser J
    while(l <= r) {
      int mid = (l + r) >> 1;
      calc(i, mid);
      if(cntJ >= K) {
        res = mid;
        r = mid - 1;
      }else {
        l = mid + 1;
      }
    }
//    cout << res << " J\n";
    if(res == -1) break;
    //binser O
    int bef = res + 1;
    l = res + 1, r = N, res = -1;
    while(l <= r) {
      int mid = (l + r) >> 1;
      calc(bef, mid);
      if(cntO >= K) {
        res = mid;
        r = mid - 1;
      }else {
        l = mid + 1;
      }
    }
//    cout << res << " O\n";
    if(res == -1) break;
    //binser I
    bef = res + 1;
    l = res + 1, r = N, res = -1;
    while(l <= r) {
      int mid = (l + r) >> 1;
      calc(bef, mid);
      if(cntI >= K) {
        res = mid;
        r = mid - 1;
      }else {
        l = mid + 1;
      }
    }
//    cout << res << " I\n";
    if(res == -1) break;
    ans = min(ans, res - i + 1 - K * 3);
  }
  if(ans != 1e9) cout << ans << "\n";
  else cout << "-1\n";
  cin >> N;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 4 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 5 ms 384 KB Output is correct
35 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 4 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 5 ms 384 KB Output is correct
35 Correct 5 ms 384 KB Output is correct
36 Correct 28 ms 2768 KB Output is correct
37 Correct 31 ms 2816 KB Output is correct
38 Correct 36 ms 2816 KB Output is correct
39 Correct 31 ms 2816 KB Output is correct
40 Correct 16 ms 2816 KB Output is correct
41 Correct 26 ms 2944 KB Output is correct
42 Correct 34 ms 2816 KB Output is correct
43 Correct 9 ms 1920 KB Output is correct
44 Correct 10 ms 2304 KB Output is correct
45 Correct 12 ms 2816 KB Output is correct
46 Correct 12 ms 2816 KB Output is correct
47 Correct 11 ms 2944 KB Output is correct
48 Correct 12 ms 2944 KB Output is correct
49 Correct 9 ms 2048 KB Output is correct
50 Correct 11 ms 2816 KB Output is correct
51 Correct 12 ms 2856 KB Output is correct
52 Correct 12 ms 2816 KB Output is correct
53 Correct 12 ms 2816 KB Output is correct
54 Correct 17 ms 2816 KB Output is correct
55 Correct 12 ms 2944 KB Output is correct
56 Correct 11 ms 2816 KB Output is correct