CATSDOGS - Editorial

What I’m doing is this:

  1. Calculate maximum value of legs possible which is max = 4*(c+d) when all cats and dogs are on the floor.
  2. Calculate minimum value of legs possible which is min = 4*(c-d) when max cats are on top of dogs.
  3. Number of legs should be positive multiples of 4.
  4. Number of legs should be between max and min.

To my surprise, it doesn’t work. It gives a straight WA.

Where did I go wrong?

long long int t;
scanf("%lld", &t);

while(t--)
{
    long long int c, d, l;
    scanf("%lld %lld %lld", &c, &d, &l);

    long long int max = (c+d)*4;
    long long int min = (c-d)*4;

    if(l >= min && l <= max && l%4 == 0 && l >= 4)
        printf("yes\n");
    else
        printf("no\n");
}
return 0;

}

Any help?

PS: It works fine with the given test case!

@mr_nair

Your calculation of min is only partially correct.

Its correct if number of cats are way more than number of dogs, but what if number of cats are less than number of dogs?

In a scenario when all cats can be on top of dogs, the min is not 4x(c-d), but its simply 4xd. Visualise it, when all cats on top of dogs, minimum legs is equal to number legs of dogs.

Your case fails at this test case-

Input
1
1 3 4
Output
yes
Expected output
no

Your min here is 4x(1-3) = -8. While practically we can see that min should be 4*3=12.


Can someone tell why i am failing my testcases

O(1) Solution:

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

int main()
{
int t;
cin>>t;
while(t--){
	long long c,d,l,maxlegs,minlegs,cog,flag=0;
	cin>>c>>d>>l;
	if(l%4==0){
		maxlegs=(c+d)*4;
		if(c>2*d){
			cog=c-2*d;
		}
		else cog=0;
		minlegs=(d+cog)*4;
		
		if(l>=minlegs && l<=maxlegs) flag=1;	
	}
	else flag=0;
	
	if(flag) cout<<"yes"<<endl;
	else cout<<"no"<<endl;
}
return 0;
}

Have a look at my solution https://www.codechef.com/viewsolution/17555935
It is self explanatory .

1 Like

What’s wrong in this code? https://www.codechef.com/viewsolution/18693946
Only 30%?

What’s wrong in this code? https://www.codechef.com/viewsolution/18693946

Only 30%?

what is wrong in my code .I followed editorial

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

Please help

Describe what you think the following input means:

1
5 5 0

and why you should not allow this as a valid situation.