Blog 12 | 3 (Chapter 4 - greedy) algorithm-test report

· 3 algorithm experimental report

Subject: greedy

Time: 2019/11/16

 

Analysis program storage problems. content include:

1, practice title

 

 

2. Description of the problem

Set E = {l1, l2, l3 , l4, l5, l6}, i.e., the initialization (block length by a non-descending order ) to be selected from the length of the program, set A = {}, i.e., has been selected program length.

The i-th selection, E = {li + 1, li + 2 ....}, A = {.... li-1, li}, i.e., the current to be selected from each selection procedure the smallest program size, until the sum of the length selected is greater than L.

 

3, the algorithm description

(1) greedy strategy: Each time you select the length of the shortest program ever to be produced using a new tape length (sub-problems).

(2) Code

#include <iostream>
#include <algorithm>
using namespace std;

int length[1000];

int answer(int n,int l){
    sort(length,length+n);
    int r = l;
    int count = 0;
    for(int i = 0; i < n ;i++){
        if(length[i]<=r){
            count++;
            r = r-length[i];            
        }
        else
            break;
    }
    return count;
}

int main(){
    int n,l;
    cin>>n>>l;
    for(int i = 0 ;i < n ; i++){
        cin>>length[i];
    }
    cout<<answer(n,l);
}

 

(3) proof

Sub-questions-1, the n- sub-question 1 is optimal. 

btw examine optimal solutions of a /, proved optimal solution can be modified such that it starts from the greedy choice, then each step by mathematical induction to prove the optimal solution can be obtained by selecting greedy
1, assuming the preferred selection element is not greedy the elements to prove the first element to replace greed select the desired element, still get the optimal solution;
2, mathematical induction to prove every step of the optimal solution can be obtained by greedy selection

 

(4) Specific proof

Select the greedy nature:

Proof: Suppose A = {l1 ....} is the optimal solution, if there is a choice of an optimal solution of the problem is lk (k> 1), B is the optimal solution set, set B = A - {k} ∪ {l1}

Since A, B, the same number of programs, l1 <A - {k}, B so that the program is compatible, so A, B solution is optimal, that is B is a Greedy selection process begins l1 .

Thus, the optimal solution to the problem always greedy Select Start existence.

 

Optimal substructure Properties: set selection A` = A - {l1} (set to be selected after the selection procedure 1), i.e. A 'is proved E` = E- {l1} optimal solution.

prove:

A` is assumed that the optimal solution is not E`, then add {l1} A` more than the number of the program A, and A is the optimal solution contradictory assumptions, is proved.

 

4, algorithm complexity analysis time and space

(1) time complexity: due to the use of a fast-discharge nlogn, other statements rough calculation is n, so the time complexity is O (nlogn).

(2) the spatial complexity: opening an array, so the space complexity is O (1).

 

5, and experience (the practice of this harvest and doubts summarize)

The practice of the title design less time, due to an error in the second question other than the idea of ​​various algorithms (IF boundary determination / can not be received by the input bit long int / revolution string array) has been stuck lead,

And in proving greedy strategy is still little knowledge of the state, indicating programming, mathematics foundation is not solid, lead to increase the amount of programming to warn, to keep learning mathematics.

At the same time, the difference between greedy and dynamic, the partition of a deeper understanding, greed does not need to consider "after", only need to consider the current / past, top-down.

 

 

Reference blog:

https://blog.csdn.net/sn_zzy/article/details/16892031

https://blog.csdn.net/fan2273/article/details/73549016

Guess you like

Origin www.cnblogs.com/gzq18/p/11876634.html