日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

贪心大法的几个问题

發(fā)布時(shí)間:2025/3/15 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 贪心大法的几个问题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

求最小生成樹的Prim算法和Kruskal算法都是漂亮的貪心算法。

貪心法的應(yīng)用算法有Dijkstra的單源最短路徑和Chvatal的貪心集合覆蓋啟發(fā)式 貪心算法可以與隨機(jī)化算法一起使用,具體的例子就不再多舉了。 很多的智能算法(也叫啟發(fā)式算法),本質(zhì)上就是貪心算法和隨機(jī)化算法結(jié)合。 這樣的算法結(jié)果雖然也是局部最優(yōu)解,但是比單純的貪心算法更靠近了最優(yōu)解。 例如遺傳算法,模擬退火算法。

一.把3/7和13/23分別化為三個(gè)單位分?jǐn)?shù)的和

設(shè)a、b為互質(zhì)正整數(shù),a<b 分?jǐn)?shù)a/b 可用以下的步驟分解成若干個(gè)單位分?jǐn)?shù)之和: 步驟一: 用b 除以a,得商數(shù)q1 及余數(shù)r1。(r1=b - a*q1) 步驟二:把a(bǔ)/b 記作:a/b=1/(q1+1)+(a-r1)/b(q1+1) 步驟三:重復(fù)步驟2,直到分解完畢 3/7=1/3+2/21=1/3+1/11+1/231 13/23=1/2+3/46=1/2+1/16+1/368 斐波那契的求解埃及分?jǐn)?shù)的貪心算法: 設(shè)某個(gè)真分?jǐn)?shù)的分子為a,分母為b; 把b除以a的商部分加1后的值作為埃及分?jǐn)?shù)的某一個(gè)分母c; 將a乘以c再減去b,作為新的a; 將b乘以c,得到新的b; 如果a大于1且能整除b,則最后一個(gè)分母為b/a;算法結(jié)束; 或者,如果a等于1,則,最后一個(gè)分母為b;算法結(jié)束; 否則重復(fù)上面的步驟。 二.POJ 3377:Best Cow Line, Gold?貪心+字典序 題目: 總時(shí)間限制: 10000ms 單個(gè)測試點(diǎn)時(shí)間限制: 1000ms 內(nèi)存限制: 65536kB 描述FJ is about to take his N (1 <= N <= 30,000) cows to the annual "Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (e.g., If FJ takes Bessie, Sylvia, and Dora in that order, he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order (i.e., alphabetical order) according to the string of the initials of the cows' names.FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration in this new order.Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.輸入* Line 1: A single integer: N* Lines 2..N+1: Line i+1 contains a single initial ('A'..'Z') of the cow in the ith position in the original line輸出The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the newline.樣例輸入 6 A C D B C B 樣例輸出ABCBCD View Code

示例片段:

1 int n; 2 char s[maxl+1]; 3 void solve() 4 { 5 int a=0,b=n-1; 6 while(a<=b) 7 { 8 bool left=false; 9 for(int i=0;i+a<=b;i++) 10 { 11 if(s[a+i]<s[b_i]) 12 { 13 left=true; 14 break; 15 } 16 else if(s[a+i]>s[b_i]) 17 { 18 left=false; 19 break; 20 } 21 } 22 if(left){putchar(s[a++];)} 23 else putchar(s[b--];) 24 } 25 putchar('\n'); 26 } View Code

原序取反,正反比較每次取出小的字符輸出即字典序排列,字典序比較常用得到貪心算法。

?

轉(zhuǎn)載于:https://www.cnblogs.com/dzzy/p/4711352.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的贪心大法的几个问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。