0-1 backpack

Source: Internet
Author: User

1. poj 3211 inserting clothes

Analysis: the order of clothes in a certain color is arbitrary, and the problem can be converted to: When the clothes in a certain color are washed at least, two people can wash them at the same time, to minimize the time difference between two people, that isTake 1/2 of the total time as the backpack capacity, and find the value closest to 1/2.

0-1 backpack with only weight and no weight:

Memset (F,-1, sizeof (f); // Initialization
F [0] = 0;
For (j = 0; j <CT [I]; j ++ ){
For (int K = vol-time [I] [J]; k> = 0; k --){
If (F [k]> = 0 ){
F [K + time [I] [J] = 1;
}
}
}

View code

 1 # Include <stdio. h> 2 # Include <String . H> 3   4   Int  Main ()  5   {  6       Int  N, m;  7       Char Clor [ 15 ] [ 15  ];  8       Int Time [15 ] [ 100 ], CT [ 15 ], Sum [ 15  ];  9       Int F [ 100010  ];  10       While (Scanf ( "  % D  " , & N, & M) & (N | M )){ 11           Int  I, J;  12   13           For (I = 0 ; I <n; I ++ )  14 Scanf ( "  % S  "  , Clor [I]);  15 Memset (CT, 0 , Sizeof (CT ));  16 Memset (sum, 0 , Sizeof  (SUM ));  17           For (I = 0 ; I <m; I ++ ){  18               Int  T;  19               Char Cl [ 15  ]; 20   21 Scanf ( "  % D % s  " ,& T, Cl );  22               For (J = 0 ; J <n; j ++ ){  23                   If (Strcmp (CL, clor [J]) = 0  ){  24 Time [J] [cT [J] ++] =T;  25 Sum [J] + = T;  26                       Break  ;  27   }  28   }  29   }  30           31           Int St = 0 ;  32           For (I = 0 ; I <n; I ++ ){  33               Int Vol = sum [I]/ 2  ;  34 Memset (F ,- 1 , Sizeof  (F ));  35 F [ 0 ] =0  ;  36               For (J = 0 ; J <CT [I]; j ++ ){  37                   For ( Int K = vol-time [I] [J]; k> = 0 ; K -- ){  38                       If (F [k]> = 0  ){ 39 F [K + time [I] [J] = 1  ;  40   }  41   }  42   }  43               Int V1 = Vol;  44               While (F [V1] < 0 ) V1 --;  45 St + = sum [I]- V1;  46               47   }  48 Printf ( "  % D \ n  "  , St );  49   }  50       Return   0  ; 51 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.