Submission #1062569

# Submission time Handle Problem Language Result Execution time Memory
1062569 2024-08-17T08:41:38 Z Whisper JJOOII 2 (JOI20_ho_t2) C++17
100 / 100
8 ms 7204 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
#define MAX             200005
int N, K;
string s;
int L[MAX], R[MAX], M[MAX];

void process(void){
    cin >> N >> K >> s;
    s = '@' + s;
    deque<int> l;
    int dis = 0;
    for (int i = 1; i <= N; ++i){
        if(s[i] != 'J'){
            L[i] = INF; continue;
        }

        if(l.size()) dis += i - l.back() - 1;
        l.push_back(i);

        if(l.size() > K){
            assert(l.size() >= 2);
            dis -= l[1] - l[0] - 1;
            l.pop_front();
        }
        L[i] = (l.size() == K ? dis : INF);
    }

    deque<int> m;
    dis = 0;
    for (int i = 1; i <= N; ++i){
        if (s[i] != 'O'){
            M[i] = INF; continue;
        }
        if (m.size()) dis += i - m.back() - 1;
        m.push_back(i);
        if(m.size() > K){
            assert(m.size() >= 2);
            dis -= m[1] - m[0] - 1;
            m.pop_front();
        }
        M[i] = (m.size() == K ? dis : INF);;
    }

    deque<int> r;
    dis = 0;
    for (int i = N; i >= 1; --i){
        if(s[i] != 'I'){
            R[i] = INF; continue;
        }
        if(r.size()) dis += r.back() - i - 1;
        r.push_back(i);
        if(r.size() > K){
            assert(r.size() >= 2);
            dis -= r[0] - r[1] - 1;
            r.pop_front();
        }
        R[i] = (r.size() == K ? dis : INF);
    }
    R[N + 1] = L[0] = INF;
    for (int i = 1; i <= N; ++i) L[i] = min(L[i] - i, L[i - 1]);
    for (int i = N; i >= 1; --i) R[i] = min(R[i] + i, R[i + 1]);

    int ans = INF;
    for (int i = 1; i <= N; ++i){
        if(M[i] >= INF) continue;

        int pre_pos = i - M[i] - K;
        if(pre_pos > 0){
            minimize(ans, L[pre_pos] + M[i] + R[i + 1] - i + pre_pos - 1);
        }
    }
    cout << (ans > N - 3 * K ? -1 : ans);
}
signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    process();
    return (0 ^ 0);
}




Compilation message

ho_t2.cpp: In function 'void process()':
ho_t2.cpp:56:21: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   56 |         if(l.size() > K){
      |            ~~~~~~~~~^~~
ho_t2.cpp:61:26: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   61 |         L[i] = (l.size() == K ? dis : INF);
      |                 ~~~~~~~~~^~~~
ho_t2.cpp:72:21: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   72 |         if(m.size() > K){
      |            ~~~~~~~~~^~~
ho_t2.cpp:77:26: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   77 |         M[i] = (m.size() == K ? dis : INF);;
      |                 ~~~~~~~~~^~~~
ho_t2.cpp:88:21: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   88 |         if(r.size() > K){
      |            ~~~~~~~~~^~~
ho_t2.cpp:93:26: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   93 |         R[i] = (r.size() == K ? dis : INF);
      |                 ~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 468 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 464 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 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 468 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 464 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 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 1 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 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 352 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 468 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 464 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 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 1 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 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 352 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 7 ms 5168 KB Output is correct
37 Correct 8 ms 5668 KB Output is correct
38 Correct 6 ms 5668 KB Output is correct
39 Correct 6 ms 5416 KB Output is correct
40 Correct 8 ms 6008 KB Output is correct
41 Correct 7 ms 5668 KB Output is correct
42 Correct 6 ms 5480 KB Output is correct
43 Correct 4 ms 3932 KB Output is correct
44 Correct 5 ms 4688 KB Output is correct
45 Correct 6 ms 5924 KB Output is correct
46 Correct 6 ms 5924 KB Output is correct
47 Correct 6 ms 5988 KB Output is correct
48 Correct 6 ms 5924 KB Output is correct
49 Correct 4 ms 4176 KB Output is correct
50 Correct 6 ms 5924 KB Output is correct
51 Correct 6 ms 5924 KB Output is correct
52 Correct 6 ms 6700 KB Output is correct
53 Correct 6 ms 6948 KB Output is correct
54 Correct 4 ms 5664 KB Output is correct
55 Correct 4 ms 6692 KB Output is correct
56 Correct 4 ms 7204 KB Output is correct