답안 #723245

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
723245 2023-04-13T12:30:43 Z ducanh1234 JJOOII 2 (JOI20_ho_t2) C++14
100 / 100
55 ms 5532 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long 

#define rep(i, b) for(int i = 0; i < (int)b; i++)
#define FOR(i, a, b) for(int i = a; i < (int)b; i++)
#define dbg(v) cerr << "Line(" << __LINE__ << ") -> " << #v << " = " << (v) << endl;
#define debug(a, b) cerr << "[" << #a << ", " << #b << "] = [" << a << ", " << b << "]\n";

template<class T> inline void read(T& t){ cin >> t; }
template<class T, class... H> inline void read(T& t, H&... h){ cin >> t; read(h...); }
template<class T> inline void read(vector<T>& t){ for(auto&x : t) read(x); } 
template<class T, class... H> inline void read(vector<T>& t, vector<H>&... h){ read(t); read(h...); }

template<class T> inline void wt(const T t) { cout << t; } 
template<class T> inline void write(const T t) { cout << t << " "; } 
template<class T> inline void print(const T t){ cout << t << "\n";}
template<class T, class... H> inline void wt(const T& t, const H&... h){ cout << t; wt(h...); }
template<class T, class... H> inline void print(const T& t, const H&... h){ cout << t; cout << " "; print(h...);}
#define all(x) x.begin(), x.end()
#define pb push_back
#define sz(a) (int)a.size()

bool bs(vector<int>&a, int x){
    return binary_search(all(a), x);
}
int a, b, c;

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n, k; string s; read(n, k, s);
    vector<int> pre_j(n + 1, 0), pre_o(n + 1, 0), pre_i(n + 1, 0);
    // build prefix sum
    rep(i, n){
      pre_j[i + 1] = pre_j[i] + (s[i] == 'J');
      pre_o[i + 1] = pre_o[i] + (s[i] == 'O');
      pre_i[i + 1] = pre_i[i] + (s[i] == 'I');
    }    
    int Min = 1e9, pos_j = 0, l = 0, cnt = 0, tmp = 0;
    while(true){
      if(bs(pre_j, pre_j[cnt] + k)){
        pos_j = lower_bound(all(pre_j), pre_j[cnt] + 1) - pre_j.begin();
        a = lower_bound(all(pre_j), pre_j[cnt++] + k) - pre_j.begin(); 
      }else break;
      
      if(bs(pre_o, pre_o[a] + k)){
        b = lower_bound(all(pre_o), pre_o[a] + k) - pre_o.begin();
      }else break;

      if(bs(pre_i, pre_i[b] + k)){
        c = lower_bound(all(pre_i), pre_i[b] + k) - pre_i.begin();
      }else break;
 //     print(a, pos_j, b, c);
      Min = min(Min, c - pos_j + 1 - 3 * k); 
    }
    print(Min == (int)1e9 ? -1 : Min);
    return 0;
}

/*
JOI 2020 JJOOII
- For each starting point...
    - Find the next point such that the number of "J"s between it and the starting
      point is equal to K
    - Find the next point such that the number of "O"s between it and the previous
      point is equal to K
    - Find the next point such that the number of "I"s between it and the previous
      point is equal to K
    - Notice how this is always optimal because we want to minimize the distance
      between the first and last points
- We can use 4 pointers and prefix sums to find the 4 points for each starting point
- Complexity: O(N)
*/

Compilation message

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:39:31: warning: unused variable 'l' [-Wunused-variable]
   39 |     int Min = 1e9, pos_j = 0, l = 0, cnt = 0, tmp = 0;
      |                               ^
ho_t2.cpp:39:47: warning: unused variable 'tmp' [-Wunused-variable]
   39 |     int Min = 1e9, pos_j = 0, l = 0, cnt = 0, tmp = 0;
      |                                               ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 320 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 2 ms 400 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
29 Correct 1 ms 320 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 328 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 320 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 2 ms 400 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
29 Correct 1 ms 320 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 328 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 52 ms 5176 KB Output is correct
37 Correct 55 ms 5524 KB Output is correct
38 Correct 51 ms 5472 KB Output is correct
39 Correct 47 ms 5472 KB Output is correct
40 Correct 14 ms 5472 KB Output is correct
41 Correct 45 ms 5440 KB Output is correct
42 Correct 53 ms 5524 KB Output is correct
43 Correct 3 ms 3540 KB Output is correct
44 Correct 4 ms 4308 KB Output is correct
45 Correct 5 ms 5444 KB Output is correct
46 Correct 7 ms 5460 KB Output is correct
47 Correct 5 ms 5444 KB Output is correct
48 Correct 5 ms 5472 KB Output is correct
49 Correct 3 ms 3808 KB Output is correct
50 Correct 5 ms 5472 KB Output is correct
51 Correct 4 ms 5472 KB Output is correct
52 Correct 4 ms 5344 KB Output is correct
53 Correct 5 ms 5472 KB Output is correct
54 Correct 20 ms 5532 KB Output is correct
55 Correct 10 ms 5472 KB Output is correct
56 Correct 5 ms 5472 KB Output is correct