A man and a woman wash clothes together. It takes some time to wash clothes in different colors, in order not to allow two people to wash their clothes in one color, they must find the shortest time for the two to finish the clothes.
Idea: This question can be drawn from the idea of POJ 1014, there is a total time for each color to wash it, the minimum time required to wash this color, is to check whether a person can wash the clothes of this color to half the total time.
Code:
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
Struct node
{
Int time;
Char str [12];
} Lnode [102];
Int s = 0, num [12], dp [102*1002];
Int cmp (void const * a, void const * B)
{
Return strcmp (* (struct node *) a). str, (* (struct node *) B). str );
}
Int DP (int n, int count)
{
Int I = 0, j = 0, ave = num [count]/2;
Memset (dp, 0, sizeof (dp ));
For (I = s; I <n; I ++)
{
For (j = ave; j> = Lnode [I]. time; j --)
Dp [j] = dp [j]> dp [j-Lnode [I]. time] + Lnode [I]. time? Dp [j]: dp [j-Lnode [I]. time] + Lnode [I]. time;
}
S = n;
Return num [count]-dp [ave];
}
Int main ()
{
Int I = 0, n = 0, m = 0, sum = 0, count = 0;
Char str [12];
While (scanf ("% d", & m, & n), n + m)
{
Getchar ();
For (I = 0; I <m; I ++)
Scanf ("% s", str );
For (I = 0; I <n; I ++)
{
Getchar ();
Scanf ("% d % s", & Lnode [I]. time, Lnode [I]. str );
}
Getchar ();
Memset (num, 0, sizeof (num ));
Sum = count = s = 0;
Qsort (Lnode, n, sizeof (Lnode [0]), cmp );
Num [0] = Lnode [0]. time;
For (I = 1; I <n; I ++)
{
If (strcmp (Lnode [I]. str, Lnode [I-1]. str) = 0)
Num [count] + = Lnode [I]. time;
Else www.2cto.com
{
Sum + = DP (I, count );
Count ++;
Num [count] = Lnode [I]. time;
}
}
Sum + = DP (I, count );
Printf ("% d \ n", sum );
}
Return 0;
}
Author: ulquiorra0cifer