MarblesProblem code: MANIP5 |
Malcom and Angus own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Malcom and Angus start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way sometimes even if the total value of all marbles is even. For example, if there are one marble of value 1, one of value 3 ,three of value 4 and one of value 6 ,then they can be split into sets of equal value (Set 1 = { 1,0,0,1,0,1} Set 2 = { 0,0,1,2,0,0}) So, they ask you to write a program that checks whether it is possible to split the marbles fairly.
Input
The first line consists of t (1<= t<= 50) the number of test cases. Then t test cases follow. Each line in the test case describes one collection of marbles to be divided. The lines consist of six non-negative integers n1 , n2 , n3 , n4 , n5 , n6 where ni is the number of marbles of value i. So, the example from above would be described by the input-line 1 0 1 3 0 1 . The maximum number of marbles of each priority is 20.
Output
For each test case output a single line with the word Yes if the marbles can be divided or No" if they can't be divided.
Sample
Input 5 1 0 1 2 0 0 1 0 0 0 1 1 2 1 2 1 2 1 1 0 1 3 0 1 20 20 20 20 20 Output No Yes No Yes Yes
| Author: | admin |
| Date Added: | 31-08-2009 |
| Time Limit: | 5 sec |
| Source Limit: | 50000 Bytes |
| Languages: | ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC |
Comments

Fetching successful submissions

it seems theres something
it seems theres something wrong with the case 2 1 2 1 2 1.
i can have set1=2 0 1 1 0 1 and set2=0 1 1 0 2 0. doesnt this satisfy the reqd condition?
please correct me if im wrong...
Why are there only 5 integers
Why are there only 5 integers in the last case?
And as the above comment says, the answer for 3rd case is Yes.
hi, last has 6 20s and the
hi,
last has 6 20s and the answer to the 3rd is a YES.
regards,
Ankur (event co-ordinator)
The third sample is wrong.
The third sample is wrong. The answer should be Yes.
I want the admins to check. I am damn sure about my solution..
Third one is WRONG. it is
Third one is WRONG. it is definitely "YES". i coded the problem twice and yet the answer is same. Please make appropriate changes so that the answer is shown as "Correct".
Er, take a look at the
Er, take a look at the comments section too. In fact, the one by Ankur just right above your own comment.
but still the judge gives
but still the judge gives incorrect submission, and i am damn sure that my solution gives correct result for all the input cases...So there may be something wrong in your result...
Wasted one hell of a time on
Wasted one hell of a time on this problem, and even after getting all answers correct ( twice), as Kunal said, the judge says "Wrong Answer". Something should be done for this, and that too quick.
hi, I've just contacted the
hi,
I've just contacted the author of the problem. He checked the solutions. The outputs are correct.
regards,
Ankur (event- co-ordinator)
#include<iostream> #include<s
#include<iostream>
#include<stdlib>
struct marble{
int type1;
int type2;
int type3;
int type4;
int type5;
int type6;
void input()
{
cin>>type1>>type2>>type3>>type4>>type5>>type6;
}
int total_val()
{
return(type1*1+type2*2+type3*3+type4*4+type5*5+type6*6);
}
int val(int i,int j,int k,int l,int m,int n)
{
return(i*type1+j*type2+k*type3+l*type4+m*type5+n*type6);
}
int split();
};
int marble::split()
{
int total = total_val();
if(total%2!=0)
return 0;
for(int i=0;i<=type1;i++)
for(int j=0;j<=type2;j++)
for(int k=0;k<=type3;k++)
for(int l=0;l<=type4;l++)
for(int m=0;m<=type5;m++)
for(int n=0;n<=type6;n++)
{
if(val(i,j,k,l,m,n)==val(type1-i,type2-j,type3-k,type4-l,type5-m,type6-n))
return 1;
}
}
main()
{
using namespace std;
int total_cases;
marble *marbles;
marbles=(marble*)malloc(total_cases*sizeof(marble));
cin>>total_cases;
for(int i=0;i<total_cases;i++)
marbles[i].input();
for(i=0;i<total_cases;i++)
{
if(marbles[i].split()==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
Is the above code correct??
Is the above code correct?? If not then please give corrections....ty
I got my error ......i forgot
I got my error ......i forgot to place else return 0; in split() function
The CORRECT program is as
The CORRECT program is as follows:
#include<iostream>
#include<stdlib>
struct marble{
int type1;
int type2;
int type3;
int type4;
int type5;
int type6;
void input()
{
cin>>type1>>type2>>type3>>type4>>type5>>type6;
}
int total_val()
{
return(type1*1+type2*2+type3*3+type4*4+type5*5+type6*6);
}
int val(int i,int j,int k,int l,int m,int n)
{
return(i*1+j*2+k*3+l*4+m*5+n*6);
}
int split();
};
int marble::split()
{
int tobereturned = 0;
int total = total_val();
if(total%2!=0)
return tobereturned;
int half_val=total/2;
for(int i=0;i<=type1;i++)
for(int j=0;j<=type2;j++)
for(int k=0;k<=type3;k++)
for(int l=0;l<=type4;l++)
for(int m=0;m<=type5;m++)
for(int n=0;n<=type6;n++)
if(val(i,j,k,l,m,n) == half_val)
tobereturned = 1;
return tobereturned;
}
main()
{
using namespace std;
int total_cases;
marble *marbles;
marbles=(marble*)malloc(total_cases*sizeof(marble));
cin>>total_cases;
for(int i=0;i<total_cases;i++)
marbles[i].input();
for(i=0;i<total_cases;i++)
{
if(marbles[i].split()==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
You are not supposed to post
You are not supposed to post codes here.So for future plz make sure you dont repeat this.Anyways first atleast run the code.The logic is right but efficiency is also taken into consideration here, using 6 nested for loops(have you considered the time it will take if there are 50 test cases).Think of Dynamic programming for solving this question or atleast use a break inside when you have found the value once.And what you have written will give segmentation fault if appropriate changes are not made to it.Also for the case when the values of all n1...n6 are 0 the answer should be No .