#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main(){
int n, m, s;
bool a[45002];
a[0] = 1;
scanf( "%d", &n );
for ( int i = 0; i < n; i++ ){
memset( a, 0, 45002 * sizeof( bool ) );
a[0] = 1;
int sum = 0;
scanf( "%d", &m );
for ( int j = 0; j < m; j++ ){
scanf( "%d", &s );
sum += s;
for ( int k = 45001; k >= s; k-- ){
a[k] |= a[k-s];
}
}
for ( int k = ( sum ) / 2 ; k >= 0; k-- ){
if ( a[k] ){ printf("%d %d\n", k, ( sum ) - k );
break;
}
}
}
}
I don't know what's wrong with this.. it works with most of the cases I've tried it with. Thanks in advance!
hi~
I always got wrong answer!
I didn't find any bug.
I use Greedy Algorithm to use this problem.
Please give me some test data of this problem.
(I think it well be helpful~)
[c]
#include "stdio.h"
#include "stdlib.h"
#define MAX 10000
int man[120];
void solve(int n)
{
int i,j,k,p,amt=0,max,min;
int s=0,target,minof,index;
target=0;
for(i=1;i<=n;i++)
target+=man;
target*=10;
target/=2;
minof=MAX;
if(n%2==0) max=n/2;
else max=n/2+1;
Are you sure? I think there is somewhere in the problem that the groups must be divided into groups as close in numbers as possible. Hence the output should be 13 5 instead.
Oops, ... you're totally correct. I should have read the problem desc. more carefully ... Next time I post a comment, I'll try to solve the problem first ...
I recently solved it using straightforward logic that will have all possible weights info, and at the end look up the best subset of (n/2) people that has minimal weight-difference. However, it is pretty slow ... I got AC in about 2 seconds.
Can anybody give a hint about another faster approach ... ???
Thanks for all the attention
-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
it's my code
[pascal]var tab:Array[1..5000] of longint;
z:array[0..5000] of integer;
qw,how,sss,s,p,d,w,n,ile,a,q,srednia:longint;
begin
readln(how);
for sss:=1 to how do
begin
qw:=0;
s:=0;
p:=0;
d:=0;
n:=0;
ile:=0;
q:=0;
srednia:=0;
for a:=1 to 5000 do begin tab[a]:=0; z[a]:=0; end;
a:=0;
readln;
readln(ile);
for a:=1 to ile do begin read(tab[a]); srednia:=srednia+tab[a]; end;
readln;
qw:=srednia;
srednia:=srednia div 2;
z[0]:=1;
repeat
n:=n+1;
for q:=srednia+1 downto tab[n] do
if z[q-tab[n]]=1 then z[q]:=1;
until (n=srednia) ;
s:=srednia+1;
repeat
dec(s);
if z[s]=1 then begin p:=s; s:=1; end;
until s=1;
d:=qw-p;
if p>d then writeln(d,' ',p) else writeln(p,' ',d);
writeln;
Given a number of players with their weights, create the most evenly weighted teams where the number of players on each team is equal or off by one.
Here's my code. I basically make the most evenly weighted teams and then check to see if they are within one of each other. If not, I fall back to the previous item and switch teams.
/*for(i = 0; i < numpeople; ++i)
printf("%i ", weights);*/
--numcases;
}
return 0;
}
int addweight(int n, int weighta, int weightb, int playersa, int playersb, int *weights)
{
/*weightflag 1: a is smaller, 0: b is smaller*/
int stop, weightflag;
/*printf("%i: weighta:%i playersa:%i, weightb:%i playersb:%i\n", n, weighta, playersa, weightb, playersb);*/