LOJ # 2764. 「JOI 2013 Final」 JOIOI 塔

Topics address

https://loj.ac/problem/2764

answer

Really can not think half ... do not look at tag words ...
consider the answer to be converted to binary decision problem, then the question becomes, can a combination of x JOI / IOI, consider greedy determination, backwards to do, one of the statistics I number cnt, OI group has the number of tot, and JOI / IOI number ans. For J is clearly looking for a direct answer OI composition. Obviously for O directly to I. I need to be considered greedy, assuming current cnt + tot + ans> = x then the set of answers, or that I do inside OI, let cnt ++. (X greedy consider only need to make one OI, will create more waste)

#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int N = 1000010;

int n;
char s[N];

bool check(int x) {
    int ans = 0, tot = 0, cnt = 0;
    for(int i = n; i; --i) {
        if(x == ans) return 1;
        if(s[i] == 'J') {
            if(tot) --tot, ++ans;
            continue;
        }
        if(s[i] == 'O') {
            if(cnt) --cnt, ++tot;
            continue;
        }
        if(s[i] == 'I') {
            if(ans + tot + cnt < x) cnt++;
            else {
                if(tot) --tot, ++ans;
            }
        }
    }
    if(x == ans) return 1;
    return 0;
}

int main() {
    scanf("%d%s", &n, s + 1);
    int l = 0, r = n, ans = 0;
    while(l <= r) {
        int mid = (l + r) >> 1;
        if(check(mid)) ans = mid, l = mid + 1;
        else r = mid - 1;
    } 
    printf("%d\n", ans);
}

Guess you like

Origin www.cnblogs.com/henry-1202/p/11432873.html