Page 4 of 5
Re: 562 - Dividing Coins
Posted: Fri Jan 14, 2011 10:02 am
by md_yusuf
why wa i have cant understand
Code: Select all
//C++
#include<iostream>
#include<algorithm>
using namespace std;
#define max_(a,b)(a>b?a:b)
int a[110];
int mark[10000][10000];
int store[10000][10000];
int n,t;
int dp(int i, int w)
{
if(i==0 || w==0)
return 0;
else if(mark[i][w]==t)
return store[i][w];
int res;
if(a[i]<=w)
{
res=max_(dp(i-1,w),dp(i-1,w-a[i])+a[i]);
}
else
res=dp(i-1,w);
mark[i][w]=t;
store[i][w]=res;
return res;
}
int main()
{
int i,w,cs,tt,sum,x;
//freopen("1.txt","r",stdin);
cin>>tt;
for(cs=1;cs<=tt;cs++)
{
t++;
cin>>n;
sum=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
w=sum/2;
x=dp(n,w);
printf("%d\n",(sum-2*x));
}
}
plz help me. im getting frustrated..
plz...
Re: 562 - Dividing Coins
Posted: Fri Jan 14, 2011 11:02 am
by sazzadcsedu
you should get memory limit exceed.How much memory you used-
Code: Select all
int mark[10000][10000];
int store[10000][10000];
Its a simple 1-dimentional DP problem.look at the sample input
4
1 2 4 6
Declare an array A[50000] because highest value may be 100*500=50000 and and fill it by 0.
now add all element .Sum=1+2+4+6=13.
as sum is odd you can't divide it evenly,so look for number closet to half-6.
so try to make sum=6 by adding some element.
For 1st element we have sum- 1. set A[1]=1;
For 2nd element we have sum-2,(2+1=3).set A[2]=1,A[3]=1.
For 3rd element we have sum-4,(1+4=5),(2+4=6) stop as you find 6 from element 2,4=6 and other set certainly contain=13-6=7.
SO diff is 1.
Hope you understand .
Re: 562 - Dividing Coins
Posted: Fri Jan 14, 2011 11:49 am
by md_yusuf
thx via. i cant understand why im getting wa. I should get re or ce.. bt making the the arry mark[101][25001] and mark[101][25001] get accepted its too weird to me.
Re: 562 - Dividing Coins
Posted: Wed Feb 02, 2011 8:30 pm
by Shafaet_du
I got a wa for a simple but tricky case.
output is 0
another case that may help you:
ans: 47
Re: 562 - Dividing Coins
Posted: Thu Jun 09, 2011 4:41 pm
by mdpallob
got two times wrong answer.
Can anyone help me?
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int value=0;
int diff;
for(int i=1;i<=n;i++)
{
int coin_no;
cin>>coin_no;
for(int x=1;x<=coin_no;x++)
{
int val;
cin>>val;
value=value+val;
}
diff=value%2;
cout<<"\n"<<diff;
}
return 0;
}
Re: 562 - Dividing Coins
Posted: Wed Jun 15, 2011 8:26 pm
by Imti
Hello
My code passing all the input found here in this board and also my hand-generated one's(compared with toolkit output).I used simple DP here.
I worked like..
1)Sum up all the coin first
2)Then set S=sum/2
3)Now I used 0-1 knapsack here.That is Fill knapsack with size S as maximum as possible
4)then output sum-2*p[n][S]
But I'm getting WA..Here is my code
Edit:
A silly Bug.Algorithm was okay.allocated memory less than program need..wondered to see no one here in this board to help....its dead one..

Re: 562 - Dividing Coins
Posted: Sat Jun 25, 2011 4:12 pm
by Imti
mr. mdpallob
What you did???You really understood what problem wants??I don't think so
Your algorithm is totally wrong.Your code always produce minimal difference as 0 or 1.But answer may be greater than 0 or 1 too. Read carefully problem statement again and try learning Dynamic programming technique.
Re: 562 - Dividing Coins
Posted: Tue May 22, 2012 7:51 pm
by mostafiz93
I used 0-1 knapsack to solve this problem.
can anyone find any bug in my code:
Re: 562 - Dividing Coins
Posted: Tue May 22, 2012 11:48 pm
by brianfry713
see I/O #7 at
http://ideone.com/XgQ97, AC output is 1.
Re: 562 - Dividing Coins
Posted: Mon May 28, 2012 5:40 pm
by mostafiz93
thanks brianfry!
got AC.
Re: 562 - Dividing Coins
Posted: Fri Jun 15, 2012 12:38 am
by shopnobaj_raju
Re: 562 - Dividing Coins
Posted: Fri Jun 15, 2012 9:40 pm
by brianfry713
For one thing, this declares more memory than you need to solve this problem.
long int coins[102],opt[102][25010];
Your RE is here:
for(i=0;i<103;i++)
for(j=0;j<25011;j++) opt[j]=-100;
Re: 562 - Dividing Coins
Posted: Sat Jun 16, 2012 2:53 pm
by shopnobaj_raju
Thanks
562 - Dividing coins and Knapsack Algorithm
Posted: Tue Jan 22, 2013 3:30 am
by MauriWilde
This looks like an easy knapsack problem, but I don't know the knapsack algorithm. So, could anyone give me a pseudo-code for this problem? Or a pseudo-code for the knapsack algorithm?
Thank you very much.

Re: 562 - Dividing coins and Knapsack Algorithm
Posted: Tue Jan 22, 2013 10:41 pm
by brianfry713