Codeforces 1324C Frog Jumps

题目链接

最大最小, 很容易想到二分答案, 并且可以贪心验证, 复杂度为(O(NlogN)), 无视L, 每次选择最远的R作为新的起点进行贪心验证即可

#include<bits/stdc++.h>
using namespace std;
#define ms(x,y) memset(x, y, sizeof(x))
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<int,int> pii;

const int maxn = 2e5+5;

char str[maxn];
int n;

bool check(int d) {
    int l = 0;
    while(l < n) {
        for(int i = d; i >= 0; --i) {
            if(i == 0) return false;
            if(l+i >= n) return true;
            if(str[i+l] == 'R') {
                l += i;
                break;
            }
        }
    }
    return true;
}

void run_case() {
    cin >> (str+1);
    int l = 1, r = strlen(str+1) + 1, ans, mid;
    n = r;
    while(l <= r) {
        mid = (l+r) /2;
        if(check(mid)) {
            ans = mid;
            r = mid - 1;
        } else 
            l = mid + 1;
    }
    cout << ans << "
";
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cout.flags(ios::fixed);cout.precision(2);
    int t; cin >> t;
    while(t--)
    run_case();
    cout.flush();
    return 0;
}
原文地址:https://www.cnblogs.com/GRedComeT/p/12487554.html