909D - Colorful Points - CodeForces Solution


data structures greedy implementation *2100

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

s = list(input().rstrip())
u, v = [], []
c, l = 0, s[0]
for i in s:
    if l ^ i:
        u.append(c)
        v.append(l)
        c, l = 0, i
    c += 1
u.append(c)
v.append(l)
ans = 0
while len(u) > 1:
    ans += 1
    u[0] += 1
    u[-1] += 1
    u0, v0 = [], []
    for i, j in zip(u, v):
        c = i - 2
        if c <= 0:
            continue
        if not v0 or v0[-1] ^ j:
            u0.append(c)
            v0.append(j)
        else:
            u0[-1] += c
    u, v = u0, v0
print(ans)

C++ Code:

#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define For(i,a,b) for(int i=a;i<b;i++)
#define Forn(i,a,b) for(int i=a;i>=0;i--)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define LL long long
#define first(x) get<0>(x)
#define second(x) get<1>(x)
#define third(x) get<2>(x)
using namespace std;

 
void solve(){
    string s;
    cin>>s;
    int n=s.length();
    
    vector<pair<int,int>> vec;
    int cnt=1;
    for(int i=1;i<n;i++){
        if(s[i]==s[i-1]){
            cnt++;
        }
        else{
            vec.pb({s[i-1]-'a',cnt});
            cnt=1;
        }
    }
    vec.pb({s[n-1]-'a',cnt});
    
    vector<pair<int,int>> new_vec;
    int ans=0;
    while(true){
        int sz=vec.size();
        /*
        for(auto p:vec){
            cout<<p.first<<" "<<p.second<<endl;
        }
        cout<<endl;
        */
        if(sz<=1) break;
        ans++;
        for(int i=0;i<sz;i++){
            if(i==0 || i==sz-1){
                vec[i].second--;
            }
            else vec[i].second-=2;
        }
        /*
        cout<<"here2"<<endl;
        for(auto p:vec){
            cout<<p.first<<" "<<p.second<<endl;
        }
        cout<<endl;
        */
        int i=0, j=1, cnt=0;
        while(i<sz){
            if(vec[i].second>0){
                cnt=vec[i].second;
                break;
            }
            i++;
        }
        //cout<<"i="<<i<<endl;
        if(i==sz){
            break;
        }
        j=i+1;
        while(j<sz){
            if(vec[j].second>0){
                if(vec[j].first==vec[i].first){
                    cnt+=vec[j].second;
                }
                else{
                    new_vec.pb({vec[i].first,cnt});
                    i=j;
                    cnt=vec[j].second;
                }
            }
            j++;
        }
        new_vec.pb({vec[i].first,cnt});
        
        vec=new_vec;
        new_vec.clear();
    }
    cout<<ans<<endl;
}
 

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	
	solve();
	
    return 0;
}


Comments

Submit
0 Comments
More Questions

1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts