[luogu p2392] kkksc03 temporarily hugs Buddha feet before the exam

Portal

kkksc03 Temporarily hold Buddha feet before the exam

Topic background

Kkksc03's university life is very decadent and he doesn't study at all. However, as he neared the final exam, he had to start to embrace Buddha's feet in order not to leave the subject.

Title description

For this final exam, kkksc03 needs to take the \ (4 \) subject . Therefore, to start brushing problem sets, each subject has a problem set with \ (s_1, s_2, s_3, s_4 \) questions. It takes some time to complete each question . ldots, A_ {s_1} \) , \ (B_1, B_2, \ ldots, B_ {s_2} \) , \ (C_1, C_2, \ ldots, C_ {s_3} \) , \ (D_1, D_2, \ ldots, D_ {s_4} \) ).

kkksc03 has an ability. His left and right brains can simultaneously calculate \ (2 \) different topics, but only in the same subject. Therefore, kkksc03 must be reviewed one by one.

Since kkksc03 is still anxious to deal with Luogu's bugs, he wants to finish things as soon as possible, so he wants to know the shortest time to complete the review.

Input and output format

Input format

This question contains \ (5 \) lines of data: line \ (1 \) , which is four positive integers \ (s_1, s_2, s_3, s_4 \) .

Line \ (2 \) is the total number of \ (s_1 \) in \ (A_1, A_2, \ ldots, A_ {s_1} \) , which represents the time consumed by each question in the first subject problem set.

Line \ (3 \) is the total number of \ (s_2 \) in \ (B_1, B_2, \ ldots, B_ {s_2} \) .

Line \ (4 \) is the total number of \ (s_3 \) in \ (C_1, C_2, \ ldots, C_ {s_3} \) .

Line \ (5 \) is \ (D_1, D_2, \ ldots, D_ {s_4} \), a total of \ (s_4 \) , the meaning is the same as above.

Output format

A line is output, which is the shortest time for review.

Sample input and output

Input sample # 1

1 2 1 3
5
4 3
6
2 4 3

Sample output # 1

20

Explanation

\ (1 \ leq s_1, s_2, s_3, s_4 \ leq 20 \)

\(1\leq A_1,A_2,\ldots,A_{s_1},B_1,B_2,\ldots,B_{s_2},C_1,C_2,\ldots,C_{s_3},D_1,D_2,\ldots,D_{s_4}\leq60\)

analysis

First of all, in this question, kkk needs to take four subjects. Obviously, these four subjects are not related to each other, because the calculation of the left and right brains of kkk must be the same subject, so the entire optimal solution is actually the sum of the optimal solutions of the four subjects. Then we will analyze one subject.

Obviously, if the sum of the time of each question in a certain subject is sumthe best case sum / 2, it means that the left and right brains are divided. But most of the cases do not allow us to do this. We can only make the time difference between the left and right brains as small as possible.

We can change our thinking and assign the exercises to half of the brains as close as possible sum / 2, then the time required by the other brain at this time is the optimal solution for this subject.

In other words, let the exercises be filled as much as possible sum / 2, but not exceeded sum / 2, and each exercise can only be selected and not selected.

How about it, come up with it, it is a proper 01backpack. 01Just follow the idea of ​​the backpack.

Finally, adding the optimal solutions of the four subjects is the answer.

Talk is cheap, show you the code.

Code

/*
 * @Author: crab-in-the-northeast 
 * @Date: 2020-04-12 20:54:32 
 * @Last Modified by: crab-in-the-northeast
 * @Last Modified time: 2020-04-12 21:53:26
 */
#include <iostream>
#include <cstdio>
#include <cstring>

inline int max(int a, int b) {
    return a > b ? a : b;
}

int main() {
    int s[5], ans = 0;
    for(int i = 1; i <= 4; i++) std :: cin >> s[i];
    for(int i = 1; i <= 4; i++) {
        int sum = 0, a[25], dp[1205] = {0};
        for(int j = 1; j <= s[i]; j++) {
            std :: cin >> a[j];
            sum += a[j];
        }
        for(int j = 1; j <= s[i]; j++)
            for(int k = sum / 2; k >= a[j]; k--)
                dp[k] = max(dp[k], dp[k - a[j]] + a[j]);
        ans += sum - dp[sum / 2];
    }
    std :: cout << ans << std :: endl;
    return 0;
}

Evaluation results

AC 100:R32745565

Guess you like

Origin www.cnblogs.com/crab-in-the-northeast/p/luogu-p2392.html