Question link: http://poj.org/problem? Id = 3211
There are m pieces of clothing. Each type of clothing has a color. There are n colors in total. Now, when two people wash clothes, the rule is to wash all the clothes of this color before they can wash the clothes of the next color.
Q: How long does it take for two people to wash their clothes at the same time.
If two people wash clothes of the same color at the same time, the minimum time is half the total time of the clothes of the same color.
We can separate each type of clothes to see it as a 01 backpack. The capacity is half the total time of washing the color clothes.
The maximum length. The total time of the color-the maximum time the person spends is the time for another person to wash the color.
Finally, find the maximum value.
Code:
1 import java.util.*; 2 3 public class Main{ 4 static HashMap<String,Integer> hs; 5 public static void main(String[] args){ 6 Scanner cin = new Scanner(System.in); 7 while( true ){ 8 int N = cin.nextInt(); 9 int M = cin.nextInt();10 11 if( N==0&&M==0 ) break;12 13 int c[][] = new int[N+1][M+1];14 15 hs = new HashMap<String,Integer>();16 17 for(int i=1;i<=N;i++){18 String s = cin.next();19 hs.put(s, i);20 }21 22 int sum[] = new int[N+1];23 24 int sumn = 0;25 26 for(int i=1;i<=M;i++){27 int ta = cin.nextInt();28 String tb = cin.next();29 int x = hs.get(tb);30 c[x][++c[x][0]] = ta;31 sum[x] += ta;32 sumn += ta;33 }34 35 int dp[][] = new int[N+1][sumn+100];36 int ans = 0;37 for(int i=1;i<=N;i++){38 for(int k=1;k<=c[i][0];k++){39 for(int j=sum[i]/2;j>=c[i][k];j--){40 if( j>=c[i][k] )41 dp[i][j] = Math.max(dp[i][j],dp[i][j-c[i][k]]+c[i][k]);42 }43 }44 ans += dp[i][sum[i]/2];45 }46 47 System.out.println(Math.max(ans,sumn-ans)); 48 49 }50 }51 }
[Poj 3211] Rolling ing clothes (Dynamic Planning)