& A greedy sort through 1431: fishing problem solution

Title transfer

 

( In fact, there is a solution to a problem after corrections )

I looked for a long time and found that this problem is a dynamic programming looks like ah, but that is where the greedy exam questions, or think of it with a greedy solution.

After (learn Gangster ideas) after a very complex thought, finally understanding the greedy thinking this problem. The main difficulty of the problem can be stopped at any lake in the end, and can not go back, the efficiency of fishing in a lake will be less and less. Conventional thinking seems to be not enough, the subject of a lot of dynamic unknown quantity, we only replace the angle, "of moving to static":

Now that the last I do not know which stopped at the lake, it would be classified discussions chant. The stop at each lake optimal solution obtained all, to take a final optimal solution is not on line yet? We found that when we know the hero finally stopped after which the lake, her path will uniquely identify (for example, Allison finally stopped in the i-th lake, her path must be 1 - "2 -" 3- ".. .- "i), while her pure fishing time by the total idle time minus the travel time is uniquely determined. Consider a lake from which fishing 5 minutes, is equivalent to the path 1- "2-" 3-. " . . - "on a node i in the" heap "represents a mark in the lake and catch fish for five minutes, apparently greedy strategy is available here, every five minutes marked by far the largest number of fishing the lake, and the current record the answer is SUM i + = number of fish in the lake and fishing. Finally compare all SUM I (I = 1,2, ..., n-) taking the maximum output of the line.

Still do not know? Perhaps a look at the code to understand the AC:

. 1 #include <the iostream>
 2 #include <cstdio>
 . 3 #include <the cctype>
 . 4 #include <Vector>
 . 5  the using  namespace STD;
 . 6  
. 7  int ANS;
 . 8  
. 9 Vector < int > FISH, lesss, T; // each the number of number of fish reduce the number of the first 5 minutes of the lake can catch a fish, each fishing lake every five minutes over the previous five minutes of fishing, such as the meaning of the questions 
10 the Vector < int > GET , tmpfish; // from the first Lake went i-th time required Lake, Lake each current five minutes the number of fish can be caught 
. 11  
12 is  char CH;
 13 is   
14 inline int read () // fast read (also called read optimization)
15  {
 16      ANS = 0 ;
 . 17      CH = getchar ();
 18 is      the while CH = (isdigit (CH)!) Getchar ();
 . 19      the while (isdigit (CH)) = ANS (ANS << . 3 ) + (ANS << 1 ) + CH-, ' 0 ' , CH = getchar ();
 20 is      return ANS;
 21 is  }
 22 is  
23 is inline void the init () // initial process dynamic array, as desired dynamic array index starts from 1. 
24  {
 25      FISH. push_back ( 0 );
 26 is      lesss.push_back ( 0);
 27      t.push_back ( 0 );
 28      get .push_back ( 0 );
 29      get .push_back ( 0 ); // Note get array process starts from the main function is labeled 2, is required to fill a plurality 0. 
30      tmpfish.push_back ( 0 );
 31 } // Why fill 0? For the properties of common global array is the same (default defining full initialized to 0) 
32  
33 is  int main ()
 34 is  {
 35      the init ();
 36      int n-= Read (), H = Read () * 12 is ;
 37 [      for ( int = I . 1 ; I <= n-; I ++) Fish.push_back (Read ());
 38 is      for ( int I = . 1 ; I <= n-; I ++ ) lesss.push_back (Read ());
 39      for ( int I = . 1 ; I <n-; I ++ ) T. push_back (Read ());
 40      for ( int I = 2 ; I <= n-; I ++) GET .push_back ( GET [I- . 1 ] + T [I- . 1 ]);
 41 is      int Mava, Mapo, tmphours, matot = 0 ; // maximum current greedy found, greedy find current corresponding to the maximum index (ie, the number of lakes), fishing net current time, the final answer. 
42 is      for ( int I = . 1; I <= n-; I ++) tmpfish.push_back ( 0 );
 43 is      for ( int K = . 1 ; K <= n-; K ++ )
 44 is          IF (H> GET [K])
 45          {
 46 is              tmphours = H- GET [K ]; // available time fishing net 
47              for ( int I = . 1 ; I <= K; I ++) tmpfish [I] = FISH [I]; // initialize 
48              int SUM = 0 ; // when recording Allison finally stopped at the k-th answer Lake current 
49              the while (tmphours> 0 )
 50              {
51 is                  Mava = - 2000000000. ;
 52 is                  Mapo = 0 ;
 53 is                  for ( int I = . 1 ; I <= K; I ++) // greedy choice 
54 is                      IF (Mava < tmpfish [I])
 55                      {
 56 is                          Mava = tmpfish [I];
 57 is                          Mapo = I;
 58                      }
 59                  IF (Mava <= 0 ) BREAK ; // not directly exit can catch fish 
60                  SUM = +mava;
61                  if (tmpfish [mapo]> lesss [mapo]) tmpfish [mapo] - = lesss [mapo];
62                  else tmpfish [mapo] = 0 ;
63                  tmphours-- ;
64              } 
 65              if (sum> matot) matot = sum;
66          }
 67          else  break ;
68      printf ( " % d " , matot);    
69      return  0 ;
70 }

Finally, summarize greedy it:

Determine greedy strategy: When you see the problem, according to the experience of life (funny) to confirm a hunch greedy strategy guidelines. Deal with simple questions useful.

        Look at the nature and subject-related (can be derived by a mathematical formula, or some physical properties with life-related items described in the title) with basic greedy related issues will have to find some aspect of the maximum or minimum value.

Proof greedy strategy: direct mathematical derivation.

        Suppose there is a better solution, evidence to the contrary.

        Metaphysics divination

Some greedy Note: When only the best overall solution launched by local optima (not confined to the strategy) available greedy. (Otherwise use dynamic programming)

        The basic dynamic programming can be greedy do, but most greedy complexity better than dynamic programming.

Guess you like

Origin www.cnblogs.com/InductiveSorting-QYF/p/10987715.html