Greedy algorithm of basic algorithm

Source: Internet
Author: User

See Rujia Daniel's Black book really have experience, although difficult, but really the title of classic, must insist on sitting down, below we say greedy law

Greedy algorithm is to choose the local optimal strategy to implement each time, and not to consider the impact on the future. Greedy algorithm is not to all problems can get the overall optimal solution, the key is the choice of greedy strategy, the choice of greedy strategy must have no effect, that a state of the previous process will not affect the future state, only related to the current state.

Let's look at a topic:

POJ1042 Fishing (Black book)

Links: http://poj.org/problem?id=1042

Greed: In order not to discuss the time spent on the road, can be enumerated to the lake: for example: A=j said to Lake 1, 2 、..、 J corresponding, spend time on the road
* is t[1]+t[2]+. +t[j-1] (obviously only one trip per paragraph) so it's time to figure out how long it takes to go fishing, and it's no good to walk back, so we don't choose to go back.
* In this way, it is different to consider the time on the road, you can think of John has an instant move, which lake fish more, to which lake fishing (of course, the number of lake to meet 1 <= ~ <=a)

We enumerate in turn how many lakes to fish in the first place, then we can first calculate the time on the road, and then people can be in between these lakes instantaneous transfer, each pick a hook rate the highest lake to catch.

The code is Kinghai Daniel, who learns the new code style and will continue to do so later.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5#include <cmath>6#include <cctype>7#include <algorithm>8#include <vector>9#include <map>Ten#include <stack> One using namespacestd; A Const intmaxn= -; - intF[MAXN],D[MAXN],T[MAXN],ANS[MAXN]; - intn,h,ansf; the BOOLp=false; -  - voidInit ()//input part, with function better - { +Cin>>h; -h*= A; +ansf=-1; A      for(intI=1; i<=n;i++) atCin>>F[i]; -      for(intI=1; i<=n;i++) -Cin>>D[i]; -      for(intI=1; i<n;i++) -Cin>>T[i]; - } in  - voidMakeintAintTime ) to { +     intLs[maxn],ans1[maxn],mx,mxi,fish=0; -memset (ANS1,0,sizeof(ans1)); the      for(intI=1; i<=a;i++) *ls[i]=F[i]; $      while(time>0)//The greedy method is used here to find the optimal scheme.Panax Notoginseng     { -mx=-1; themxi=-1; +          for(intI=1; i<=a;i++) A             if(mx<Ls[i]) the         { +mx=Ls[i]; -Mxi=i; $         } $fish+=MX; -ans1[mxi]+=1; -ls[mxi]=0&GT;LS[MXI]-D[MXI]?0: ls[mxi]-D[mxi]; thetime--; -     }Wuyi     if(fish<ANSF) the         return; -     if(fish>ANSF) Wu     { -          for(intI=1; i<=n;i++) Aboutans[i]=Ans1[i]; $ansf=Fish; -         return; -     } -     intj=1; A      while(j<=n&&ans[j]==Ans1[j]) +J + +; the     if(ans1[j]>Ans[j]) -          for(intI=1; i<=n;i++) $ans[i]=Ans1[i]; the } the voidWork () the { the     intTime=0; -t[0]=0; in      for(intI=1; i<=n;i++)//"instantaneous transfer" classic approach the     { thetime+=t[i-1]; AboutMake (i,h-Time ); the     } the } the  + voidPrint ()//output part, with function better - { the     if(P)Bayicout<<Endl; the          for(intI=1; i<n;i++) thecout<<ans[i]*5<<", "; -cout<<ans[n]*5<<endl<<"Number of fish expected:"<<ansf<<Endl; -p=true; the } the intMain () the { the      while(cin>>N) -     { the         if(n==0) the              Break; the init ();94 Work (); the print (); the     } the     return 0;98}
View Code

Greedy algorithm of basic algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.