Dual-core processing

Source: Internet
Author: User

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

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.