651D - Image Preview - CodeForces Solution


binary search brute force dp two pointers *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pl;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define all(x) x.begin(),x.end()  
#define allr(x) x.rbegin(),x.rend() 
#define print(a) for(auto e:a) cout<<e<<" ";cout<<endl; 
#define cr(_) {cout<<_<<endl;return;}
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define s second  
#define f first
#define pb push_back
#define sz(a) a.size()
const ll N=1e5+5;
const ll MOD=998244353;
using namespace std;

bool isPrime(int num) {
    if (num < 2) {
        return false;
    }
    for (int i = 2; i <= sqrt(num); ++i) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}
int dx[] = {0, 0, 1, -1};
    int dy[] = {1, -1, 0, 0};
void solve(){
ll n,a,b,c;
cin>>n>>a>>b>>c;
string s;
cin>>s;
vl pre(n+1);
rep(i,1,n+1){
    pre[i]=(s[i-1]=='w'?b+1:1);

}
rep(i,1,n+1) pre[i]+=pre[i-1];

ll l=0;
ll r=n;
ll res=0;
while(l<=r){
    ll mid=(l+r)/2;
    ll ok=0;
    rep(i,1,mid+1){
        ll sum=pre[i]+pre[n]-pre[n-mid+i];
        ll sum1=min(2*(i-1)*a+(mid-i)*a,2*(mid-i)*a+(i-1)*a);
        if(sum+sum1<=c){
            ok=1;
        }
    }
    if(ok){
        res=mid;
        l=mid+1;
    }
    else r=mid-1;
    
}
cout<<res<<endl;

}



int main() {
    int t=1;
 //  cin >> t;

    while (t--) {
      solve();
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST