The problem can be broken down into 8 cases depending on whether a, b, c are even or odd.
It is easy to see that if exactly one of a or c is odd, then the sum of all elements in the multiset will be odd. So, in these cases (4 of them), the answer would be NO.
Also, if all of a, b and c are even, the answer would be YES because we will always be able to take a/2, b/2 and c/2 elements in one subset forming equal sums.
The three cases that remain are :
[s := a + (2*b) + (3*c)]
a is odd, b is odd, c is odd.
Let a = 2k + 1,\ \ b = 2l + 1,\ \ c = 2m + 1 s = 2k + 1 + 4l + 2 + 6m + 3 s = 2k + 4l + 6m + 6 \therefore\ \ s/2 = k + 2l + 3m + 3 \therefore\ \ s/2 = \lfloor{a/2}\rfloor + (2*\lfloor{b/2}\rfloor) + (3*\lfloor{c/2}\rfloor )+ 3
We only need to make sure that the additional 3 at the end can be formed. a' := a - \lfloor{a/2}\rfloor, \ \ b' := b - \lfloor{b/2}\rfloor, \ \ c' := c - \lfloor{c/2}\rfloor \ \ 3 can be formed iff a' \geq 3 \ \ || \ \ (a' \geq 1 \ \ \&\& \ \ b' \geq 1) \ \ || \ \ c' \geq 1.
So, the answer would be YES if this is true. Else, the answer would be NO.
a is odd, b is even, c is odd.
Let a = 2k + 1,\ \ b = 2l,\ \ c = 2m + 1 s = 2k + 1 + 4l + 6m + 3 s = 2k + 4l + 6m + 4 \therefore\ \ s/2 = k + 2l + 3m + 2 \therefore\ \ s/2 = \lfloor{a/2}\rfloor + (2*\lfloor{b/2}\rfloor) + (3*\lfloor{c/2}\rfloor )+ 2
We only need to make sure that the additional 2 at the end can be formed. a' := a - \lfloor{a/2}\rfloor, \ \ b' := b - \lfloor{b/2}\rfloor, \ \ c' := c - \lfloor{c/2}\rfloor \ \ 2 can be formed iff a' \geq 2 \ \ || \ \ b' \geq 1.
So, the answer would be YES if this is true. Else, the answer would be NO.
a is even, b is odd, c is even.
Let a = 2k,\ \ b = 2l + 1,\ \ c = 2m s = 2k + 4l + 2 + 6m s = 2k + 4l + 6m + 2 \therefore\ \ s/2 = k + 2l + 3m + 1 \therefore\ \ s/2 = \lfloor{a/2}\rfloor + (2*\lfloor{b/2}\rfloor) + (3*\lfloor{c/2}\rfloor )+ 1
We only need to make sure that the additional 1 at the end can be formed. a' := a - \lfloor{a/2}\rfloor, \ \ b' := b - \lfloor{b/2}\rfloor, \ \ c' := c - \lfloor{c/2}\rfloor \ \ 1 can be formed iff a' \geq 1
So, the answer would be YES if this is true. Else, the answer would be NO.
Here’s the link to my code.
However, this received WA. Can someone help me figure out the problem here?
You should first of all check, if the sum is even or not, if you don’t do that, you are bound to mess up few cases, as it was seen by so many Was during the contest
Can anyone please suggest me the mistake in my program. As, the testcases I am able to think gives the correct output, but instead it is showing wrong answer.
I will be very much thankful to the one who helps me…
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
// your code goes here
//freopen(“input.txt”,“r”,stdin);
int t;
cin>>t;
while(t–){
int a,b,c;
cin>>a>>b>>c;
long long sum1 = 0;
long long sum2 = 0;
if(c%2 == 1){
sum1 += 3;
c--;
}
int x = c / 2;
sum1 += 3*x;
sum2 += 3*x;
if(b%2 != 0){
if(sum1<sum2){
sum1 += 2;
}else
sum2 += 2;
b--;
}
x = b / 2;
sum1 += 2*x;
sum2 += 2*x;
long long diff = sum1 - sum2;
if(a >= diff){
a = a - diff;
if(a%2 == 0){
cout<<"Yes"<<endl;
}else
cout<<"No"<<endl;
}else{
cout<<"No"<<endl;
}
}
return 0;
Can someone please tell what is wrong in my code?? Thanks
#include<bits/stdc++.h>
using namespace std;
int main()
{
int tc;
cin>>tc;
int a,b,c,q1,q2,q3;
while(tc--)
{
cin>>a>>b>>c;
long int sum = a + b*2 + c*3;
if(sum%2==1)
cout<<"NO\n";
else
{
sum=sum/2;
q3=sum/3;
sum = sum - min(q3,c)*3;
q2=sum/2;
sum = sum - min(q2,b)*2;
q1=sum/1;
sum = sum - min(q1,a);
if(sum>0)
cout<<"NO\n";
else
cout<<"YES\n";
}
}
}