[Nov1 P3, respectable second answer] Software Company

Source: Internet
Author: User

The second answer is a good thing ~ However, there are many ways to verify the correctness of the solution. This is a classic DP verification, and it is not so good for this DP verification. Well, let's get into the question.

 

 [Description]
A software development company needs to develop two software at the same time and deliver it to users at the same time. Now the company divides each software into M modules to complete this task as soon as possible, the division of labor is completed by technicians in the company. Each technician takes the same number of days to complete different modules of the same software and is known. however, the time required to complete a module of different software is different. Each technician can create only one module at a time. A module can only be completed by one individual, but not by multiple persons. After a technician completes a module during the entire development period, he can proceed with any module of any software. Write a program to find out when the company can deliver the software at the earliest.
[Input format]
The first line of the input file contains two integers n and m separated by spaces. 1 ≤ n ≤ 100.1 ≤ m ≤ l00.
Each line in the next n rows contains two integers D1 and D2 separated by spaces. D1 indicates the number of days required by the technician to complete a module in the first software, d2 indicates the number of days required by the technician to complete a module in the second software. where 1 is less than or equal to D1 and D2 is less than or equal to 100.
[Output format]
Only one row of the output file contains an integer d, which indicates that the company can deliver the software as early as D days later.
[Example input]
3 20
1 1
2 4
1 6
[Sample output]
18
[Note]
The fastest solution is that the first technician completes 18 modules of the second software. in 18 days, the Third Technician completes 18 modules of the first software, in 18 days. the remaining modules are completed by the Second Technician, which takes 12 days. It takes 18 days to complete all modules. If the first technician completes 17 modules of the second software. the Third Technician completes 17 modules of the first software, and the other modules are completed by the Second Technician. it takes 18 days. it still takes 18 days to complete all modules, so it is impossible to complete all modules in less than 18 days.

It is also a grouping question. Naturally, you can consider the second answer. We use mid to calculate the minimum time. In this way, F [I, j] indicates the total number of modules of the second project that I can do after completing the module of the first project of J. As long as you find the value of F [n, m] and compare it with M, you will know that within the mid time, you must finish M Project 1, is it possible to complete M project 2 modules. If f [n, m]> m, then mid is the feasible solution. Then we can perform the Second Division on this basis to find the optimal solution.

With this idea, the dynamic equations are also very clear.

F [I, j] = max {f [I-1, J-K] + (mid-A [I] * k) Div B [I])} (K <= J)

Here, K indicates that only K modules of the first project are used, and the time of mid-A [I] * k is used for the second project.

Note: first, pay attention to the K <= J condition. Otherwise, the F array will be out of bounds. Second, it is necessary to initialize the f array to a negative value, because it may be caused by focusing on Project 1 rather than project 2. In this way, F [0, 0] should be initialized to 0. In addition, the interval of the second answer should be set enough. Because it is a matter of two points, it does not matter if you decide more, that is, you need to look for more things. But when you set it down, it will be a tragedy ~ Finally, because the mid found at the beginning is only a feasible solution, if f [n, m]> M is satisfied, the feasible solution should be recorded in [L, mid-1) in the interval.

 

Reference code:

 

 1 program software;
2 VaR
3 F: array [0 .. 100, 0 .. 100] of integer;
4 I, j, L, R, Maxx: integer;
5 N, M, mid, ANS, C: integer;
6 A, B: array [1 .. 100] of integer;
7 function max (X, Y: longint): longint;
8 begin
9 if x> Y then exit (X)
10 else exit (y );
11 end;
12 function check (X: longint): longint;
13 VaR
14 I, J, K: integer;
15 begin
16 For I: = 0 to n do
17 For J: = 0 to M do
18 F [I, j]: =-maxint; // The value is initialized to a negative value.
19 F [0, 0]: = 0;
20 For I: = 1 to n do
21 For J: = 0 to M do
22 For K: = 0 to X Div A [I] Do
23 if k <= J then
24 f [I, j]: = max (F [I, j], F [I-1, J-K] + (X-A [I] * K) div B [I]);
25 exit (F [n, m]);
26 end;
27 begin
28 readln (n, m );
29 For I: = 1 to n do
30 begin
31 readln (A [I], B [I]);
32 If a [I]> Maxx then Maxx: = A [I];
33 if B [I]> Maxx then Maxx: = B [I]; // find the slowest result. multiply the number of modules by the upper limit of the second.
34 end;
35 Maxx: = Maxx * m;
36 L: = (M * 2) Div N;
37 R: = Maxx;
38 while l <= r do
39 begin
40 mid: = (L + r) Div 2;
41 c: = check (MID );
42 if C> = m then
43 begin
44 r: = mid-1;
45 ans: = mid;
46 end
47 else l: = Mid + 1;
48 end;
49 writeln (ANS );
50 end.

(Saltless original, reprinted please indicate the source)

Related Article

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.