Home > Back-end >  C, tug-of-war, dynamic programming
C, tug-of-war, dynamic programming

Time:09-29

For help, the problem how to calculate ah, here is the AC code, from the Internet to find, but I'm a little look not to understand, can someone explain it, especially the triple loop there





 # include & lt; stdio.h> 
# include & lt; String. H>
# define maxn 500 + 1
55 * int dp [55] [510]={0};
Int p={0} [maxn];
Int min (int a, int b) {
Return a}
Int Max (int a, int b) {
Return a> b? A: b;
}
Int main ()
{
int n;
While (the scanf (" % d ", & amp; N)!=(EOF)
{
Memset (dp, 0, sizeof (dp));
int sum=0;
for(int i=1; i<=n; I++)
{
The scanf (" % d ", & amp; P [I]);
The sum +=p [I];
}
Int N=N/2 + (N % 2==0? 1-0);
for(int i=1; i<=n; I++)
{
For (int j=min (N, I); J>=1; J -)
{
For (int k=sum/2; K>=0; K -)
{
If (k - p [I] & gt;=0)
{
If (==0) j - 1
Dp [j] [k]=(k==p [I]? P: [I] 0);
The else
If (dp [1] - [k p [I]].=0)
Dp [j] [k]=dp [1] - [k p [I]] + p [I];
}
}
}
}
Int w;
For (int I=sum/2; I>=0; I -)
{
If (dp [N] [I]!=0)
{
W=dp [N] [I];
break;
}
}
Int qq;
If (w> The sum - w)
{
Qq=sum - w;
}
The else
{
Qq=w;
W=sum - w;
}
Printf (" % d % d \ n ", qq, w);
}
return 0;
}