Quy hoạch động trạng thái

  • Published on
    25-Sep-2015

  • View
    45

  • Download
    0

Embed Size (px)

DESCRIPTION

quy hoch ng trng thi

Transcript

<ul><li><p>[TI LIU CHUYN TIN] October 21, 2013</p><p>V Vn Tr - CQB | Confidential</p><p>Chuyn . QUY HOCH NG TRNG THIa s cc bi quy hoch ng u da vo trng thi. Tuy nhin c trng thi d pht hin, c trngthi kh pht hin, c bi ton s lng trng thi t, c bi ton s lng trng thi rt nhiu. V vy,khi ni n quy hoch ng trng thi ta thng ngh ngay rng y l cc bi ton tng i phctp, c khng gian trng thi ln m ta cn phi s dng k thut m ha bng dy bit nh phn. Sauy l mt s bi ton c th gii quyt trit bng phng php quy hoch ng trng thi.1. Chn - SelectCho ma trn vung a kch thc nn (1n20). Cc hng c nh s t trn xung di bt u t1, cc ct c nh s t phi sang tri bt u t 1. nm giao ca hng i v ct j c ta [i, j].Trn mi a[i, j] c cha mt s nguyn.Yu cu: Hy chn trn ma trn n sao cho- Mi hng c nhiu nht mt c chn;- Mi ct c nhiu nht mt c chn;- Tng gi tr ca cc c chn l ln nht.Input: cho trong tp vn bn SELECT.INP:- Dng 1: ghi s nguyn dng n;- N dng tip theo, mi dng ghi n s nguyn dng khng vt qu 109 th hin dng th i cama trn.Output: ghi ra tp vn bn SELECT.INP trn mt dng l tng ln nht tm c.V d:</p><p>Input Ouput33 1 21 1 21 4 2</p><p>9</p><p>Gii: Quy hoch ng trng thi- Gi S l trng thi chn ct, nh vy S l mt dy n bit, mi bit c gi tr 0 hoc 1. Cc bit cnh s t phi sang bt u t 0 (nh s t bit thp n bit cao). ngha nh sau:+ bit th i ca trng thi S = 0: ct i+1 cha c chn;+ bit th i ca trng thi S = 1: ct i+1 c chn;(0i</p></li><li><p>[TI LIU CHUYN TIN] October 21, 2013</p><p>V Vn Tr - CQB | Confidential</p><p>12345- Gi T[S] l gi tr tt nht khi chn cc k dng u tin vi trng thi S (k l s bit 1: s ctc chn ca trng thi S).- V d, khi ta tnh T[{1, 2, 5} = 10011 = 19] th c th:+ T[{1, 2, 5}] = T[{1, 2} = 00011 = 3] + a[3, 5]+ T[{1, 2, 5}] = T[{1, 5} = 10001 = 17] + a[3, 2]+ T[{1, 2, 5}] = T[{2, 5} = 10010 = 18] + a[3, 1]C ngha l T[{10011}=19] l gi tr tt nht khi chn 3 3 dng, 3 ct u tin s bngmax(tng cch chn 2 hai dng, 2 ct u tin vi mt dng th 3). V l gi tr ln nhtnn ta phi ly max.- Tng qut ta c cng thc quy hoch ng nh sau:T[S] = max (T[P] + a[k, j])Trong :+ S l trng thi gm c k ct c chn+ P l trng thi bn sao ca S nhng ch khuyt ct j (gm c k 1 ct c chn, ringct th j trng thi S c gi tr 1, trng thi P c gi tr 0)- Khi lp trnh, ta phi thc hin hai thao tc x l bit:+ Tt bit th j ca trng thi S, ta s c trng thi lin trc preS ca Sfunction TurnOff(state:long; j:byte):long;</p><p>begin</p><p>TurnOff:=state and not (1 shl j);</p><p>end;+ Ly bit th j ca trng thi S, thao tc ny c s dng gii m trng thi (hin th ccbit 1 ca trng thi)function getBit(state:long; j:byte):byte;</p><p>begin</p><p>getBit:= (state shr j) and 1;</p><p>end;- Thao tc quy hoch ng c thc hin bnh thng: tnh gi tr ca trng thi sau thng quacc trng thi k trc n:</p></li><li><p>[TI LIU CHUYN TIN] October 21, 2013</p><p>V Vn Tr - CQB | Confidential</p><p>function getMax(state:long):long;</p><p>var j,k : byte; preState,max : long;</p><p>b : array[1..nm] of byte;</p><p>begin</p><p>{giai ma trang thai luu vao mang b}</p><p>fillchar(b,sizeof(b),0); k:=0;</p><p>for j:=1 to n do</p><p>if getBit(state,j-1)=1 then</p><p>begin</p><p>inc(k);</p><p>b[k]:=j;</p><p>end;</p><p>{Tim max cua cac trang thai lien truoc state + a[k,j] voi j</p><p>la bit khac duy nhat giua preState voi state}</p><p>max:=0;</p><p>for j:=1 to k do</p><p>begin</p><p>preState:=TurnOff(state,b[j]-1);</p><p>if max</p></li><li><p>[TI LIU CHUYN TIN] October 21, 2013</p><p>V Vn Tr - CQB | Confidential</p><p>- Gm 1 dng duy nht ghi chi ph b nht tm cExample</p><p>Input Output60 1 2 1 3 45 0 3 2 3 44 1 0 2 1 24 2 5 0 4 32 5 3 5 0 25 4 3 3 1 0</p><p>8</p><p>Gii: Quy hoch ng trng thi</p><p>- Gi S l trng thi th hin hnh trnh du lch ca Sherry, nh vy S l dy gm n bit vi ngha:+ S[i] = 0: thnh ph i+1 cha c thm;+ S[i] = 1: thnh ph i+1 c thm.Cc bit c nh s t 0..n-1, t phi sang tri (bt u t bit thp nht).- V d, trng thi S = 10101 = 21 th hin Sherry i qua 3 thnh ph 1, 3, 5 (ta chacn quan tm n th t thm lc ny)- Gi T[i, S] l chi ph nh nht n thnh ph i vi trng thi S, ta c cng thc quyhoch ng nh sau: T[i, S] = min(T[j,P] + C[j, i])- Vi P l trng thi lin trc ca trng thi S, l bn sao ca S, ch khc bit i 1 (thnhph i cha c thm); j l v tr ca cc bit 1 trong trng thi P (cc thnh ph cthm trng thi P).- Khi ci t, ta phi thc hin hai thao tc x l bit</p><p>function getBit(state:long; i:byte):byte;</p><p>begin</p><p>getBit := (state shr i) and 1;</p><p>end;</p><p>function TurnOff(state:long; j:byte):long;</p><p>begin</p><p>TurnOff := state and not (1 shl j);</p><p>end;</p><p>- Thao tc quy hoch ng c thc hin bnh thngprocedure DPBitmask;</p><p>var</p><p>u,i,j,k,state,preState,first,last : long;</p></li><li><p>[TI LIU CHUYN TIN] October 21, 2013</p><p>V Vn Tr - CQB | Confidential</p><p>b : array[1..nm] of byte;</p><p>begin</p><p>first:=1; last:= 1 shl n -1;</p><p>for u:=1 to n do</p><p>for state:=0 to last do T[u,state]:=oo;</p><p>for u:=1 to n do T[u,1 shl (u-1)]:=0;</p><p>for state:=first to last do</p><p>begin</p><p>fillchar(b,sizeof(b),0);</p><p>k:=0;</p><p>{giai ma trang thai state}</p><p>for i:=1 to n do</p><p>if getBit(state,i-1)=1 then</p><p>begin</p><p>inc(k);</p><p>b[k]:=i;</p><p>end;</p><p>for i:=1 to k do</p><p>begin</p><p>u:=b[i];</p><p>preState:=TurnOff(state,b[i]-1);</p><p>for j:=1 to k do</p><p>if (ij) and</p><p>(T[b[j],preState]+c[b[j],u]</p></li><li><p>[TI LIU CHUYN TIN] October 21, 2013</p><p>V Vn Tr - CQB | Confidential</p><p>V d: Xt bng vi n=3 trong hnh v di y:</p><p>Cch chn cn tm l tp cc S = {(3,1), (1,2), (4,2), (3,3)} vi trng lng 32.Input- Dng u tin cha s nguyn dng n l s ct ca bng.- Ct th j trong s n ct tip theo cha 4 s nguyn a 1j, a2j, a3j, a4j, hai s lin tip cch nhau tnht mt du cch, l 4 s trn ct j ca bng.Output- Gm 1 dng duy nht l trng lng ca c ch chn tm c.Example</p><p>Input Output3-1 9 3-4 5 -67 8 99 7 2</p><p>32</p><p>Hn ch: Trong tt c cc test: n 10000, |a ij| 30000. C 50% s lng test vi n 1000.Gii:- Theo bi th bng c 4 dng v n ct;- Gi S l trng thi chn cc ct th j, ta c th biu din S bng 4 bit (cc bit cnh s t phi sang bt u bng 0) vi ngha:+ S[i-1] = 0: dng th i ca ct j khng c chn;+ S[i-1] =1: dng th i ca ct j c chn.- Vi 4 bit, S c th biu din 16 trng thi t {0000} n {1111} (t 0 n 15), tuy nhinta nhn thy ch c 8 trng thi sau l tha yu cu ca bi ton: {0000}, {0001}, {0010},{0100}, {1000}, {1001}, {0101}, {1010} (tng ng vi cc gi tr 0, 1, 2, 4, 5, 8, 9, 10).</p></li><li><p>[TI LIU CHUYN TIN] October 21, 2013</p><p>V Vn Tr - CQB | Confidential</p><p>- Gi T[S, j] l trng lng ln nht khi chn cc n ct th j vi trng thi chn l S, tac cng thc quy hoch ng nh sau:T[S, j] = max(T[P, j-1] + value(S))vi P l trng thi ca ct lin trc ca S sao cho P v S khng c 2 bit 1 ng thi cng v tr, cn value (S) l gi tr cch chn ct j vi trng thi S.- Khi ci t, vi bi ton ny, ta ch cn xy dng hm getBit gii m trng thi S:- Cn thao tc quy hoch ng c thc hin bnh thngprocedure DPBitmask;var i,j,k:int; x:long; state:byte;begin</p><p>for i:=1 to 8 do T[i,0]:=0;for j:=1 to n dofor i:=1 to 8 dobegin</p><p>state:=S[i];x:=value(state,j);T[i,j]:=x;for k:=1 to 8 do</p><p>if (state and S[k] = 0) and (T[k,j-1]+x&gt;T[i,j]) thenT[i,j]:=T[k,j-1]+x;</p><p>end;KetQua:=T[1,n];for i:=2 to 8 do</p><p>if T[i,n]&gt;KetQua then KetQua:=T[i,n];end;</p><p>LUYN TP</p><p>1. Tr chi trn ma trn - M bi: QBGAMENgy nay cc nh khoa hc ngh ra 1 tr chi trn ma trn rt th v. Thng qua c th o IQ mtcch kh hiu qu. Tr chi c m t nh sau:Bn c 1 ma trn A kch thc 8 x N trn gm cc s nguyn l im ca cc . Ngi ta s yucu bn chn 1 tp khc rng cc trn ma trn ny sau tnh tng im trn nhng ny. Trongnhng c chn khng c hai no k cnh. IQ ca ngi chi s t l thun vi s im nhnc. Sherry tham gia tr chi v t kt qu kh tt.V by gi Sherry mun bit tng im ln nhtnhn c trong tr chi ny l bao nhiu. Bn hy gip sherry nh !!!Input- Dng 1 l s nguyn N ( 1 </p></li><li><p>[TI LIU CHUYN TIN] October 21, 2013</p><p>V Vn Tr - CQB | Confidential</p><p>- Gm 1 dng duy nht l s im ln nht tm cExample</p><p>Input Output2-22 2-33 4556 -60-8 -3879 66-10 -2399 461 -55</p><p>279</p><p>Gii thch: Chn cc (3,1) (5,1) (7,1) (2,2)2. C gi chn b - M bi: COWGIRLTrn mt tho nguyn nh b c 1 gia nh gm 3 anh em: 2 ngi anh trai l Nvutri v Andorea cnngi em gi l Lola. Cuc sng gia nh kh gi nhng gia nh c truyn thng chn nui v mun cc con t lp nn cha m 3 ngi quyt nh cc con hng ngy s i chn 1 s b no (ty 3ngi con).Tho nguyn l 1 cnh ng chia lm M*N vung, mi con b ch ng trong 1 v mi ch cha 1con b.Ch c 1 quy tc duy nht l khng bao gi c 4 con b to thnh 1 hnh vung 2*2 hoc trng 1 khu t 2*2.Hai ngi anh mi chi nn hi l kem Lola chn b 1 mnh. Lola mun bit tt c c bao nhiucch xp b tha mn quy tc trn phng mi trng hp. V con s ny rt ln nn hy gipLola tnh ton con s ny.Input</p><p>- Dng u gm 1 s T duy nht l s test (T 111)- T dng tip gm 2 s M, N cho bit kch thc ca tho nguyn (M*N 30)</p><p>Output</p><p>- Gm T dng, mi dng ng vi 1 test l s cch xp b ca test .Example</p><p>Input Output11 1</p><p>2</p><p>3. n b hn lon M bi: MIXUP2Mi trong N c b (4 </p></li><li><p>[TI LIU CHUYN TIN] October 21, 2013</p><p>V Vn Tr - CQB | Confidential</p><p>Cc c b giang h ny thch ni lon nn ng xp hng ch vt sa theo mt th t gi c gi l'hn lon'.Mt th t b l 'hn lon' nu trong dy s seri to bi hng b, hai s lin tip khc bit nhau nhiuhn K (1 </p></li><li><p>[TI LIU CHUYN TIN] October 21, 2013</p><p>V Vn Tr - CQB | Confidential</p><p>5. K lc DOMINO (Ngun bi: Thy c ng)Mt k lc th gii v xp domino c ghi nhn vo hm 17/11/2006. K lc ny thuc v HLan khi 4.079.381 qun domino ln lt xung theo phn ng dy chuyn trong ting v tay reoh ca cc c ng vin. Nhng ngi t chc s kin Ngy Domino H Lan cho bit, 4.079.381 qundomino ln lt xung trong vng 2 gi ng h.Nhng qun domino di ng uyn chuyn trn nn nhng iu nhc c in v ng i l ntc bit nht ca mn trnh din domino. Tc gi Robin Paul Weijers ni: Hn 4 triu qun domino,iu ny cha bao gi xy ra. Chng ti cn thnh cng trong vic khin cho nhng qun bi dominonhy ma trong ting nhc. Ti rt hnh phc v thnh cng.Vi mn trnh din tuyt vi ny, nhng k lc gia domino H Lan ph v k lc ca chnh h lpc nm 2005 vi 4.002.136 qun bi domino.Sp ti, Bm d nh xy dng mt cng trnh ln hn ph k lc ca ngi H Lan. Cng trnh sbao gm 2 cng on chnh:- Cng on 1: Xp qun domino vo cc cn trng trn hnh ch nht kch thc ( , 16), trong hnh ch nht c c t trc vt trang tr.- Cng on 2: Xp qun domino thnh mt dy di ( 106), mi hng c ng( 8) qun (c th c hiu nh xp vo hnh ch nht kch thc ).im c o trong cng trnh ny l s phi mu gia cc qun domino ln cn chung cnh. Cc qundomino c xp bng hai loi domino, loi 1 c mu xanh nht v loi 2 c mu xanh m. Qundomino v tr ( , ) s phi tha mn iu kin: nu + l th mu qun domino ny s phi c mukhng nht hn cc qun cc chung cnh (nu c), nu + chn th mu qun domino ny s phic mu khng m hn cc qun cc chung cnh (nu c). c nhng thng tin th v khi gii thiu v cng trnh, Bm mun bit s lng cch xp khc nhauca cng on 1 v cng on 2. Hai cch xp c gi l khc nhau nu khi chng kht 2 cch lnnhau (khng xoay hoc lt) c t nht mt qun khc mu.</p><p>D liu vo trong file DOMINO.INP c dng:</p><p>- Dng 1: gm 1 s nguyn dng ( 109), cc kt qu tm c s mod cho ;- Dng 2: bt u l 3 s nguyn dng , , ( , 16; &lt; ), trong , l kchthc hnh ch nht trong cng on 1, l s lng trong hnh ch nht t vt trang tr,tip theo l cp s, cp s , l ta t vt trang tr;- Dng 3: gm 2 s nguyn dng , ( 8; 106) l kch thc hnh ca cng on 2.</p><p>Kt qu ra file DOMINO.OUT c dng:</p><p>- Dng 1: s cch xp cng on 1 khc nhau mod ;- Dng 2: s cch xp cng on 2 khc nhau mod .</p><p>DOMINO.INP DOMINO.OUT10005 5 1 3 33 10000</p><p>240593</p>5. K lc DOMINO (Ngun bi: Thy c ng)</li></ul>