1.3.1 Backpack model (1)

Insert image description here

01 Backpack
Division Basis: Rely on the "last step" to divide
Insert image description here
the complete backpack

Insert image description here

Insert image description here

Multiple backpacks
Insert image description here

Complete knapsack: Find the maximum value of all prefixes
Multiple knapsack: Find the maximum value within the sliding window

1. Multiple Knapsack Problem III

There is NNN items and a capacity areVVV 's backpack.

Part iiThere are at most si s_{i} for i itemssipieces, the volume of each piece is vi v_{i}vi,It's wi w_{i}wi

Find out which items should be packed into the backpack so that the total volume of the items does not exceed the capacity of the backpack and the total value is the largest.
Output the maximum value.

Input format:
Two integers in the first line, N, V ​​(0 < N ≤ 1000, 0 < V ≤ 20000) N, V ​​(0<N≤1000, 0<V≤20000)NV(0<N1000,0<V20000 ) , separated by spaces, indicating the number of items and backpack capacity respectively.

Next there is NNN lines, each line has three integersvi v_{i}vi, w i w_{i} wi, s i s_{i} siSeparated by spaces, they represent the volume, value and quantity of the i-th item respectively.

Output format
Output an integer representing the maximum value.

Data range
0 < N ≤ 1000 0<N≤10000<N1000
0 < V ≤ 20000 0<V≤20000 0<V20000
0 < v i , w i , s i ≤ 20000 0<v_{i},w_{i},s_{i}≤20000 0<vi,wi,si20000

Tip:
This question examines the monotonic queue optimization method for multiple backpacks.

Input sample

4 5
1 2 3
2 4 1
3 4 3
4 5 2

Output sample:

10
1.1 Solution

Difficulty warning! This question is one of the eight questions asked by Lou Guozhu Men, so act according to your ability!
The general idea of ​​the question:
  This question
is a solution to the multiple knapsack problem:
Let’s analyze the formula first.
Insert image description here
Okay, let me explain these
expressionsrrr : It’sjjj always minusvvThe remaining remainder at the end of v .

Then we will find that we cannot use the previous formula this time, because f [ i ] [ j − v ] f[i][j−v]f[i][jThere is one more item after v ] . We know the value of the last item, but we cannot deduce the values ​​of the previous items.
Mathematics teachers often tell us: draw pictures when you are in doubt. Then I will draw one and give it a try.
Insert image description here
I'm just giving a hint. The subscript at the bottom of this picture isthe volume
. We will find that each ffThe numbers in the f array are transferred to a length of k + 1 k+1k+The interval of 1 !
This is reflected in the picture:

f [ i ] [ j ] f[i][j] f [ i ] [ j ]不是以f [ i − 1 ] [ j ] , f [ i − 1 ] [ j − v ] + w , f [ i − 1 ] [ j − 2 v ] + 2 w . . . . . . f [ i − 1 ] [ j − sv ] + swf[i−1][j] ,f[i−1][j−v]+w,f[i−1][j−2v]+2w. .....f[i−1][j−sv]+swf[i1][j]f[i1][jv]+wf[i1][j2v ] _+2w......f[i1][jsv]+
What is transferred from s w is a window with a length of s+1!

Okay, you're already halfway through this question, drink some water and take a break ~


Read on next.
We will find that the initial formula, corresponding to this number axis, is ww added to the leftThe larger w is, so we need to reflectthe wwThe advantage of more w , so the formula becomes like this:

Insert image description here
That is, subtract some ww from each correspondencew

Insert image description here
Question session:
I Q: Why do you do this? Are you bored? ? ?
Me A: Specifically, it is the difference of xx between the current subscript and the subscript of the maximum value.xvv __v , thenxxx ww_w . The key point is that when comparing, compare relative sizes rather than absolute sizes! ! ! To put it in detail, it means that you cannot determine how many wware added to the number in the monotonic queue.w , it is impossible to compare.
Me Q: I understand, I understand, nbnb likes and likes.

