Skip to main content

Codeforces Round 857 Problem B(Settlement of Guinea Pigs) solution c++

 Codeforces Round 857 (Div. 2)

                                   B. Settlement of Guinea Pigs 

Dasha loves guinea pigs very much. In this regard, she decided to settle as many guinea pigs at home as possible and developed a plan for the next n days. Every day, she will either buy a new guinea pig or call a doctor to examine all her pets.

Unfortunately, the store where she was going to buy guinea pigs does not understand them. Therefore, it cannot determine their gender. Dasha can't do it either. The only one who can help is a doctor.

To keep guinea pigs, aviaries are needed. Dasha plans to buy them in the same store. Unfortunately, only one species is sold there — a double aviary. No more than two guinea pigs can live in it.

Since Dasha does not want to cause moral injury to her pets — she will not settle two guinea pigs of different genders in one aviary.

Help Dasha calculate how many aviaries in the worst case you need to buy so that you can be sure that at no moment of time do two guinea pigs of different genders live in the same aviary.

As part of this task, we believe that guinea pigs have only two genders — male and female.

Solution Code:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main()
{
    int t;
    cin >> t;

    while (t--)
    {
        ll n;
        cin >> n;

        ll arr[n];
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
        }

        ll box = 0;
        ll occu = 0;
        ll unoc = 0;
        ll rabits = 0;

        int i = 0;
        while (i < n)
        {
            ll temp = 0;
            while (arr[i] == 1)
            {
                i++;
                temp++;
                rabits++;
            }
            if (temp - unoc > 0)
            {
                box += (temp - unoc);
            }
            else
            {
                unoc -= temp;
            }
            if (arr[i] == 2)
            {
                if (rabits > 0)
                {
                    occu = ((rabits) / 2) + 1;
                }
                else // when the array and only contain 2s
                {
                    occu = rabits / 2;
                }
                unoc = box - occu;
                i++;
            }
        }

        cout << box << endl;
    }
}

If u have any problem understanding pls comment.

Comments

Popular posts from this blog

Solved: Deleted my tcs codevita account in authenticator accidentally and there is no backup enabled. What to Do now?

 If you have deleted your authenticator or have deleted only the codevita account from the authenticator, then just mail TCS campus commune (Mail id -  campuscommune@tcs.com ) describing your problem. You will get a response within 24-72hrs. Mail again if you don't receive any mail even after three working days. Then after one week or so, you will again receive another mail from TCS saying, " Try to login again now, you'll be prompted to configure the OTP account again. You have to scan the QR Code in Microsoft Authenticator app and there you will get the OTP." After this mail when you will try to log in in codevita, you will get to see the QR code again. P.S. -> Kindly remember NOT to delete the Microsoft authentication app in the future. Comment if you have any query.

Problem B,Maximum Substring,CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!)

B: Maximum Substring  A binary string is a string consisting only of the characters   0   and   1 . You are given a binary string  s. For some non-empty substring † †   t t  of string  s s  containing  x x  characters  0  and  y y  characters  1 , define its  cost  as: x ⋅ y x ⋅ y , if  x > 0 x > 0  and  y > 0 y > 0 ; x 2 x 2 , if  x > 0 x > 0  and  y = 0 y = 0 ; y 2 y 2 , if  x = 0 x = 0  and  y > 0 y > 0 . Given a binary string  s s  of length  n n , find the maximum cost across all its non-empty substrings. † †  A string  a  is a substring of a string  b  if  a a  can be obtained from  b  by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Input Each test consists of multiple test cas...