constructive algorithms greedy implementation *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>indexed_set;


#pragma GCC optimize("Ofast")  
//#pragma GCC target("avx,avx2,fma") 
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("-O2")

#define f(i,a,b)        for(int i=(int)(a);i<=(int)(b);i++)
#define all(a)          (a).begin(), (a).end()
#define ll                 long long
#define fi                 first
#define se                 second
#define pb                 push_back
#define pob                pop_back
#define mem(ara,x)      memset((ara),(x),sizeof(ara))
#define memv(ara,x)     memset((&ara[0]),(x),sizeof(ara))
#define memn(ara)         memset((ara),-1,sizeof(ara))
#define bb                 begin()
#define ee                 end()
#define em                 emplace
#define eb                 emplace_back
#define D                 double
#define endl            '\n'

#define pii             pair<int,int>
#define pll             pair<long long int,long long int>
#define vi              vector<int>
#define vll             vector<long long int>
#define vii             vector<pii>
 
#define mx                 (1<<30)-1
#define llmx             (1ll<<62)

#define mod             1000000007
#define n_size             200005
#define g_size             20000005
#define eps             1e-9

#define sc1(a)             scanf("%d",&a)
#define sc2(a,b)         scanf("%d %d",&a,&b)
#define pf1(a)             printf("%d\n",a);
#define pf2(a,b)          printf("%d %d\n",a,b)

const double PI=acos(-1);

int Dx[]={-1,-1,0,1,1, 1, 0,-1};
int Dy[]={ 0, 1,1,1,0,-1,-1,-1};
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};

int arr[g_size];
vi v;

inline void solve(){

    

    int n,m,t,res;

    int l,r,mid;

    int maxi=-mx,mini=mx,count=0;

    int i,j,k;

    bool flag;

    int a,b;
    cin>>n>>a>>b;

    string s;
    cin>>s;

    for(i=0;i<n;i++){
        if(s[i]=='.') count++;
        else{
            if(count) v.pb(count);
            count=0;
        }
    }

    if(count) v.pb(count);
    count=0;


    sort(all(v));
    // for(auto x:v) cout<<x<<" ";
    // cout<<endl;
    if(a<b) swap(a,b);
    for(i=0;i<v.size();i++){
        if(a){
            if((v[i]+1)/2 >=a){
                count+=a;
                v[i]=v[i]-a;
                a=0;
            }
            else{
                a=a-((v[i]+1)/2);
                count+=((v[i]+1)/2);
                v[i]=v[i]-(v[i]+1)/2;
            }
        }
        else break;
    }

    // for(auto x:v) cout<<x<<" ";
    // cout<<endl;

    for(i=0;i<v.size();i++){
        if(v[i]==0) continue;
        if(b){
            if(v[i] >=b){
                count+=b;
                v[i]=v[i]-b;
                b=0;
            }
            else{
                b=b-v[i];
                count+=v[i];
                v[i]=0;
            }
        }
        else break;
    }

    cout<<count<<endl;
 

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    //freopen("input.in","r",stdin);
    //freopen("output.out","w",stdout);
    //cout<<fixed<<setprecision(11);


    int T=1;
    //cin>>T;

    while(T--) solve();
    
    
}


Comments

Submit
0 Comments
More Questions

1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations
677C - Vanya and Label
1583B - Omkar and Heavenly Tree
1703C - Cypher
1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon