GIT01 - Editorial

PROBLEM LINK:

Practice
Contest

Author: Gitesh Narula
Tester: Mugurel Ionut Andreica
Editorialist: Kirill Gulin

PROBLEM

You’re given a cake in a form of the N \times M matrix. Each cell of the cake can be either red or green. The cake is considered correct if any two adjacent cells of the matrix are different. You can change any green cell of the given cake to red in 3 units of penalty and red to green in 5 units. Determine the minimum penalty you need to make your cake correct.

QUICK EXPLANTION

Notice there are only two different correct cakes. Try each one and choose the one with the minimum penalty.

EXPLANATION:

Look at the top left cherry. It can be either red or green. If the color of the top left cherry is determined then it is easy to see the first row of the cake is determined uniquely, or more precisely, two adjacent cherries have different colors. Again, if the cherries of the first row are determined, then the second row is also uniquely determined. Moreover, if the cherry in a certain column of the first row is determined, then the cherry in the same column of the second row has a color different from the color of the cherry in the first row. In the same way, the remaining rows of the cake can be uniquely restored.

Therefore, there are only two different correct cakes: the first one with a green top left cherry and the second one with a red top left cherry. Try each one, count the penalty required and choose the best one.

The implementation itself is not hard. Firstly, read the input cake as two-dimensional array of characters. Create another one such array and fill it in the next way. Set its top left cell as green, for example. After that, fill the remaining characters of the first line in such a way that each two adjacent cherries are different. In the similar way, fill the remaining rows of the cake, with the cherry in the first column different from the cherry in the row above. Set variable for penalty and initialize it as zero. Then go over each cell of the created cake and check whether the corresponding cell of the input cake has different color. If so, add 3 or 5 to the penalty depending whether the input cell is green or red respectively. Do the same one more time, setting the top left cell as red.

There is a shorter implementation without creating any extra matrixes, but using the parity of the cell’s row and column sum.

Total time and memory complexity: O(NM).

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.
Tester’s solution can be found here.

Why is there no solution from either author or editorialist? Can anyone post their solution along with required comments. I am feeling a bit ashamed of not able to understand this simple problem.

Can anyone please tell me what am i missing here. I’m creating 2 posssible matrices based on the input and comparing them while iterating row-wise and column-wise.

Here’s my code:

#include<stdio.h>
int main()
{
int T,n,m,i,j;
scanf("%d",&T);
while(T–)
{
int cost1=0,cost2=0;
scanf("%d%d",&n,&m);
char str[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if((i+j)%2==0)
{

if(str[i][j]==‘G’)
{
cost 1++ = 3;
}
else
{
cost 2++ = 5;
}
}
else
{
if(str[i][j] ==‘R’)
{
cost 1+=5;
}
else
{
cost 2+=3;
}
}}}
if(cost1<cost2)
{
printf(’%d\n",cost 1);
}
else
{
printf("%d\n",cost 2);
}
return 0;
}

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

int main()
{
ll t,m,n,i,ans,diff1,diff2,sumR,sumG;
scanf("%lld",&t);
char a;
while(t–)
{
sumR = 0,sumG = 0;
scanf("%lld %lld",&m,&n);
for(i=0;i<m*n;i++)
{
cin >> a;
switch(i%2)
{
case 0:
if(a==‘G’)
sumR += 3;
else if(a==‘R’)
sumG += 5;
break;
case 1:
if(a==‘G’)
sumG += 3;
else if(a==‘R’)
sumR += 5;
break;
}
}
printf("%lld\n",min(sumR,sumG));
}

return 0;

}

Can you please spot the error in my program ?? I think it’s good to go but getting wrong answer.

import numpy as np
for _ in range(int(input())):
row, col = map(int,input().split())
arr = np.empty((0,col), str)
for i in range(row):
arr = np.append(arr, np.array([[i for i in input()]]), axis = 0)

temp1, temp2 = 0,0
for i in range(row):
    for j in range(col):
        if (i+j)%2 == 0:
            if arr[i][j] != 'R':
                temp1 += 3
        else:
            if arr[i][j] != 'G':
                temp1 += 5

for i in range(row):
    for j in range(col):
        if (i+j)%2 == 0:
            if arr[i][j] != 'G':
                temp2 += 5
        else:
            if arr[i][j] != 'R':
                temp2 += 3

print(min(temp1, temp2))

can you help me to find error in this code

Can anyone tell me that my logic is correct or not . It is passing test cases but giving wrong answer

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

int main() {

int t;

cin>>t;

while(t--)

{

    int n,m;

    cin>>n>>m;

    int sum=0;

    while(n--)

    {

        string s;

        cin>>s;

        for(int i=0;i<s.length()-1;i++)

        {

           if(s[i]=='R'){

               if(s[i]==s[i+1])

               sum+=5;

           }

           else if(s[i]=='G'){

               if(s[i]==s[i+1])

               sum+=3;

           }

        }

    }

    cout<<sum<<endl;

}

return 0;

}

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

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);

long long t;
cin >> t;

while(t--) 
{
    long long n,m;
    cin>>n>>m;
    char mat[n][m];
    for(long long i=0;i<n;i++)
    {
        for(long long j=0;j<m;j++)
        {
            cin>>mat[i][j];
        }
    }
    vector<char>a;
    vector<char>b;
    vector<vector<char>>m1;
    vector<vector<char>>m2;
    for(long long i=0;i<m;i++)
    {
        if(i%2)
        {
            a.push_back('R');
            b.push_back('G');
        }
        else
        {
            b.push_back('R');
            a.push_back('G');
        }
    }
    /*for(long long i=0;i<m;i++)
    cout<<a[i];
    cout<<"\n";
    for(long long i=0;i<m;i++)
    cout<<b[i];
    cout<<"\n";*/
    for(long long i=0;i<n;i++)
    {
        if(i%2)
        {
            m1.push_back(a);
            m2.push_back(b);
        }
        else
        {
            m2.push_back(a);
            m1.push_back(b);
        }
    }
    /*for(long long i=0;i<n;i++)
    {
        for(long long j=0;j<m;j++)
        {
            cout<<m1[i][j];
        }
        cout<<"\n";
    }
    cout<<"\n";
    for(long long i=0;i<n;i++)
    {
        for(long long j=0;j<m;j++)
        {
            cout<<m2[i][j];
        }
        cout<<"\n";
    }
    */
    long long cnt1=0,cnt2=0;
    for(long long i=0;i<n;i++)
    {
        for(long long j=0;j<m;j++)
        {
            if(mat[i][j]=='R'&&m1[i][j]=='G')
            cnt1=cnt1+5;
            else if(mat[i][j]=='G'&&m1[i][j]=='R')
            cnt1=cnt1+3;
            else
            cnt1=cnt1+0;
        }
    }
    for(long long i=0;i<n;i++)
    {
        for(long long j=0;j<m;j++)
        {
            if(mat[i][j]=='R'&&m2[i][j]=='G')
            cnt2=cnt2+5;
            else if(mat[i][j]=='G'&&m2[i][j]=='R')
            cnt2=cnt2+3;
            else
            cnt2=cnt2+0;
        }
    }
    long long ans=min(cnt1,cnt2);
    cout<<ans<<endl;
}
return 0;

}

Just check mine you will be clear.

1 Like