1. Trang chủ
  2. » Luận Văn - Báo Cáo

SGK BT chuyên tin quyển 2

176 12 2

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 176
Dung lượng 27,37 MB

Nội dung

I A A TAI ilEU CHUYEN TIN H0c I O \i ,l BAI TAP H0 si DAM (chn bicn) oO or-tc oOtlc - lE vlNu HoANG - ttcuvEru THANH HUNG IA CHUYTN TIN HOC TAI IITU tl \rtl BAII TAP ) -:.]:-=l QUYEN (Tdi bdn ldn tha nhiit) ruun xuAr aAn ctao ouc vtEr runvt Lor NoI nAu liau chuyan Tin hoc - Bdi hap Quydn I, 2, duo c vidt kdm v6i b0 Tdi tac'gid tham hau' chuyan Tin hoc - Quydn 1, 2, tuong rlng da duoc xudt ban cdc gia bien soan bo s6ch ld nhfrng thdy gi6o dd vd dang day b c6c trudng chuyen, l6p gi6o vion tin chon hoac.tham gia c5c kh5a bdi dudng thi tin hoc qudc te, bdi dudng muon cho c6c truong chuy6n theo chuong trinh cua BQ Gi6o duc vh Ddo tao' mong linh vuc xay drrng duoc c6c tdi liOu c6 tfnh h0 thong phuc vu t6t cdc ddi tuong thuOc BO s6ch Tdj chuyen tin hoc C6c cudn Tdi IiAu chuy,n Tin hoc - Bdi ttfrp ddu c6 cAu trfc nhu nhau, gdm hai phdn: Phdn I - Biri tap bao g6m tdtcac6Lc bhi tap ffong nhirng chuyen dd cua s6chTiii den hAu chuyAn Tin hoc tuong rlng vh c6c bdi tap bd sung, du-o c s6p xdp til de kh6, til don giin ddn Phrlc taP Phdn II - Huong dan giii bii tAp c6 thd la nhfrng huong dan chi tiet dd gifp ban hidu vd tlm doc tim du-o.c ldi giii hoac chi lh doan chuong trinh chinh girip ban doc biri tQp duoc ldi giai hoac chudng trinh.hohn chinh dd tham kh6o' Ddi vdi mOt so thi c6 the chi lir drip rin hay huong d6n ngin gon Iao Hai bo sSch Tdi liau chuyan Tin hoc vd Tdi liau chuyan Tin hoc Bdi trip chuyOn cdc thhnh hC th6ng tdi li6u kh6 hohn chinh theo dinh huong Chuong trinh cing v6i bO dd chuyen tin hoc dd duo c B0 GiSo duc vd Dho tao ban hhnh Do vAy ld tiri s(tchTdi liAu chuyAn Tin hoc, b0 s6ch Tdi tiAu chuy'n Tin hoc - Bdi tdp s6 cd chon l6p chuyen li6u thiet thuc phuc vu cho giSo vien, hoc sinh c6c trudng Trung hoc phd th6ng vh Trung hoc co so Ngohi {a, b8 sSch cdn l)t t}ri liou tham gia khao bd fch cho vioc tap hudn sinh vion ci{c trudng Dai hoc, Cao ding tham vien Quoc t€' c6c ki thi Olympic Tin hoc Sinh vien Tohn qudc vh Ki thi lAp trinh Luu j srt dung b0 sdch: c6c bdi tap b6 s6ch nhy du-o.c d6nh s6 nhu trolg siich li thuyet; c6c bhi tap bd sung duo c dd o muc neng vh d6nh so tiep theo bo Mac dD c6c tdrc gia vd Ban bi6n tap dd cd g6ng hohn thien nhunq chac ch6n g6p dd sdch cdn nhidu thidu s6t, c6c tltc gittmong nhAn du-o.c nhidu f kidn d6ng ve: gur y xin g6p s6ch s6 hodn thien hOn, phuc vu ban doc duoc hieu qua hon c6c BanTodn-Tin, c6ng ty cri phdn Dich vu xud't btin Gido duc Hd ^l6i' Nhd rud't bdn Gido duc vi€t Nam, tdng 4, toc) nhd Diamond Flower, 56' I Hodng DaoThuY, Hd Ndi' C6c tric eii ldoo # tl'tN BAI TAP DE runu ntluuu rntlu rUQl\G vA cAu rRUc ntILrEU lal- - -;t chuong trinh thgc hiQn c6c thu tqc chdn, xo6, vd tim ki6rn mQt phAn tu ::,:g danh sdch c6c s5 nguy€n da sAp x6p theo thfi'ru tang clAn bi€u di6n boi: : : : t'l \lang: Danh s6ch n6i don; Danh s6ch nOi kep j.:.' r i€t chuong trinh n6i hai danh s6ch s6 nguy€n da sAp x6p vi6t chuong trinh n6i k danh s6ch si5 nguy€n dd ,.,-r: ,J€ dugc rnQt danh s6ch g6m t6t cit citc phAn tu dugc sip x6p' l-,ng qu6t L-i- hcrn, hdy sat, ,-l.a su chung ta bi6u di6n mQt da thuc p(x) = alxbr * a2xb'+ "'+ Qnxb', b, ) b, :-.: ,lanh sfch chira hg s6 d;, s6 mfr b; vd tro tro toi nrit k6 ti€p (ntit - 1) Hiy tim thuQt to6n cQhg vd nh1n hai da thirc theo bi6u di6n ndy ::'-.ng a{ \t"1t so nhi phin anen-r a0,trong d6ai e {0,1}c6 gid tri bingfa,z' ' l=0 \guoi ta bi6u di6n s6 nhi phdn ndy bing rnot danh s6ch ndi don g6rn n :.;t c6 nirt dAu danh s6ch chua gi6 tfi Qn, m6i ntit danh s6ch chira : \r cht s6 nhi phdn a; vd tro tro t6i nrit :hin ki5 as-1 :iir lqp chuong trinh thuc hiQn phdp todn "c6ng ' r rJua bi6u di6n nhi phdn ctra k€t qua ,j,,ri tiep Id nirt chua chir sd nhi -t1.' Su' dqng dg quy l" tr€n s6 nhi phdn dd cho 6.5 Hdng dqi hai ddu (doubled-ended queue) ld m6t danh silch duoc trang.bi bdn thao t6c: PushF (u): DAy phAn tir y vdo dAu danh s6ch PushR(v): E6y phAn tu u vdo cudi danh s6ch PopF: Lo4i bo phAn tu dAu danh sdch PopR: Loai bo phAn tu cu6i danh s6ch H6y tim cAu truc dqi hai dAu 6.6, df liQu thich hqp d6 cdi dflt ki6u dfr ligu truu tuong heng C6 hai so d6 duong ray xe lta b6 tri nhu hinh sau: Ban dAu co ntoa tdu x€p theo thg ru tt I tdi n tu phdi qua trdi tr€n clucrng ray A Ngucri ta mu6n x€p lai c6c toa tdu theo thri tu m6i tu phii qua rr6i (pr,pr, ,?n) l6n'duong ray C theo nguyOri tdc: Ctc toa tiu kh6ng dugc "vugt nhau" trdn ray, rn6i lAn chi dugc chuy6n mQt toa tiu tu A B, B+Cho{c4 +C Hdy cho uict aieu d6 c6 th6 thuc hiqn dugc trcn so d6 dudng ray ndo hai so dO tren? 6.7 xdt hai nft x, y tr€n mQt cdy nhi phdn, y nim bOn phdi nrit x) n6u: o o n6i nrit x nim b6n tr6i nrit y (nrit Hodc nrit x ndm nh6nh tr6i cria nut y; Ho{c nrit y nim nh6nh phAi cira nrit x; o 3bi HoAc t6n t4i rnQt nirt z cho x nh6nh Phii cua ntt z' nim nh6nh trai v2r y nim cdy nhi ph6n (x + y) chi c6 Hdy chi ring v6i hai nut x,y uat ki tr€n mot tlung mQt bOn menh dd sau ld dung: ' i) ii) iii) iv) tng aJ x nim b€n tr6i Y; x nim b€n Phai Y; x ld ti€n b6i ttrgc su cua Y; cua x' Y ld ti€n UOi ttrUc su T, gia su ring ta bitit duqc c6c gi6 tri truoc' preord.erlxf ,lnord"er[x] vd Postorderlx] lan lugt ld thu tu duyQt gita, sau cua x hai nrit c6 quan hQ tiOn Tim c6ch chi dua viro c6c gri tr1 ndy dO ki6m tra V6i m6i nut x tr6n cdy nhi phdn UOi-nau duQ haY kh6ng ,ei BQc (degree) nhi phdn, rdng cia mot nirt ld s6 nirt cua no Chung minh sO ta nni6u hon sO trOn cay nirt b4c dung m6t nirt' nhi ph6n c6 ttr6 kh6i phgc mQt c6ch 6.10 Hdy chi ring cau truc cua mQt ciy AonAintneutabi€tduqcthutgduyQttru6cvirgiiracuac6cnirt' phuc n6u ta bi6t duqc thil tg Tucrng tg nhu vQy, c6u truc cdy co th6 kh6i duYQt sau vd gita cua c6c nirt' 6.lr.Haychoviduv€haiciynhiph6nkh6cnhaunhungc6thritgtluoccua gi6ng nhau' gi6ng vir thir tU sau cfia c6c nut cfrng "e"nut chirig han bi€u thuc bao g6m c, 6.12 X6t bi6u thric c6 th6 co d4ng phirc tap, phepl6ys6A6l(-x),ph6ptinhlulthria(xv)'hdmsovoimQthaynhi6u bi0n s6 ndy bing mqt caV t1n9 qudt vh Ta c6 tnti UiCu diSn nhirng bi6u thuc d4ng hay ki ph6p nghich dAo Ba tir d6 c6 the chuyi5n bi6u thuc v6 d4"g hOu t0 Lan (RPN) dC ttruc hiQn tinh to6n' s6 hQc (dang phuc tqp) vd Hdy,x6y dgng thudt to6n d€ chuy6n bi6u thuc thuc d6' d4ng RPN va thuflt to6n tinh gi6 tri biOu logic d?ng trung t6 sang d4ng RPN' 6.13 vict chuong trinh chuy6n bi6u thuc d andor' Vi du chuy6n: a and b or cand d thdnh: ab andc 6.14 Chuy6n ciic bi€u thric sau ddy dang RpN: a) Ax (B + C); 't\ B b) A*;* C c) Ax D ; (B + C); D d) A-(B+c)E; e) (A or B)and (C or (Dor not E)); (A-B)or(C=D) '6.15 vdi mqt dnh den/tring kich thudc 2n xzn, nguoi dung phuong ph6p sau d€ md hoil rinh: r NOu Anh chi g6m todn di6m den thi rinh d6 c6 th6 duoc md hori bing 'xdu chi g6m mQt ki tu B,; N6u anh chi gdrn rodn di6rn tring thi inh dri c6 th€ ducyc m6 ho6 bing I xiiu chi gom mdt ki tu ,W,; N6u P,Q,R,s ldn luqt ld x6u md ho6 cta b6n rinh kich thu6c bins thi &PQR.9 lir x6u md horr cua dnh tao thdnh bing cdch aar uol dnh ban dAu theo so d6: Pa 9R Vi.dU "&B&BWWB&W&BWBW,' vd "&&BBBB&BWWB&W&BWBW" ld hai xdu md hori cua cung mQt rinh b6n" Bdi torln drt ld cho s6 nguy€n ducyng n vd hai xdu md ho6 cira hai 6nh kich thuoc 2n x zn Hdy cho bi€t hai anh d6 c6 kh6c kh6ng vd neu chring kh6c thi chi mQt vi tri c6 mdu kh6c rr6n hai 6nh 6.16 Qu6 irinh ti* ki6m ft6n cdy rthi ph6n tim ki6m (BST) c6 th6 coi nhu mQt ducrng di xu6t ph6t tu nrit gdc Gi6o su X ph6t hi€n m6t tinh ch6t thir vi: N€u duong cli qu6 trinh tim ki6m kdt thric o mQt nrit ld, ki hieu t ld 6p c|c gi| tri chua rrong c6c nrit nim b€n tr6i ducng di vd R ld tflp c6c gi(t tri chria c6c nrit nim b6n ph6i duong di Khi dovx l,,y e R, ta c6 F x < y Hdy chring rninh ph6t hiQn cira gi6o su X ld dring hoflc chi mQt phrin vl dq uLr Prl
    reari Resul-t :: Faf se; endi end,: procedure var -FntanAr F\all-r .i v, Augment.Fl-ow ; Tnf6^6r , begin v ::' n,' h^l +uerLd - h^ t[a^v, rePeat i :: trace [v] ; if elil.c - elil.f < Delta then De1ta :: elil.c - elil.f; v :: elil;x; until.v = 1; v | - ll, rePeat i :: trace [v] ; < etil.c) then Inc (e Ii] , De1ta) ; [-i] f, Detta) ; v := e lil xl unlil v = 1; Fl-owValue :: Fl-owValue + Del-ta; end; procedure PiintResult, f Dec (e , i: Integer; f o : TextFil-e; ,begin AssignFile (fo, OutputFiJ-e) Rewrite (fo) ; ; Ery Writel,n (fo, Fl_owValue.) ; fori:=ltomdo with elil if, (tnacelxl = 0) xor (traceIy] : 0) then Write (fo, i, ' ') ; fin'al1y : CLoseFife (fo) end; ; end,' begin Enter: t Init.ZeroFlow; while Findpath AugmentFlow; ' PrintResult; end :1.49 Xdy dwng thuqt todn vd cdi ddt chuong,trinh Ddy le bei to6n ngdoi dua thu Trung Hoa trucrng hqp d6 thi c6 huong (Directed Chinese postman problem) Gqi X ln tf,p dinh "thira": Nhirng dinh c6 s6 cung di vdo nhieu hcrn s6 cung di'ra, mfc thria cia mQt dinh u ld deg-( u) deg+ (v) - Gqi I/ ld tpp dinh "thi6u": Nhirng dinh c6 s6 cung di nhiAu hon sti cung \ di vdo, muc thi6u cria mQt dinh u ld deg+(u) deg*(u) 168 Tim m6t bQ duong di fu X sang I' thoa r,ndn: -:"r M6ijduong'cli xudt ph6t tu mQt dinh thua vd k6tthric tpi mQt din[ thi6u; M6i dinh thua c6 sti ducrng di xu6t ph6t tu n6 dring bing mric thua; vt6i dinh thi6i c6 s6 duong di ktit thfc tai n6 dring bing mric thi6u; -l TOng d0 ddi c6c dudng di ld ngdn nhat c6 th6, DC tim bQ duong di ndy c6 thO su dgng thuat todn ghdp cflp c6 trgng s6 ho[c luOng chi phi cgc ti€u Chwong rrinh {$MODE OBJFPC} Di rectedChrnesePostman program ; consE InputFile = 'DCPP trNP' ; OutputFile = ' DCPP OUT' ; maxN = Round(1E3); maxM = Round (1E4 ) ; maxW = Round(1E5); m:xD=maxN*maxW, ^ *:' " tyPe TEdge = reqord u/ v: Integeir; c, f , w: Integer,' - link: Integer,' end; ' Tlleap = recor{ ' items, pos: array[1 maxN] of Integer; nftems: fnteger; end,' var fi, LL I fo : TextFife r n f a^a ; r e: array[-maxM maxM] of TEdge; head: array[1 .maxN] o.f 'f nteger,' p: 'array [1 maxN] 'of Integer; d: arl?y[1 maxN] of Integer; excess: array[1 .maxN] of Integer; 169 trace: array[ maxN] of Ihteger; z array[1 maxN] of Integer; Tnl-a^6r heap: THeap; res: Int64 i procedure Enter,' var i ' Tn F6-ar rl.LvYvr begin (fi, n, m) ; {or.i::1to.mdo Readln(fi, eIi].u, for i :: l- to m Readln elil.w); elil.v, begin e[-i].u :: e[1].v; eI-il.v := elil.u; t elil.c := mi e[-i].c := 0; etil.f := 0; :: 0; e[-i].f := -etil.w; e[-i].w ' 'end; Fif l-DWo::d(head[1], n, 0) i ',f,or i :: -m tg m ifi0then with elil begin ' Iink := headlul,' headlul := i; end; endi function cf(const e: TEdge): Integer; inlinei begin with e Resuft := c - f;: end,' function wp(const e: TEdge): Integer; inline; begin with e Result := w + FluJ - ptvl t70 i endi procedure tnit; : r ver : ' i :' rntegey';' l 'i beain Fil1DWor$(excess[1], n, for + :=.1 to m , with elil O); ' Deg:.n : '\ Ded(excess [u] ) ; r_nc (excess lvl ); end, r FillDWord (p t lr, n, ) ;' end; ^ bi1 kstrarnit ( stqrt : :;fr9""" FiIlDWord(d.[1], n; maxD) ii.tstartl : = 01 with - heap, '' FiIlDWord (bos [1 J ,, n, nlte$s := Li Oegin ; Tnl-aaar\ : ' t '., ), ; I Pos[startl ':! 1; endt en6; :1 fuocirion Extract: Y+r r Tnl-aaar rrrevYv! t l i t Tnl-ariar, gvYv!, temp: ': fnteqer; beaia -iith,heap 't^ , begin Result :1 items '' l [L]l ; temp := ,items Inltems], Dec-( nltems) t r = Lt1 r€peat { c t: i *.2; I n (c < nltem5) and (dlitems Ic]l > dlitems Ic + then I.nc (c) i if ].1I ) : Break; items [.r] :: items Ic] pos Iitems IrJ ] 2: r; ! - ,' r U, until False; itemsIr] i= tempq Poslt.ernPl := r; glnd; end,' function Update (v, dv: Integer) : Boolean; var r Lt i Tnl_6d6r , begin ? Result := dlvl > dv; if not Result then Exit; otvl := crv; with heap begin ifc=0then begin Inc (nltems ) , c := nltems; : end; rePeat r r :: c d,Lv 2; if (r : 0) or, (dlitems trl I o).and UPdate(v,dluPoPl + wP(elil)) then trace[v] := i; i := ]ink; 'end; until False; / /Uodate price function for'1 := 1'to nz rnc (p tz Iil L dlz til I.- dlResultl.) ; end,' procedure AugmentFf var ow ( s', t : Integer:) ; L, -v, De]ta: Integet; begin if excessIs] < -excessIt] then Del-ta :: excessIs] else Delta :: -excessIt]; v := t; repead i := trace [v] ; t73 rnn /a f i l !tr \v LrJ Delta) ; Dec(e.[-i] f, Delt.a) ; v := elil.u; until v = s; Dec (excess Is], Del-ta),' Inc (excess It] , Detta) ; eod; , , , procedure Succes s iveShortes tPaths,' var sr L, ii Integer; begin for s :: to n while excessls] > begin t Di j kstra (s )-; AugmentFlow (s, t) ; : = end; rac = n.' for i :: to m res :: res + e[i].w; fori:=-mtomdo if (i 0) and (etil.f > 0) then res :: res + Int64 (e til f) * e Ii] w; end; begin AssignFile (fi, Le ; ( Reset (fi) ; ASS]-gnr']1e Par^rrifa/fn\ I InputFile) (ro, uu.EpuEf rte) ; ( t \!v/ try !fk hfr uv!^P t Init; SuccessiveShortest.Pat.hs; Write (f o, res ) ,' finally 1-lneoE'i'la/fi.); f f v \!4/ CloseFil-e (f o) end; end ' t74 t ; , ' MUCLUC • • Loi n6i d�u BAI T�P J Chuyen d� KIEU DU LIBU TRl U TVQNG v A cAu TRUC DU' uBu Bai t�p chuyeu-d� Bai t�p b6 ·sung 14 Chuyen d� DO THl Bai t�p chuyen d� 23 Bai t�p b6 sung_ 37 HU'O'NG DAN - GIAI BAI T�p Chuyen d� 44 ChuY,en d� 116 175 vuoNc M|FN KrM GUdNC cH{r ruoNs auocrd Gtfi rHtEu BQ sAcH TATLTFU CHUYEN TrN HgC DANH CHo HgC S|NH pHo THoNG BVn dlc co thd mua sdch tqi cdc Cong ty Sdch - Thi6t b! trudng hgc cdc hodc cthc cta hdng sdtch crta Nhd xudt bAn Gi6o dyc ViQt Nam : - T4i TP Hi N0i : - Tai TP 0d Ning - - T4i phndng 45 Phd Vqng ;187 ,187C Giing VO ;232fAy Son ; 25 Hin Thuy0n; 51 Lo D0c ; 45 Hdng Chudi ; NgO 385 Hodng Oudc Vi0t; : TaiTP ttd Ctri lvtintr : -TaiTP CO'nTho Ctia : 17 T2 - 17T3 Trung Hda - Nh0n Chinh ; Tda nhd HESCO Vin 0u6n - Hi D0ng 78 Pasteur ; 45 L0 Ldi ;223 L0 Dinh Lf 261 C LC Ouang Dinh, 0u0n Binh Thanh ; 231 Nguy6n Vin Cil, 0u0n b ; 23 Dinh Tien Hoing, Phtldng Da Kao, 0u0n , TP Hd ChiMinh 162DDudng3 thAng2,PhudngXudn Kh6nh, 0u0n Ninh Kidu Website b6n hing trqc tuyOn : www.sach24.vn lsBN 978-604-0-082 I 6-9 www.nxbgd.vn ,ll[[Jil[tillluulil Giti:31.000d ... th6 tinh dugc thoi gian O(lEl) Gq'i y;Chf i ring c6c trgng sd w{e) e {0,1}' 27 c) Hdy chi ring v6i moi i= 2, 3, ,2 wrG) = 2. wr-r(e) hoflc wr@),= 2. wi-r(e) + f Tt 7.1 d6 chring minh ring: 2. 6i-r(s,... hdnh trinh tirn duoc Vi du: DCPP.INP DCPP,OUT 69 L44 24 2 28 3r2 326 341 4 52 463 533 631 Gidi thich; Hdnh trinh cAn tim ld I -+ -+ 6-+3 - +2 -+ -> -+3 -+ -+ -+3 -+ I nen thay r.,.' 4Ieu dia ,'l... tQp Input va Output dwqc ghi cach ft nhd:t m ddu cach Vf du: SEAPORTS.INP 25 03 35 o2 25 14 SEAPORTS.OUT LL22O 6. 12 Thdng Bdm vd Phir Ong rg la ii rc o u a Bom thing Phri Ong mQt cuQc tl6nh

Ngày đăng: 13/10/2021, 08:47

TỪ KHÓA LIÊN QUAN

w