Submission #583935

# Submission time Handle Problem Language Result Execution time Memory
583935 2022-06-26T14:02:18 Z Kanaifu JJOOII 2 (JOI20_ho_t2) C++17
100 / 100
18 ms 2864 KB
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define fr first
#define sc second

int n, k;
int pref1[200001], pref2[200001], pref3[200001];

int bin_search1(int val)
{
    int l = 1, r = n;
    while(l<=r)
    {
        int mid = ((l+r)>>1);
        if (pref1[mid] < val)
        {
            l = mid+1;
        }
        else
        {
            r = mid - 1;
            if (pref1[mid] < val)
            {
                return mid;
            }
        }
    }
    if (l>n)
    {
        return -1;
    }
    return l;
}

int bin_search2(int val)
{
    int l = 1, r = n;
    while(l<=r)
    {
        int mid = ((l+r)>>1);
        if (pref2[mid] < val)
        {
            l = mid+1;
        }
        else
        {
            r = mid - 1;
            if (pref2[mid] < val)
            {
                return mid;
            }
        }
    }
    if (l>n)
    {
        return -1;
    }
    return l;
}

int bin_search3(int val)
{
    int l = 1, r = n;
    while(l<=r)
    {
        int mid = ((l+r)>>1);
        if (pref3[mid] < val)
        {
            l = mid+1;
        }
        else
        {
            r = mid - 1;
            if (pref3[mid] < val)
            {
                return mid;
            }
        }
    }
    if (l>n)
    {
        return -1;
    }
    return l;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

    cin>>n>>k;

    pref1[0] = 0;
    pref2[0] = 0;
    pref3[0] = 0;

    for (int i=1; i<=n; i++)
    {
        char c;
        cin>>c;
        pref1[i] = pref1[i-1];
        pref2[i] = pref2[i-1];
        pref3[i] = pref3[i-1];
        if (c == 'J')
        {
            pref1[i]++;
        }
        else if (c == 'O')
        {
            pref2[i]++;
        }
        else
        {
            pref3[i]++;
        }
    }

    int mn = 1e9;

    for (int i=1; i<=n; i++)
    {
        int st = pref1[i];

        int poz2;
        if (pref1[i-1] == pref1[i])
        {
            continue;
        }
        poz2 = bin_search1(st+k-1);
        if (poz2 == -1)
        {
            continue;
        }

        st = pref2[poz2];
        int poz3 = bin_search2(st+k);

        if (poz3 == -1)
        {
            continue;
        }

        st = pref3[poz3];

        int kraj = bin_search3(st+k);

        if (kraj == -1)
        {
            continue;
        }
        mn = min(mn, kraj - i + 1 - 3 * k);
    }
    if (mn == 1e9)
    {
        cout<<-1;
        return 0;
    }
    cout<<mn;

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 0 ms 324 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 328 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 0 ms 324 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 328 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 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 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 324 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 340 KB Output is correct
29 Correct 1 ms 340 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 328 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 0 ms 324 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 328 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 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 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 324 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 340 KB Output is correct
29 Correct 1 ms 340 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 328 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 17 ms 2668 KB Output is correct
37 Correct 18 ms 2772 KB Output is correct
38 Correct 18 ms 2772 KB Output is correct
39 Correct 16 ms 2772 KB Output is correct
40 Correct 12 ms 2800 KB Output is correct
41 Correct 15 ms 2820 KB Output is correct
42 Correct 17 ms 2772 KB Output is correct
43 Correct 8 ms 1820 KB Output is correct
44 Correct 11 ms 2260 KB Output is correct
45 Correct 12 ms 2772 KB Output is correct
46 Correct 14 ms 2772 KB Output is correct
47 Correct 13 ms 2772 KB Output is correct
48 Correct 14 ms 2864 KB Output is correct
49 Correct 8 ms 1952 KB Output is correct
50 Correct 11 ms 2768 KB Output is correct
51 Correct 11 ms 2772 KB Output is correct
52 Correct 7 ms 2772 KB Output is correct
53 Correct 9 ms 2784 KB Output is correct
54 Correct 9 ms 2772 KB Output is correct
55 Correct 7 ms 2772 KB Output is correct
56 Correct 6 ms 2768 KB Output is correct