Algorithm training and packing problem (greedy, dynamic programming, blue bridge cup, C++)

Algorithm training and packing problem

Resource limit
Time limit: 1.0s Memory limit: 256.0MB The
problem describes
  a box with a capacity of V (positive integer, 0<=V<=20000), and there are n items (0<n<=30), each item There is a volume (a positive integer).
  It is required that among the n items, any number of them can be put into the box, so that the remaining space of the box is minimized.
Input format    The
  first line is an integer, indicating the box capacity; the
  second line is an integer, indicating that there are n items; the
  next n lines, an integer in each line indicates the respective volume of the n items.
Output format    is
  an integer, indicating the remaining space of the box.
  Sample input
  24
  6
  8
  3
  12
  7
  9
  7
Sample output
0

Ideas and analysis:

1. Although the question stem suggests that this is a dynamic programming , obviously this is a relatively simple and basic greedy . First of all, when we consider this question, we must analyze how to minimize the remaining space, then you will think of installing as large as possible. The most greedy items are the large ones , and the remaining space will be very small. Then we can sort the weight of the items first when we can first think about it: (STL is really easy to use)

	#include<algorithm>
	
	sort(&a[0],&a[n]);

2. For example, our capacity is 10, 4 items at this time, respectively 2, 3, 1, 4, then the array becomes 1 2 3 4 after sort, and then we can move forward from the back (n-1) (0) Start traversal. If the item is less than the capacity if the condition is met, then the capacity is subtracted from the item size:

for(int i =n - 1 ;i >= 0;i--)
    {
    
    
        if(box >= a[i])
        {
    
    
            box -= a[i];
        }
    }

※When I submitted the code happily, the sample only gave me 80 points (I have to say that the data is really watery, otherwise I can't get 40)

At this time, I began to think about whether there are special circumstances that have not been taken into account. ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

For example: the capacity is 10 and 5 items, respectively 1 2 3 4 5, then if you look from the back to the front, first install 5, then 4, you can no longer install, the remaining space is 1, but our naked eyes It can be judged that 1 + 2 + 3 + 4 == 10, it should be installed as 4 3 2 1. At this time, the thinking must be changed, and the algorithm must be changed from O(n) to O(n²), because the first In the case of different items loaded , the final remaining space is also different, so it is necessary to traverse the remaining space at the beginning of each situation , and then take the minimum value of these remaining spaces!

int ans = box;					//答案最初等于箱子的容量
for(int i =n - 1 ;i >= 0;i--)	//i是第一个装的物品
    {
    
    
        min_val = box;			//每一次最小值要重置为箱子初始的容量
        for(int j = i ;j>= 0;j-- ) //第一个是i的情况下,向前遍历
        {
    
    
               if(min_val >= a[j])	//如果容量比物品大,那就装!
                {
    
    
                   min_val -= a[j];	//减去物品
                }
        }
        ans = min(ans,min_val);	//在答案和每次剩余容量中取最小值
    }

3. This question may use dp, but I haven't thought about it yet, you can look at the codes and ideas of other big guys

Finally, attach the complete AC code:

#include <stdio.h>
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int box,n,min_val;
	int a[32];          ///装物品
	cin >> box >> n;
	int ans = box;
	for(int i = 0;i < n; i++)
    {
    
    
        cin >> a[i];
    }
    sort(&a[0],&a[n]);

    for(int i =n - 1 ;i >= 0;i--)
    {
    
    
        min_val = box;
        for(int j = i ;j>= 0;j-- )
        {
    
    
               if(min_val >= a[j])
                {
    
    
                   min_val -= a[j];
                }
        }
        ans = min(ans,min_val);
    }
    cout << ans;
	return 0;
}

Afterword: I generally like more concise algorithm ideas. I usually see the concise code of other big guys and not only sigh, so the code is generally shorter. I like to share this kind of efficient algorithm with everyone. If you have this question Please communicate with us for better algorithms!

Guess you like

Origin blog.csdn.net/Kyrie_irving_kun/article/details/113638715