Greedy algorithm to find the "best" I Want It All!

Looking for "the most beautiful" greedy algorithm, I Want It All

- Chapter IV summarizes the algorithm soft three Yang Geng 20181003083

One, Seeing, greedy strategy

 

Greedy algorithm always makes the current view is the best choice, greedy algorithms are not to be considered as a whole the best, but the choices made in the best sense of the local, of course, we also hope to get final the result is the best overall. In my opinion, it is to find a "most" of the process, such as by ordering the biggest, the smallest sort, the average maximum and so on. The disintegration of thinking generally is to first find a "most", and then think if you can find counterexamples to overthrow the "most", there are counter-examples, then re looking for "most", until you can prove by contradiction and so the "most" is the best choice , without the presence of counterexample.

 

Vehicle refueling problem:

Car refueling  (15  points )

Topic Source: Wang Xiaodong, "Design and Analysis of Algorithms"

Fill up the car can travel n kilometers. There are a number of gas stations in the journey. Design an efficient algorithm, which pointed out that should stop refueling at the gas station, refueling along the way so that the least number.

Input formats:

The first line has two positive integers n and k (k <= 1000), fill up the car can travel represents n km, and the road has k stations. The second row has k + 1 integers, the distance between the k-th and the k-1 stations gas stations. 0 represents the first gas station of departure, fill up the car already. The first k + 1 gas stations expressed destinations.

Output formats:

Output minimum number of refueling. If you can not reach the destination, the output "No Solution!".

Sample input:

7 7
1 2 3 4 5 1 6 6 

Sample output:

4

 

Select greedy nature: a record number variable sum spent gasoline kilometers can travel, when the accumulated time exceeds the maximum number of kilometers will refuel, which record the number of time with another variable. If the gas station is too far away (more than one time refueling can travel the farthest distance, you can not reach, output "No Solution!").

Much distance as possible every time travel, not to refuel.

Source code is as follows:

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main() {
 6 
 7     int n,k,a[1001];
 8 
 9     cin>>n>>k;
10 
11     for(int i = 0; i <= k; i++) {
12 
13         cin>>a[i];
14 
15     }
16 
17     if(a[k] > n) {
18 
19         cout<<"No Solution!"<<endl;
20 
21         return 0;
22 
23     }
24 
25     int sum = 0, p = 0;
26 
27     for(int i = 0; i <= k; i++) {
28 
29         sum += a[i];
30 
31         if(sum > n) {
32 
33             p++;
34 
35             sum = a[i];
36 
37         }
38 
39     }
40 
41     cout<<p<<endl;
42 
43     return 0;
44 
45 }

 

 

Second, the pair programming leak filled

Greedy algorithm is simple, with little need to discuss the case of problem-solving ideas, but many of the details and optimized the code to play need to think together. Pair programming is a leak process to fill a vacancy, two people can see each other's deficiencies and shortcomings in the problem solving process, you can point out each other and learn to correct each other.

The fourth chapter is to find a local optimum, and proved to be the best overall choice of topic. Code implementation difficulty is that, with respect to solving idea is derived.

Guess you like

Origin www.cnblogs.com/990924991101ywg/p/11922766.html