1.2 Code implementation
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 20010;
int n, m;
int f[N], g[N], q[N];
//拷贝数组,因为这里不能使用一维优化,因为如果倒叙枚举,你就不能用滑动窗口了。

int main()
{
    
    
    cin >> n >> m;
    for (int i = 1; i <= n; i ++ ) {
    
    
        int v, w, s;
        cin >> v >> w >> s;
        memcpy(g, f, sizeof f);
        for (int j = 0; j < v; j ++ ) {
    
    
            int hh = 0, tt = -1;
            for (int k = j; k <= m; k += v) {
    
    
                if (hh <= tt && q[hh] < k - s * v) hh ++ ; //剔除超出长度元素
                if (hh <= tt) f[k] = max(f[k], g[q[hh]] + (k - q[hh]) / v * w); //更新当前答案
                while (hh <= tt && g[q[tt]] - (q[tt] - j) / v * w <= g[k] - (k - j) / v * w) tt -- ;
                //维持单调性
                //这里也可以这样写,更易理解
                //while (hh <= tt && g[q[tt]] <= g[k] - (k - q[tt]) / v * w) tt -- ;
                q[ ++ tt] = k;
            } 
        }
    }
    cout << f[m] << endl;
    return 0;
}

2. Collect herbs

Chenchen is a talented child, and his dream is to become the greatest doctor in the world.

To this end, he wanted to become a disciple of the most prestigious doctor nearby.

In order to judge his qualifications, the doctor gave him a difficult problem.

The doctor took him to a cave full of medicinal herbs and said to him: "My child, there are some different herbs in this cave. It takes some time to collect each one, and each one has its own value. I will give You have a period of time, during which time you can collect some herbs. If you are a smart child, you should be able to maximize the total value of the herbs you collect."

If you were Chenchen, could you complete this task?

Input format
The first line of the input file contains two integers TTT andMMM , separated by a space,TTT represents the total time that can be used to collect herbs,MMM represents the number of herbs in the cave.

Next MMEach line of M lines consists of two characters in 1 11 to100 100Between 100 (including1 11 and100 100100 ) integers respectively represent the time of picking a certain herb and the value of this herb.

Output format
The output file includes one line, which contains only one integer, indicating the maximum total value of the herbs that can be collected within the specified time.

Data range
1 ≤ T ≤ 1000 1≤T≤10001T1000,
1 ≤ M ≤ 100 1≤M≤100 1M100
input example:

70 3
71 100
69 1
1 2

Output sample:

3
2.1 Solution

Reference 01 Backpack

2.2 Code
#include<iostream>

using namespace std;

const int N = 1010;

int n,m;
int f[N];

int main()
{
    
    
    cin>>m>>n;
    for(int i = 0; i < n;i++)
    {
    
    
        int v,w;
        cin>>v>>w;
        for(int j = m;j >= v;j--)   f[j] = max(f[j],f[j - v] + w);
    }
    
    cout<<f[m]<<endl;
    
    return 0;
}

3. Packing problem

There is a box with capacity VVV , there arennn items, each item has a volume (positive integer).

Request nnPick any number of n items and put them into the box to minimize the remaining space in the box.

Input format
The first line is an integer VVV , represents the box capacity.

The second line is an integer nnn , represents the number of items.

nextnn _n lines, each line has a positive integer (not exceeding10000 1000010000 ), respectively representing thesennThe respective volumes of n items.

Output format
An integer representing the remaining space in the box.

Data range
0 < V ≤ 20000 0<V≤200000<V20000,
0 < n ≤ 30 0<n≤30 0<n30
input sample:

24
6
8
3
12
7
9
7

Output sample:

0
3.1 Solution

This question is a 01 backpack deformation question. Note that this question requires considering volume as value.

3.2 Code implementation
#include<iostream>
#include<algorithm>

using namespace std;

const int N = 20010;

int m,n;
int f[N];
int main()
{
    
    
    cin>>m>>n;
    for(int i = 0;i < n;i++)
    {
    
    
        int v;
        cin>>v;
        //m表示容量
        for(int j = m;j >= v;j--) f[j] = max(f[j],f[j - v] + v);

    }
    //注意:剩余空间
    cout<<m - f[m]<<endl;
    
    return 0;
}

4Pokemon Conquer

Pokemon is a story about the adventures of Ash Ketchum and his partner Pikachu.

One day, Xiaozhi and Pikachu came to the Elf Hunting Ground, which contained many precious wild Pokemon.

Xiaozhi also wants to conquer some of the elves.

However, wild elves are not so easy to tame.

For each wild elf, Xiaozhi may need to use many elf balls to conquer it, and in the process of conquering it, the wild elf will also cause certain damage to Pikachu (thus reducing Pikachu's physical strength).

When Pikachu's physical strength is less than or equal to 0, Xiaozhi must end the hunt (because he needs to heal Pikachu), and wild elves whose physical strength is less than or equal to 0 will not be conquered by Xiaozhi.

When Ash's Poké Balls run out, the hunt ends.

We assume that Xiaozhi has two choices when encountering a wild elf: conquer it, or leave it.

If Xiaozhi chooses to subdue the elf, he will definitely throw the elf ball that can subdue the elf, and Pikachu will definitely receive corresponding damage; if he chooses to leave it, then Xiaozhi will not lose the elf ball, and neither will Pikachu. physical strength.

Xiaozhi has two goals: the main goal is to conquer as many wild elves as possible; if the number of elves that can be conquered is the same, Xiaozhi hopes that Pikachu will suffer less damage (the remaining health will be greater), because they will continue adventure.

Now we know the number of Pokemon balls that Ash has and Pikachu's initial physical strength. We know the number of Pokemon balls that each Pokemon needs to conquer and the amount of damage it will cause to Pikachu in the process of being conquered.

Please tell me, how should Xiaozhi choose which elves to conquer in order to achieve his goal?

Input format

The first line of input data contains three integers: N , M , KN, M, KN , M , and K respectively represent the number of Xiaozhi's Pokémon balls, Pikachu's initial physical strength, and the number of wild Pokémon.

The following K lines, each line represents a wild elf, including two integers: the number of elf balls required to conquer the elf, and the damage caused to Pikachu in the process of conquering.

Output format

The output is a line containing two integers: C , RC , RC and R respectively represent the most conqueredCCsC elves, and the subjugation ofCCWhen there are C elves, Pikachu's remaining health value is at most R.

Data range
0 < N ≤ 1000 0<N≤10000<N1000,
0 < M ≤ 500 0<M≤500 0<M500,
0 < K ≤ 100 0<K≤100 0<K100

Input example 1:

10 100 5
7 10
2 40
2 50
1 20
4 20

Output sample 1:

3 30

Input example 2:

10 100 5
8 110
12 10
20 10
5 200
1 110

Output sample 2:

0 100
4.1 Solution

Two-dimensional cost 01knapsack problem
Insert image description here

Note: The question says: Wild elves that make Pikachu's physical strength less than or equal to 0 will not be conquered by Ash, so Pikachu's physical strength needs to start V2 - 1at

4.2 Code implementation
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1010, M = 510;

int n, V1, V2;
int f[N][M];

int main()
{
    
    
    cin >> V1 >> V2 >> n;
    for (int i = 0; i < n; i ++ )
    {
    
    
        int v1, v2;
        cin >> v1 >> v2;
        for (int j = V1; j >= v1; j -- )
            for (int k = V2 - 1; k >= v2; k -- )
                f[j][k] = max(f[j][k], f[j - v1][k - v2] + 1);
    }

    cout << f[V1][V2 - 1] << ' ';
    int k = V2 - 1;//找到当前皮卡丘消耗体力最少的情况
    while (k > 0 && f[V1][k - 1] == f[V1][V2 - 1]) k -- ;
    cout << V2 - k << endl;

    return 0;
}


Guess you like

Origin blog.csdn.net/m0_51366201/article/details/132030699