1615C - Menorah - CodeForces Solution


brute force graphs greedy math *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define FILEIO 0

#if FILEIO == 1
    #define IN input
    #define OUT output
#else
    #define IN cin
    #define OUT cout
#endif

#define FOR(var, start, end) for(int var = start; var < end; var++)
#define ull int64_t


constexpr long double pi = 3.141592653589793238462643383279;
     

int T, N;
string A, B;

int64_t solve()
{
    int a, b, c, d;
    a = b = c = d = 0;
    for(int i=0; i < N; i++)
    {
        if(A[i] == '1' && B[i] == '1')
            a++;
        if(A[i] == '1' && B[i] == '0')
            b++;
        if(A[i] == '0' && B[i] == '1')
            c++;
        if(A[i] == '0' && B[i] == '0')
            d++;
    }

    int AB = INT_MAX;
    int BA = INT_MAX;

    if(b == c && b == 0)
        return 0;

    if(b == c)
        BA = 2*b;

    swap(a,c);
    swap(b,d);
    a++;
    c--;

    if(b == c)
        AB = 2 * b + 1;

    /*
    if(a == 1 + d)
    {
        int count = 0;
        int par = 0;
        while(true)
        {
            if(b == 0 && c == 0)
                break;
            if(!par)
            {
                swap(a,c);
                swap(b,d);
                a++;
                c--;
            } else
            {
                swap(a,c);
                swap(b,d);
                b++;
                d--;
            }
            par = !par;
            count++;
        }
        AB = count;
    }
    */


    if(AB == BA && AB == INT_MAX)
        return -1;

    return min(AB, BA);
}

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

#if FILEIO == 1
    ifstream input("input.txt");
    ofstream output("output.txt");
#endif

    //////////////////////////////////////////////////////
    //////////////////////////////////////////////////////
    
    IN >> T;
    FOR(t,0,T)
    {
        IN >> N;
        IN >> A >> B;
        auto res = solve();
        OUT << res << "\n";
    }
    OUT << endl;
}


Comments

Submit
0 Comments
More Questions

165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom