CONFLIP - Editorial

I have more or less used the same logic but my submission gives wrong answer.
http://www.codechef.com/viewsolution/5963715

Could someone tell me the corner cases?

@arpan…you’re not checking if i==q…in case n is odd…no of coins=n/2+1 when i is not equal to q…but n/2 if i==q

#include<stdio.h>
char replace(char);
int main()
{
int n,i,I,k,q,h=0,t=0,T,G;
char arr[1000];
scanf("%d",&T);
while(T–)
{
while(G–)
{
scanf("%d",&G);
scanf("%d%d%d",&I,&n,&q);
for(i=0;i<n;i++)
{
if(I==1)
{
arr[i]= ‘h’;
}
else
{
arr[i]= ‘t’;
}
}
for(i=0;i<n;i++)
{
if(I==1)
{
arr[i]=‘t’; }
else
{
arr[i]=‘h’;}
for(k=i-1;k>=0;k–)
{
arr[k]= replace(arr[k]);
}
}
for(i=0;i<n;i++)
{
if(arr[i]==‘h’)
{
h++;
}
else
{
t++;
}
}
if(q==1)
{
printf("%d",h);
}
else{
printf("%d",t);
}
}
}

return 0;

}
char replace(char ch)
{
if(ch==‘h’)
{
ch=‘t’;
}
else
{
ch=‘h’;
}
return(ch);
}

its showing runtime error. please someone tell me why??

#include
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
void coin1()
{
long int t,j,k,i,n,g,c=0,d=0,m,q;
cin>>t;
while(t>=1)
{
cin>>g;
char a[g];
while(g>=1)
{

			cin>>i>>n>>q;
			for(j=1;j<=n;j++)
			{
				if(i==1)
				{
					a[j]='H';
				}
				else if(i==2)
				{
					a[j]='T';
			}
			}
				for(k=1;k<=n;k++)
		{
			for(j=1;j<=n;j++)
			{
				if(j<=k)
				{
					if(a[j]=='H')
					{
						a[j]='T';
					}
					else if(a[j]=='T')
					{
						a[j]='H';
					}
				}
				
			}
		}
		j=1;
		while(j<=n)
	{
		if(a[j]=='H')
		{
			c++;
			//cout<<c<<endl;
		}
		else if(a[j]=='T')
		{
			d++;
		}
		j++;
	}
	if(q==1)
	{
		cout<<c<<endl;
	}
	else if(q==2)
	{
		cout<<d<<endl;
	}
		c=0;
	d=0;
	g--;
	}
c=0;
d=0;

	t--;

}
}

int main()

{
coin1();
}

i have also runtime error problem please help

I executed your code @arus_47

Firstly, I removed that “include<bits stdc++.h=”">" as it led to fatal error.

After that, I put in custom input of Sample case and your code passed. I think that line was the problem.

I suggest removing “include<bits stdc++.h=”">" from your code

(EDIT-The correct code is #include < bits/stdc++.h > Also, the ‘#’ were missing in first three lines. Please see to it dear!)

:slight_smile:

(Upvote if you like my answer, so that I can also ask questions :slight_smile: )

2 Likes

#include
using namespace std;
int main()
{
int t,n,i,g,q;
cin>>t>>n;
while(t–)
{
while(n–)
{
int h=0;
cin>>i>>g>>q;
h=g/2;
if(g%2==0)
{
if(q==1||q==2)
cout<<h<<endl;
}
else
{
if(i==q)
cout<<h<<endl;
else
cout<<h+1<<endl;
}

  }

}
return 0;
}

can anyone tell me what’s wrong in this…?

https://www.codechef.com/viewsolution/14978375

I am using the same logic but getting time limit exceeded error.
Here is my code:
Click Here

My Code Exceeds Time Limit
Can anyone help me with this code

@jatin_b it seems you have removed conio.h but have forgotten to remove clrscr(); from your code…
I wonder how can your code posted can compile sucessfully in linux then…
Resubmit it now…
Hope it doesn’t show RTE then…

You are declaring too much memory for a . The limit for n is 1 ≤ N ≤ 10^9 .you cant have so much memory . In turbo C++ also if you also try to run for such big value I am sure it wont run . try to do this without having so much memory .

Can anyone tell me what is the problem with this code?

@vijju123 @xpertcoder

// Problem Code: CONFLIP on codechef
#include<iostream>
#include<math.h>

using namespace std;

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

    for(int t=1;t<=T;t++){
        int G;
        cin>>G;
        for(int g=1;g<=G;g++){
            int I,N,Q;
            cin>>I>>N>>Q;
            
            int coinsInterested = Q==1? (I==1?N:0) : (I==2?N:0) ;
            
            int changes = ceil(float(N)/2);
            
            if(coinsInterested==0){
                coinsInterested+=changes;
            }
            else{
                coinsInterested-=changes;
            }

            cout<<coinsInterested<<endl;
        }
    }

    return 0;
}

Can you please identify the issue with this code? It’s a short solution won’t take much of your time. I have been stuck at this for more than an hour now. What’s wrong?

The sample cases run okay. But in submission, I get wrong answer.
Also, I am failing the following case (someone mentioned in the comments on a post) -
Input -
2
2
1 5 1
1 5 2
1
1 5 1

Expected Output -
2
3
2

But my program terminates after the second line.

#include <iostream>
using namespace std;

int main() {
    int tcases; cin >> tcases;
    
    // declare variables
    int g,i,q=0; long int n=0;

    for(int i = 0; i < tcases; i++){

        cin>>g;
        for (int j = 0; j < g; j++)
        {
            cin>>i>>n>>q;
            if(n%2==0) cout<<n/2;
            else (q==i) ? (cout<<(n-1)/2):(cout<<(n+1)/2);
            printf("\n");
        }
        

    }
    return 0;
}

You should take G as input before the while loop of G

you are using i for the test cases loop and also for initial state of the coins,it is causing conflict.Use k or some other variable in the tcases loop instead of i,it will run perfectly.

I am using the same logic but still getting the wrong answers. Can anyone helps me?

cook your dish here

if name == “main”:
try:
t = int(input())
for i in range(t):
g = int(input())
for j in range(g):
arr = list(map(int,input().split()))
if arr[1]%2 == 0:
print(arr[1]//2)
break
else:
if arr[0] == arr[2]:
print(arr[1]//2)
elif arr[0] != arr[2]:
print((arr[1]//2) + 1)
except EOFError:
pass

Here is my code in python.

A simple code
#include
using namespace std;

int main() {
int t,g,i,n,q;
cin>>t;
while(t–){
cin>>g;
while(g–){
cin>>i>>n>>q;
if(n%2==0){
cout<<n/2<<endl;
}else{
if(i==q){
cout<<n/2<<endl;
}else{
cout<<(n/2)+1<<endl;
}
}
}
}
return 0;
}

this my code and it works perfectly fine in custom inputs without any problem , but during the submission it shows time limit exceeded.

MY CODE:-

T = int(input())

for i in range(1,T+1):

G = int(input())

for u in range(1,G+1):

    I,N,Q = input().split() 

    I = int(I)

    N = int(N)

    Q = int(Q)

    k = 1

    mylist = list()

    for z in range(0,N):

        mylist.append(I)

    for i in range(0,N):

        for j in range(0,k):

            if (mylist[j] == 1):

                mylist[j] = 2

            else:

                mylist[j] = 1

        k = k + 1

    

    if (Q == 1):

        count = mylist.count(Q)

        print(count)

    else:

        count = mylist.count(Q)

        print(count)

i have written according to what i understood…the code runs fine and gives correct output but at time of submission it shows Time limit exceeded ! How can i optimize it!

#include <iostream>
#include<algorithm>
using namespace std;

int getGuesses(int init, int n, int q) {
    int arr[n];
    int count=0;
    fill_n(arr + 1, n, init);
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=i; j++) {
            arr[j] = (arr[j]%2==0) ? 1 : 2;
        }
    }
    for(int i=1; i<=n; i++) {
        if(arr[i]==q) {
            ++count;
        }
    }
    return count;
    
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int test, g, ini, n, q, count=0;
    cin >> test;
    while(test--) {
        cin >> g;
        while(g--) {
            cin >> ini >> n >> q;
            count = getGuesses(ini, n, q);
            cout << count << "\n";
        }
    }
	return 0;
}