1082B - Vova and Trophies - CodeForces Solution


greedy *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define lol long long
#define all(x) x.begin(),x.end()
#define rev(x) x.rbegin(),x.rend()
#define nline "\n"
#define hell 1000000007
#define ld long double
#define mp make_pair
#define pb push_back
#define coutyes cout << "YES" << endl
#define coutno cout << "NO" << endl
#define endl '\n'
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
const int N = 1e5+10;
vector<int>g[N];
bool vis[N];

void dfs(int vertex){
    if(!vis[vertex]){
        vis[vertex]=true;
        for (int child: g[vertex])
        {
                if(!vis[child]) dfs(child);
        }
    }
}

void solve(){
    
    lol n;cin>>n;
    string s;cin>>s;
    vector<lol>len;
    lol cnt =0;
       vector<lol>pos;
       lol cntb =0;
    for (int i = 0; i < n; ++i)
    {
            if(s[i]=='G'){
                cnt++;
            }
            else{
                cntb++;
                if(cnt!=0){
                    len.pb(cnt);
                    pos.pb(i);
                    cnt=0;
                }
            }
    }
    if(cnt!=0){
        len.pb(cnt);
        pos.pb(n);

    }

    // for(auto c: len) cout<<c<<" ";
    // cout<<endl;
    
    // for(auto c: pos) cout<<c<<" ";
    // cout<<endl;
    if(len.size()>=3){
        lol ans = 0;
        for (int i = 0; i < len.size()-1; ++i)
        {
                if((pos[i+1]-len[i+1]-pos[i])==1){
                    ans=max(ans,len[i+1]+len[i]+1);
                    // cout<<ans<<" ";
                }
        }

        lol r = *max_element(all(len))+1;
        ans=max(ans,r);
        cout<<ans<<endl;
    }
    else if(len.size()==2){
        lol ans = *max_element(all(len))+1;
       if(pos[1]-len[1]-pos[0]==1){
       if(cntb==0) ans=max(ans,len[1]+len[0]-1);
       else ans=max(ans,len[1]+len[0]);
        // cout<<ans<<endl;
       }
       cout<<ans<<endl;
    }
    else if(len.size()==1){
        cout<<len[0]<<endl;
    }
    else{
        cout<<0<<endl;
    }

}


int main(){
    IOS;
    int t=1;
    // cin>>t;
    while(t-->0){

    solve();
    }
    

    return 0;
}


Comments

Submit
0 Comments
More Questions

84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship