답안 #1073286

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1073286 2024-08-24T11:53:03 Z FIFI_cpp JJOOII 2 (JOI20_ho_t2) C++17
100 / 100
14 ms 4076 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <fstream>
#include <iterator>
#include <set>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cctype>
#include <string>
#include <cassert>
#include <set>
#include <bitset>
#include <unordered_set>
using ll = int64_t;
#define pb push_back
#define all(a) a.begin(),a.end()
#define ppi pair<pair<int,int>,int>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//#define int int64_t
// xcode cant include bits/stdc++.h
using namespace std;
//ifstream fin ("diff.in");
//ofstream fout ("diff.out");
/*   /\_/\
*   (= ._.)
*   / >  \>
*/
// encouraging cat
const int INF = 1000000007;
const int mod = 1000000007;
int32_t main() {
    int n,k;
    cin >> n >> k;
    string s;
    cin >> s;
    vector<int> costJ(n,INF);
    vector<int> costI(n,INF);
    int cost = 0;
    int first = -1;
    int cnt = 0;
    for (int i = 0;i < n;i++)
    {
        if (first != -1)
            cost++;
        if (s[i] == 'J')
        {
            cnt++;
            if (first == -1)
            {
                first = i;
                cost++;
            }
            cost--;
        }
        if (cnt > k)
        {
            cnt--;
            first++;
            while (s[first] != 'J')
            {
                cost--;
                first++;
            }
        }
        if (i == 0)
        {
            if (cnt == k)
                costJ[0] = cost;
        }
        else if (cnt == k)
        {
            costJ[i] = min(costJ[i - 1] + 1,cost);
        }
        else
        {
            costJ[i] = costJ[i - 1];
        }
    }
    cost = 0;
    first = -1;
    cnt = 0;
    for (int i = n - 1;i >= 0;i--)
    {
        if (first != -1)
            cost++;
        if (s[i] == 'I')
        {
            cnt++;
            if (first == -1)
            {
                first = i;
                cost++;
            }
            cost--;
        }
        if (cnt > k)
        {
            cnt--;
            first--;
            while (s[first] != 'I')
            {
                cost--;
                first--;
            }
        }
        if (i == n - 1)
        {
            if (cnt == k)
                costI[n - 1] = cost;
        }
        else if (cnt == k)
        {
            costI[i] = min(costI[i + 1] + 1,cost);
        }
        else
        {
            costI[i] = costI[i + 1];
        }
        cnt = cnt;
    }
    cost = 0;
    first = -1;
    cnt = 0;
    vector<pair<int,int>> costO(n,{INF,INF});
    for (int i = 0;i < n;i++)
    {
        if (first != -1)
            cost++;
        if (s[i] == 'O')
        {
            cnt++;
            if (first == -1)
            {
                first = i;
                cost++;
            }
            cost--;
        }
        if (cnt > k)
        {
            cnt--;
            first++;
            while (s[first] != 'O')
            {
                cost--;
                first++;
            }
        }
        if (i == 0)
        {
            if (cnt == n)
                costO[0] = {cost,0};
        }
        else if (cnt == k)
        {
            costO[i] = {min(costO[i - 1].first + 1,cost),first};
        }
        else
        {
            costO[i] = costO[i - 1];
        }
    }
    int res = INF;
    for (int i = 1;i < n - 1;i++)
    {
        if (costO[i].first != INF && costO[i].second - 1 >= 0 && costJ[costO[i].second - 1] != INF && costI[i + 1] != INF)
            res = min(res,costO[i].first + costJ[costO[i].second - 1] + costI[i + 1]);
    }
    if (res == INF)
        cout << -1 << '\n';
    else
        cout << res << '\n';
    return 0;
}

Compilation message

ho_t2.cpp:32:9: warning: "/*" within comment [-Wcomment]
   32 | /*   /\_/\
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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 416 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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 416 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 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 436 KB Output is correct
19 Correct 1 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 0 ms 348 KB Output is correct
25 Correct 0 ms 348 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 492 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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 416 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 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 436 KB Output is correct
19 Correct 1 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 0 ms 348 KB Output is correct
25 Correct 0 ms 348 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 492 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 9 ms 3800 KB Output is correct
37 Correct 9 ms 4076 KB Output is correct
38 Correct 9 ms 4076 KB Output is correct
39 Correct 10 ms 3820 KB Output is correct
40 Correct 10 ms 4076 KB Output is correct
41 Correct 9 ms 4076 KB Output is correct
42 Correct 11 ms 4076 KB Output is correct
43 Correct 8 ms 2640 KB Output is correct
44 Correct 7 ms 3052 KB Output is correct
45 Correct 14 ms 4076 KB Output is correct
46 Correct 13 ms 4072 KB Output is correct
47 Correct 8 ms 4076 KB Output is correct
48 Correct 8 ms 4076 KB Output is correct
49 Correct 6 ms 2796 KB Output is correct
50 Correct 8 ms 4076 KB Output is correct
51 Correct 9 ms 4076 KB Output is correct
52 Correct 7 ms 3816 KB Output is correct
53 Correct 8 ms 4076 KB Output is correct
54 Correct 5 ms 4076 KB Output is correct
55 Correct 6 ms 4076 KB Output is correct
56 Correct 6 ms 3904 KB Output is correct