A dual-core CPU of two cores capable of simultaneous processing tasks, now have n a known amount of data for the task to be handed over to the CPU, assuming that each core known CPU 1 seconds can handle 1KB, each core can only handle one task at a time. n tasks can be placed in any order in the CPU processing, it is now necessary to design a scheme for the CPU to complete the task of the minimum time required to seek this minimum time.
Input Description:
The input consists of two lines: the first behavior integer n (1≤n≤50) the second behavior n integers length[i] (1024≤length[i]≤4194304), which indicates that each task has a length of length[i]kb, each of which is a multiple of 1024.
Output Description:
Outputs an integer that indicates the minimum time to process
Example 1
Input
5
3072 3072 7168) 3072 1024
Output
9216
The first method (exhaustive method)
n =Int (raw_input ()) a=raw_input () a=A.strip (). Split () a= [Int (x)/1024 forXinchA]suma=sum (a) x= {0:1} forIincha:y= {} forJinchx:ifJ+i not inchx:y[j+i] = 1x.update (y) Res=Suma forIinchX:res= Min (Res,max (i,suma-i))Print(res*1024)
The x key in the code records the time it takes to select any task from n tasks, each combination. If the time is the same, record it only once, so that all the possibilities are listed. Finally, iterate over and select the combination of the minimum time required for the dual-core CPU to process all tasks
Second method (Dynamic planning)
n =Int (raw_input ()) a=raw_input () a=A.strip (). Split () a= [Int (x)/1024 forXinchA]suma=sum (a) DST= [[0]* (Suma/2+1)]* (n+1)defPack (i,rest):ifI >=N:return0ifDst[i][rest]:returnDst[i][rest]ifA[i] <=Rest:result= Max (A[i]+pack (I+1,rest-a[i]), pack (i+1, rest)) Else: Result= Pack (i+1, rest) dst[i][rest]=resultreturnResultres= Pack (0,SUMA/2)Print(Max (res,suma-res) *1024)
The solution of--01 knapsack problem with dynamic programming is borrowed.
Dual-core processing