Tutorial for Codeforces 650B/651D Image Preview

Solution #

It’s obvious that the images we opened is a sub-segment of all images. We can loop over all the possible left endpoints and use two pointers to find the rightmost endpoint.

Code #

#include <bits/stdc++.h>
 
using namespace std;
using ll=long long;
template<typename... T> void rd(T&... args) {((cin>>args), ...);}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,a,b,T;
    string s;
    rd( n,a,b,T,s);
    int ans=0;
    vector<ll> t(2*n);
    forn(i,n){
        t[i]=t[i+n]=(s[i]=='w'?b+1:1);
    }
    for(int i=1;i<2*n;i++) t[i]+=t[i-1];
    int r=n;
    auto f=[&](int l,int r){
        ll res=t[r]-t[l-1];
        ll di=r-l+min(r-n,n-l);
        return res+di*a;
    };
    for(int l=1;l<=n;l++){
        while(r+1<l+n&&f(l,r+1)<=T) r++;
        if(f(l,r)<=T) ans=max(ans,r-l+1);
    }
    cout<<ans;
    return 0;
}