Algorithms fourth practice report

A practice topic

4-1 in storage procedures (90 points)
 

There n procedures {1,2, ..., n} to be stored in a length L of the tape. I programs stored on the tape length li, 1≤i≤n. The program storage seek to establish that n procedures stored in the program on the magnetic tape, the tape makes it possible to store as many programs. For a given n programs stored on the magnetic tape length, up to the number of programs can be stored on the tape is calculated.

Input formats:

The first line is a positive integer 2, respectively, and n denotes the length of the magnetic tape the number of documents L. 1 in the next row, with n a positive integer representing the program stored on the tape length.

Output formats:

The number of output can store up program.

Sample input:

Here we are given a set of inputs. E.g:

6 50 
2 3 13 8 80 20

Sample output:

Given here corresponding output. E.g:

5


Second, the problem described
has n files, there is a length L of the tape, to a file stored in a tape, and storage requirements of most programs, the first line of the input file and the number n of the length of the tape, the second input line is the length of each program.
Outline of Solution: In order to store more program, it should be small length of tape into a program priority, the file set according to the number n of the array size, the length of a file storing a program, and send them in ascending order, a register stores the magnetic tape, the tape length L, each time a stored
program length corresponding subtract, set a counter count is set to 0, the count stored in each ++, then a determination will be stored into the next program is smaller than the length of the remaining length of the tape, if it continues to meet the storage and subtracting the corresponding length, has to be recycled to the next
program stored exceeds remaining tape, the number of the output count. Should there still remaining space to store the number of files of all finished, the number of output files directly n.

Third, the algorithm description
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
 int n,w;
 cin>>n>>w;
 int sum=0;
 int a[10000]; 
 for(int i=0;i<n;i++){
  cin>>a[i];
 }
 sort(a, a+n);
 for(int i=0;i<n;i++){
  sum+=a[i];
  if(sum>w){ 
   cout<<i<<endl;
   return 0 ;
   } 
 }
 cout<<n;
 return 0;
}
View Code

1. greedy strategy: every time the minimum length of program priorities into the tape.

2. Use required to prove that a greedy choice and optimal substructure:

     Set E = {1,2, ..., n} is a set of a given program. Since E program on non-reduced length arranged in order of size, so that the program has a shortest length. First prove a program storage problem has an optimal solution to greedy selection began, that is, the optimal solution is included in the program 1. AE is provided an optimal solution to the storage problem program, and also by the length of the program A non-decreasing size arranged in order, the first procedure is Procedure A k. If k = 1, then A is a greedy to select the optimum starting. If k1, then let B = A- {k} ∪ {1}. Since f1≤fk, the program is compatible with A, B so that the program is compatible. And B is the same as the number of the program number and the program A, and A is the best, so B is optimal. In other words, B is greedy selection procedure began 1 of the best programs stored. Thus, there is always the optimal storage solution to greedy program selection began.

 

Fourth, time and space complexity of the algorithm

Time complexity: O (N); a for loop with the shortest procedure to sequentially store.

Space complexity: O (1); no additional space applications.

 

Fifth, feelings and experiences

  The problem for Hands greedy algorithm, when just starting only know a little theory, the actual implementation code from, there is an obvious upgrade for the greedy algorithm can be realized with a simple code to understand the key issues, how greedy strategy, pair programming with the case was key to the understanding of the various problems of their own, after consultation discussions with each other, can be drawn greedy strategy, then get step by step to achieve their greedy algorithm for the greedy algorithm, always looking for the fastest The shortest was most likely to meet the conditions to implement the greedy algorithm was able to meet the conditions.

Guess you like

Origin www.cnblogs.com/ydh52/p/11878087.html