답안 #1111837

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1111837 2024-11-13T03:33:57 Z ljtunas JJOOII 2 (JOI20_ho_t2) C++17
100 / 100
11 ms 4880 KB
// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v)    memset(h, v, sizeof h)
#define Forv(i, a)     for(auto& i : a)
#define For(i, a, b)   for(int i = a; i <= b; ++i)
#define Ford(i, a, b)  for(int i = a; i >= b; --i)
#define c_bit(i)     __builtin_popcountll(i)
#define Bit(mask, i)    ((mask >> i) & 1LL)
#define onbit(mask, i)  ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
namespace std {
    template <typename T, int D>
    struct _vector : public vector <_vector <T, D - 1>> {
        static_assert(D >= 1, "Dimension must be positive!");
        template <typename... Args>
        _vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
    };
    template <typename T> struct _vector <T, 1> : public vector <T> {
        _vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
    };
    template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
    template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const  ll  mod = 1e9 + 7;
const  ll   oo = 1e18;

//#define int long long

int n, k; bool del[MAXN];
string s;
vector<int> J;
vector<int> O;
vector<int> I;

int pre[MAXN], suf[MAXN];

void Solve() {
    cin >> n >> k >> s;
    s = '$' + s;
    For(i, 1, n){
        if(s[i] == 'J') J.push_back(i);
        if(s[i] == 'O') O.push_back(i);
        if(s[i] == 'I') I.push_back(i);
    }
    For(i, 1, J.size() - 1){
        pre[i] = pre[i - 1] + max(0, J[i] - 1 - (J[i - 1] + 1) + 1);
    }
    Ford(i, I.size() - 2, 0){
        suf[i] = suf[i + 1] + max(0, I[i + 1] - 1 - (I[i] + 1) + 1);
    }
    int ans = inf, sum = 0;
    if(O.size() < k) {
        cout << -1 << '\n'; return;
    }
    For(i, 1, k - 1) sum += O[i] - 1 - (O[i - 1] + 1) + 1;
//    cout << sum << '\n';
    for (int i = k - 1; i <= O.size() - 1; ++i){
        if(i != k - 1){
            sum += O[i] - 1 - (O[i - 1] + 1) + 1;
            sum -= O[i - k + 1] - 1 - (O[i - k] + 1) + 1;
        }
        int L = O[i - k + 1], R = O[i];
        int r = lower_bound(all(I), R) - I.begin();
        if(r + k - 1 > I.size() - 1) continue;
        int lo = -1, hi = J.size();
        while(hi - lo > 1){
            int mid = lo + hi >> 1;
            if(J[mid] <= L) lo = mid;
            else hi = mid;
        }
        int l = lo;
        if(l - k + 1 < 0) continue;
        int cur = sum;
        cur += I[r] - 1 - (O[i] + 1) + 1;
        cur += O[i - k + 1] - 1 - (J[l] + 1) + 1;
        cur += pre[l] - pre[l - k + 1];
        cur += suf[r] - suf[r + k - 1];
        minimize(ans, cur);
    }
    if(ans == inf) ans = -1;
    cout << ans << '\n';
}

int32_t main() {
    cin.tie(0) -> sync_with_stdio(0);
    if(fopen("JOI20_ho_t2.inp", "r")) {
        freopen("JOI20_ho_t2.inp", "r", stdin);
        freopen("JOI20_ho_t2.out", "w", stdout);
    }

    int t = 1;
//    cin >> t;

    for (int test = 1; test <= t; test++) {
        Solve();
    }
    return 0;
}

Compilation message

ho_t2.cpp: In function 'void Solve()':
ho_t2.cpp:10:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 | #define For(i, a, b)   for(int i = a; i <= b; ++i)
......
   53 |     For(i, 1, J.size() - 1){
      |         ~~~~~~~~~~~~~~~~~~               
ho_t2.cpp:53:5: note: in expansion of macro 'For'
   53 |     For(i, 1, J.size() - 1){
      |     ^~~
ho_t2.cpp:60:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   60 |     if(O.size() < k) {
      |        ~~~~~~~~~^~~
ho_t2.cpp:65:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |     for (int i = k - 1; i <= O.size() - 1; ++i){
      |                         ~~^~~~~~~~~~~~~~~
ho_t2.cpp:72:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |         if(r + k - 1 > I.size() - 1) continue;
      |            ~~~~~~~~~~^~~~~~~~~~~~~~
ho_t2.cpp:75:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   75 |             int mid = lo + hi >> 1;
      |                       ~~~^~~~
ho_t2.cpp: In function 'int32_t main()':
ho_t2.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |         freopen("JOI20_ho_t2.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         freopen("JOI20_ho_t2.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 2 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2520 KB Output is correct
10 Correct 1 ms 2552 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
13 Correct 1 ms 2384 KB Output is correct
14 Correct 1 ms 2552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 2 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2520 KB Output is correct
10 Correct 1 ms 2552 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
13 Correct 1 ms 2384 KB Output is correct
14 Correct 1 ms 2552 KB Output is correct
15 Correct 1 ms 2384 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 1 ms 2384 KB Output is correct
18 Correct 1 ms 2384 KB Output is correct
19 Correct 1 ms 2384 KB Output is correct
20 Correct 2 ms 2520 KB Output is correct
21 Correct 1 ms 2552 KB Output is correct
22 Correct 1 ms 2384 KB Output is correct
23 Correct 2 ms 2384 KB Output is correct
24 Correct 2 ms 2384 KB Output is correct
25 Correct 1 ms 2384 KB Output is correct
26 Correct 1 ms 2384 KB Output is correct
27 Correct 1 ms 2384 KB Output is correct
28 Correct 1 ms 2384 KB Output is correct
29 Correct 1 ms 2384 KB Output is correct
30 Correct 1 ms 2384 KB Output is correct
31 Correct 1 ms 2384 KB Output is correct
32 Correct 1 ms 2384 KB Output is correct
33 Correct 1 ms 2384 KB Output is correct
34 Correct 1 ms 2384 KB Output is correct
35 Correct 1 ms 2384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 2 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2520 KB Output is correct
10 Correct 1 ms 2552 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
13 Correct 1 ms 2384 KB Output is correct
14 Correct 1 ms 2552 KB Output is correct
15 Correct 1 ms 2384 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 1 ms 2384 KB Output is correct
18 Correct 1 ms 2384 KB Output is correct
19 Correct 1 ms 2384 KB Output is correct
20 Correct 2 ms 2520 KB Output is correct
21 Correct 1 ms 2552 KB Output is correct
22 Correct 1 ms 2384 KB Output is correct
23 Correct 2 ms 2384 KB Output is correct
24 Correct 2 ms 2384 KB Output is correct
25 Correct 1 ms 2384 KB Output is correct
26 Correct 1 ms 2384 KB Output is correct
27 Correct 1 ms 2384 KB Output is correct
28 Correct 1 ms 2384 KB Output is correct
29 Correct 1 ms 2384 KB Output is correct
30 Correct 1 ms 2384 KB Output is correct
31 Correct 1 ms 2384 KB Output is correct
32 Correct 1 ms 2384 KB Output is correct
33 Correct 1 ms 2384 KB Output is correct
34 Correct 1 ms 2384 KB Output is correct
35 Correct 1 ms 2384 KB Output is correct
36 Correct 9 ms 4388 KB Output is correct
37 Correct 10 ms 4180 KB Output is correct
38 Correct 11 ms 4228 KB Output is correct
39 Correct 9 ms 4368 KB Output is correct
40 Correct 8 ms 4624 KB Output is correct
41 Correct 9 ms 4756 KB Output is correct
42 Correct 10 ms 4180 KB Output is correct
43 Correct 5 ms 3808 KB Output is correct
44 Correct 6 ms 3908 KB Output is correct
45 Correct 8 ms 4880 KB Output is correct
46 Correct 8 ms 4624 KB Output is correct
47 Correct 8 ms 4624 KB Output is correct
48 Correct 7 ms 4240 KB Output is correct
49 Correct 5 ms 3916 KB Output is correct
50 Correct 8 ms 4624 KB Output is correct
51 Correct 8 ms 4676 KB Output is correct
52 Correct 5 ms 4384 KB Output is correct
53 Correct 6 ms 4228 KB Output is correct
54 Correct 11 ms 4380 KB Output is correct
55 Correct 6 ms 4316 KB Output is correct
56 Correct 4 ms 4380 KB Output is correct