제출 #1273158

#제출 시각아이디문제언어결과실행 시간메모리
1273158icebearJJOOII 2 (JOI20_ho_t2)C++20
100 / 100
19 ms3224 KiB
// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

template<class T>
    bool minimize(T &a, const T &b) {
        if (a > b) return a = b, true;
        return false;
    }

template<class T>
    bool maximize(T &a, const T &b) {
        if (a < b) return a = b, true;
        return false;
    }

#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebear"

const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 2e5 + 5;
int n, k, f[N][3];
string S;

void init(void) {
    cin >> n >> k >> S;
    S = " " + S;
    FOR(i, 1, n) {
        f[i][0] = f[i - 1][0] + (S[i] == 'J');
        f[i][1] = f[i - 1][1] + (S[i] == 'O');
        f[i][2] = f[i - 1][2] + (S[i] == 'I');
    }
}

void process(void) {
    int l = 1, result = inf;
    FOR(r, 1, n) {
        while(l <= n && f[r][1] - f[l - 1][1] > k) l++;
        while(f[r][1] - f[l - 1][1] == k && S[l] != 'O') l++;
        if (f[r][1] - f[l - 1][1] == k && f[l - 1][0] >= k && f[n][2] - f[r][2] >= k) {
            int low = 1, high = l - 1, res = 0, ans = 0;
            while(low <= high) {
                int mid = (low + high) >> 1;
                if (f[l - 1][0] - f[mid - 1][0] >= k) res = mid, low = mid + 1;
                else high = mid - 1;
            }

            low = r + 1, high = n;
            while(low <= high) {
                int mid = (low + high) >> 1;
                if (f[mid][2] - f[r][2] >= k) ans = mid, high = mid - 1;
                else low = mid + 1;
            }
            minimize(result, ans - res + 1 - 3 * k);
        }
    }
    cout << (result == inf ? -1 : result);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int tc = 1;
//    cin >> tc;
    while(tc--) {
        init();
        process();
    }
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...