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
2
(Tdi bdn ldn tha nhiit)
ruun
xuAr aAn ctao ouc vtEr
runvt
Lor NoI nAu
liau chuyan Tin hoc - Bdi hap Quydn I, 2, 3 duo. c vidt kdm v6i b0 Tdi
tac'gid tham
hau' chuyan Tin hoc - Quydn 1, 2, 3 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 khi srt dung b0 sdch: c6c bdi tap trong 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 6. 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 con tro tro toi nrit k6 ti€p (ntit
. - 1). Hiy tim thuQt to6n cQhg vd nh1n hai da thirc theo bi6u di6n ndy.
::'-.ng do
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 trong danh s6ch chira
:..\r cht s6 nhi phdn a; vd con tro tro t6i nrit
:hin
ki5
as-1.
:iir
lqp chuong trinh thuc hiQn phdp todn "c6ng
'. r rJua ra 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 trong
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
ra n6i nrit
x nim b6n tr6i nrit y (nrit
Hodc nrit x ndm trong nh6nh con tr6i cria nut y;
Ho{c nrit y nim trong nh6nh con phAi cira nrit x;
o
3bi
HoAc t6n t4i rnQt nirt z sao cho x
trong nh6nh con Phii cua ntt z'
nim trong nh6nh con trai
v2r
y nim
cdy nhi ph6n (x + y) chi c6
Hdy chi ra ring v6i hai nut x,y uat ki tr€n mot
tlung mQt trong 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 con cua no. Chung minh
sO ta
nni6u hon
sO
trOn cay
nirt b4c 2 dung m6t nirt'
nhi ph6n c6 ttr6 kh6i phgc mQt c6ch
6.10. Hdy chi ra 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 nhau 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,
1
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 ra 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));
0 (A-B)or(C=D).
'6.15.
vdi mqt dnh den/tring kich thudc 2n xzn, nguoi
ra 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
nhau 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 ra 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 nhau kh6ng vd neu
chring kh6c nhau thi chi ra mQt vi tri c6 mdu kh6c nhau 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 ra m6t tinh
ch6t thir vi:
N€u duong cli trong 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 trong c6c nrit nim b6n ph6i duong di. Khi dovx l,,y e R, ta c6
F
8
x < y. Hdy chring rninh ph6t hiQn cira gi6o su X ld dring hoflc chi ra mQt
phrin
vl dq.
uLr..
Prl
6.17.
iho
BST g6m n nut, bit dAu tu nrit Mi.ntmum^, ngudi ta goi hirm
Suc.cessor dC di sang nrit li€n sau cho tdi khi duyQt qua nrit Maxtmum^.
Chirng minh ring thuat to6n ndy c6 thoi gian thuc hiqn O(n).
Gqi y Th\Tc hiQn n loi gqi hdm Successor li€n ti6p dC duyQt qua c6c li6n
k6t chalcon trOn BST, mdi licn t6t toi da hai lAn.
Didu tuong tg c6 th6 chung minh dugc n6u ta bit dAu il n|lt ltLaxtmumn
vd ggi liOn titip him Predecessor d6 di sang nrit li6n truoc.
6-18. Cho BST c6 chi€u cao h. Bat dAu tu rnQt nf t p1, nguo'i ta tirn ntst p2ld nrit
li6n saupl: pz.= Successor(p1), ti6p theo: l4i tim nritp, ld nirt li6n sau
p2,... Chimg minh ring thtri gian thgc hiQnkltn loi gqi hdm Successor
nhu vQy le O(k + h).
5.19. Nguoi ta c6 th6 thqc hiQn viQc tip *5p mQt ddy kho6 bing cdy nhi phdn
tim ki6m nhu sau: Chen lAn lugt c6c gi6 tri kho6 viro mQt cdy nhi phdn tim
ki6m sau d6 duyQt cdy theo thu tg gita (thu4t to6n Tree Sort).
Hdy d6nh gi6 thdi gian thgc hiQn thuat to6n trong truong hqp t6t nhAt, x6u
nhj,t vd trung binh. Cdi dat thu{t to6n Tree Sort.
6.20. Hay vitit thuflt tohnsearchLE(k) d6 tim nrit chua kho6 I6n nhat khdng
vugt qu6 k trong BST.
6.2l.Hdy vitit thuat todnSearthcE(k) d6 tim nrit chria kho6 nho nh6t kh6ng
nho hon k trong
,\
BST.
)
5.22.Hdy viCt thu tuc MovetoRoot(p) nh{n vdo nrit p vd dirng c6c ph6p quay
d6'chuy6n nirt p thdnh g6c cua cdy BST.
5.23. Hdy vitit thtr tqc MovetoLeaf (p) nhAn.vdo nrit p vd dung c6c phep quay
dti chuy,5n nft p thdnh mQt nrit 16 cira cAy BST.
5.24. Cdy tirn kiLlm co sd lRodi* Tree) ld mQt cdy nh! phdn, trong d6 m6i nrit c6
thd chira hoflc kh6ng chira gi6 tri kho6 (ngudi ta thuong dtrng mQt gi6 tri
d{c biqt tuong irng v6i nut kh6ng chfa gi6 tri kho6 ho{c su dung th6m mQt
bit d6nh d6u nhtng nrit kh6ng chua gi6 tr1 kho6).
cftc ddy nhl ph6n, hay t6ng qu6t.
non ia rnQt ki6u dir liQu ndo tl6 c6 ttre md ho6 bing c6,c ddy nhi phdn.
C6c
gi|tri khori luu trf,' tr6n Radix Tree ld
Thu tUc chen mQt kho6 vio Radix Tree dugc thgc hiQn nhu sau: Bit dAu tg
nft g6c ta duyQt bieu di6n nhi phdn cua kho6, gap bit 0 di sang nh6nh con
tr6i vd gflp bit l.di sang nh6nh con phai. M6i khi kh6ng di dugc nfr'a (di
vio li0n k6t ntlT), ta tAo ra m6t nirt vd n6i n6 vdo c6y o ch6 li€n ktit nill
vta r€ sang r6i Ai ti6p. Cudi cirng, ta d.Qt kho6 vdo nrit cu6i icirng tren
duong di.
Hinh du6i d6y ld Radix Tree sau khi chdn c6c giit tri
C6c nirt td dim ld nrit kh6ng chri'a kho6.
\1
1
01
1
, I 0,
100, 0, 01
1.
.p
l--_-,\
0
6
I
.
II-<'
.,0'1
@
@
Gqi s ld tap chri'a c6c kho6 g6m c6c ddy nhf ph6n, t6ng dQ ddi c6c d6y nhi
phdn trong S lir n.
Chri'ng minh ring chi cAn thoi gian 0(n) de xdy dgng Radix Tree chria c6c
phAn tu cuaS, cdn thdi gian O(n) d€ duyQt Radix Tree theo'thri t1r gita vd
liQt kC c6c phAn tu'cua.S theo thri'tr,r tu di6n'
- 6.25. Cho BST tpo thinh ttr n kho6 dugc chdn vd6 theo mQt tr4t tg nglu nhi€n.
Gqi X ln bi€n ngAu nhi6n cho chidu cao cua BST.
ring ki vsng ElXl = O(lgn)'
6.26. Gqi b(n) ld s6 luong c6c cdy nhi phdn tim ki6m chta.n kho6 hodn todn
Chri'ng minh
phdn biQt.
l0
a)
Chung minh ring b(0) ='1 vd b[n)
k=0
II
In
1 (zn\.
),. b(n)=--i-;l
t- - | (tOin^.^t^,^r,^..--^\
Catalan thu n).
n
b) Chung minh rang
Ti
T
n
=fbub,-r-u,
n+r\ /
d6 suy ra x6c su6t aC eSr ld c6y nh! ph6n gdn hodn chinh (ho[c cdy
nhi phdn suy bi6n) n6u n kho6 dugc chdn vdo theo thti tu ngfiu nhi6n;
Tt
c) Hdy chimg minh c6ng thuc xnp
xi
b(n)
=#(t+ t[})J
tl Ttn'
a27. Thw ty th6ng kA
Cho mQt Treap, hdy xdy dgng thu4t to6n tim kho6 dring thri.p khi s[p thf
tg. Ngugc l4i cho m6t nirt, hdy tim s6 thu tg cua niit d6 khi duyQt Treap
theo thri'l\r gifa.
5-28. Treap bi1u tli6n tAp hW
Khi dirng Treap I bi6u di6n t6p hop c6c gi6 tti kho6 (tuc ld c6c kho6 trong
Treap hoin toirn phAn biQt), phdp thu k e T c6 th€ dugc thgc hiQn th6ng
qua him Search. ViEc th€m mQt phAn tu vdo tap hqp c6 th6 dugc'thuc
hiQn th6ng qua mQt sua d6i cua him lnsert (chi chdn ntiu kho6 chua c6
trong Treap). ViQc xo6 mQt phan ttr klioi t{p hgp cflng dugc thuc hien
thdng qua viQc sua d6i thtr tgc Delete (tim phAn tir trong Treap, n6u tim
th6y thi thqc hiQn thir tgc xoa). Ngodi ra, cdn c6 nhi€u thao t6c kh6c dugc
thUc hien rAt hiqu quf v6i c6u truc Treap, hay cii dflt c6c thao t5c sau ddy
tr6n Treap:
Phep tach (Split): V6i mQt gi6 tri ko,thch c6c kho6 c6 gi6 tri nho hcrnk6
vd c6c kho6 c6 gi6 tri l6n hon ks ra har Treap oc uitiu di6n hai tap hqp
neng re.
j,:Tim nrit chria phdn tri ke trong Treap,
ntiu kh6ng thAy thi chdn k6
vdo mQt nrit m6i, DAt dg uu tien cira nirt ndy bing 1m. Theo nguy€n li cua
c6u truc Treap, nrit niry sd dugc ddy 16n thdnh g6c cua cdy. Ngodi ra, theo
nguy€ir li cua c6u truc BST, nh6nh con tr6i eua g6c sE chria tAt ca c6c kho6
Gpi
c6 gi6 tri nho hcrn ks vd nh6nh con phii cua g6c cdy
kho6 c6 gi6 tri l6n hon k6.
sE chria
t6t cd chc
II
Phep hqp (Union): Cho hai Treap chria hai tdp kho6, x6y dung Treap m6i
chua tdt ca citc kho6 cua hai Treap ban dAu.
6-
Phdp giao (lntersection): Cho hai Treap chria hai tdp kho6, x6y dung
Treap mdi chira tlrt cit cdc kho6 c6 mdt trong cii hai Treap ban dAu.
Phdp tiiy hi€u (DiJference): Cho hai Treap A,B chtra hai tap kho6, x6y
dgng Treap moi chua cac kho6 thuQc.4 nhung kh6ng thu6c B.
6.29. Cho m6t BST chria n khoii, vd hai kho6 a, b. Liet kO t6t cir circ kho6 k cira
BST thoe mdn a < k b. Hdy x6y dpng thuat to6n c6. d0 phuc tap
=
O(m + lgn) (m la sO khod duoc liQt ke;.
6.30. Chon ld m6t s6 nguy6n duong vdx
day s6 (L,2, ... ,n).
vdi i: 1 < i 4
= (xy,x2,...,xn) ld mQt ho6n vl
cua
gai fr ld sd phAn ff dirng tru6c gi6 tri I mri lon hcvn r
trong ddy x. Khi d6 day t : (t1,t2, ...,t,,) duoc goi ld d6y nghich th€ cua
ddy x = (xt,x2,...,xn).
Tt,
. Vi du, voin = 6, cho ddy x = (3,2,L,6,4,5)
t - (2,1.,0,1,1-,0).
thi ddy nghlch th6 ctra n6
ld
Hdy xdy dqng thu4t to6n c6 dQ phfc tqp o(nlgn) tirn ddy nghich th6 tir
ddy ho6n vi cho tru6c.vd thu6t to6n c6 d0 phirc tqp o(nlgn) d0 tim ddy
ho6n vitu ddy nghich thO cho tru6c.
6.31. Tr€n m4t ph[ng vdi hq toa dQ Descartes 0xy cho n hinh cht nh4t c6 c4nh
song song v6i c6c truc toa dQ. Hay tim thudt to6n c6 dQ phfc tap o(n lg n)
de tinh diQn tich phAn mat phing bi n hinh cht nh.6t d6 chi6m ch6.
o.gz. crro n d6'y cung cua mQt hinh trdn, rrong d6 kh6ng c6 hai d6y cung ndo
chung dAu rnrit. Tim thu4t ro6n c6 dQ phr?c t4p o(nlgn) x6c dinh s6 c4p
ddy cung
cit nhaubOn trong hinh tron (vi
kinh thi s6 cflp ra I I
du, n6u
n
ddy cung ddu lir ducrng
ll
\2)
6.33. Tr0n tryc s6 chondo4n, doan thri ilitfai,bJ(ai,b, e N). Hdy chon rr€n
truc s6 m6t sO it nh6t cdc di€m nguy€n phdn bi€t sao cho c6 it nhAt c1 di6rn
duoc chon thu6c vdo doan thri t (1 1n 110s; 0 S ai,bi,c; < 1_0s).
t2
6.
G34. B6n d6'mqt khu d6t hinh cht nh4t kich thu6c m x n duoc chia thdnh ludi
6 vu6ng don vi, trdn d6 c6 d6nh d6u k 6 trdng cdy (m,n,k < 10s). Nguoi
ta mu6n gidi ph6ng mqt m4t bing nim trong khu dat nay. tsrin d6 m4t b6ng
c6 c6c rdng buQc sau:
r
o
o
Canh rnat bing c6 d0 dei ld s6 nguy€n;
Mit bing chi€m trgn mQt s6 6 tt€n ban d6;
Canh m[t bing song song v6i c4nh brin d6.
Hdy trzi loi hai c6u hoi sau:
.
a)
A
-,1
Neu muon xay dlmg mflt bdng v6i canh ld D thi phai gi6i ph6ng it nhat
bao nhi€u 6 tr6ng
cdY?
,,
b) N6u khdng mu6n gi6i ph6ng 6 tr6ng cdy niro thi c6 thtl xdy dung dugc
--,bdng v6i canh 16n nhdt
mflt
.)
li bao nhi6u?
i..,
6.35. Mat b0 n < 10s 16 bdi duqc xtip thdnh t4p vd m6l ta bdi dugc ghi e6 thri tg
ban dAu cua 16 bei d6 rrong tflp bdi (vi tri c6c lA bei dugc cl6nh s6 til I toi
n, tt trCn xu6ng du6i).
X6t thao tirctr6o ki hiqu b
bei thf i trong s6 n - 1 la bdi con l4i (1< t,iS n).
euy u6c ring n6u j = nthi le bdi thri i sE duoc Cl6t vdro vi tri dudi cirng
cua t4p bdi.
Vi du, vot n
:
6:
(t,A3,4,s,6)t9
(3,t,2,@, 5,6)
@ L,2,s,4,6)
(
t,t, E, 4,s,6)
sf4 q\
"j(3,L ;2,5,@, 6)
tl3
('J',2,5,4,6,@).
,.J
Ngudi ta tr6o bo bei b4ng x ph6p tr6o (x < 10s). Nguoi choi dugc bi6t x
tirao t6c tr6o d6. Hdy su dqng th€m mQt s6 it nhdt c6c thao t6c tr6o nira dO
dua c6c 16 bAi vd vi tri ban dAu.
V6i vi dp tr6n, ta cAn su dpng th6m hai thu tgc tr6o 5(6,3) vd S(5,4)'
t3
BAI TAP BO SUNG
6.36. Xod
r
sO
i-
A
^
Cho ddy s6 nguydn
A = (a1,e.2,...,a,r). Nguoi ta tim chi s6 I (1 < t
nho nh6t thoi mdn di6n ki6n ai 1ai-1vdai 1a111 r6i xo6 di s6 a; khoi
ddy. Sau khi xo6, s6 phan tu trong ddy A girim di I vd c6c phAn tu con lai
cua ddy dugc d6nh chi s6 l4i tu 1 bdt ddu tu a1. C6ng viQc ndy l[p lai cho
toi khi khdng tim duoc chi s6 r thoa mdn dieu ki6n rren.
YOu cdu: Cho bi6t sd phAn tir cdn lai trong d6y.
Input: T€p vdn bin DELNUM.INP
o
.
bao g6m:
I chria s6 nguy€n duong n < 10s;
Dong 2 ch,0a n s6 nguy€rr a.1,e2, ...,en (vt:lail < 10e), c6ch nhau it
Ddng
I
I
I
nh6t mqt d6u c6ch.
Output: Tgp van ban DELNUM.OUT ghi mQt sd nguyOn duy nh6t ld s6
lX.,
luong phdn tu'con lai trong ddy.
t
t
I
Vl du:
I
I
DETNUM.INP
DELNUM.OUT
6
3
3L2401
6.37. Hinh chir nhQt l6n nhiit
I
I
t
I
I
cho mQt bang hinh chir nh4t kich thudc mxn dugc chia thdnh lu6i 6
vudng don vi mhdng, n cot. c6c hdng duoc ddnh so tu I tsi m theo thir tu
tu' trdn xudng dudi vd c6c c6t dtrgc cl6nh s6 tu t toi n theo thri tlt' tir tr6i
qua phrii. Nguoi ta ti€n hdnh td mhu c6c 6 cua bing theo tung cQr: cric 0
;.
tr6n rn6i cQtT sd duoc t6 tt tr€n xu6ng duoi:\ 6 mdu vdng ti6p d6n ld
m
hi 6 rndu xanh. Nhu v4y tinh trang mdu trdn biing hodn todn x6c dinh
n€u ta biOt duqc s6 hdng m. iO cdt n vd cdc s6 nguy6n h1, h2, ... , hn.
-
Hdy xac dinh mQt hinh chi.r nhQt gin cac 6 trong bang dd cho thod mdn
cac yeu cau sau;
c
o
t4
Co c'anh sdng song vo'i c'anh bang
Do'n sdc @hi gim cdc 6 vdng hoQc chi g6m cdc d xanh);
I
I
I
l
lo
I
i
I
Di€n tich ton nhtit c6 th€.
t234561
I
m=5
2
3
--o
A
11
:
(I,3,4,4,5,4,4,3,1)
5
Input: T€p vdn
o
o
Dong
ban RECT.INP bao g6m:
l: Chua hai s6 nguy€n
duong m,n (m,n
Dong 2: Chtan s6 nguy6n h1,h2,
Output:
'.',h,
(V,1:
<
0S
106)l
\
< m)'
TCp v6n ban RECT.OUT bao g6rn:
r
o
Dong l: Ghi dien tich hinh chfr' nh6t'tim duoil
Ddng 2: Ghi chi s0 hdng vd chi s6 c6t cua o o goc tr€n tr6i cua hinh
chfr'nhdt tim dugo;
o Dong 3: Ghi chi s6 hdng vh chi s6 cot cua 6 o goc du6i phai cua hinh
chfr'nhdt tirn duo. c.
Cac s6 ftAn m6t ddng cr)a t€p Input vit Outpttt dwqc ghi cdch nhatt It nhat
mot ddu cach.
Vi du:
RECT.TNP
59
134454431
RECT.OUT
2t
t2
5tt
638. rQp dQc tfip
Nguoi ta mO hinh ho5 m6t mach diQn mot chidu theo c6ch de quy nhu sau:
o
.
MOr m?ch diQn co mQt dAu vdo 1 vir mQt dAu ra O voi m6t ddy ddn nOr
tu / t6i O duoc ki hieu bing rnQt ki tg g'
N6u G1 ldr mach diQn co dAu vdo 11 vd dAu taoy G2 lii mach diQn co
dAu vdo 12 vir diu ra O, th\ mach diQn nh4n duoc bing c6ch ch4p dAu
ra OlvaiAu uuo I, thirnh mQt di€rn sE tro thdnh mach diqn noi ti6p c6
dAu vdo 1., vd dAu ra O2, ki hiQu bing xdu ki tu 'SG1G2'
15
Ndu G1 ld mach di€n c6 dAu vdo /1 vd dAu ra 01, G2lir machdiOn c6
dAu vdo12 vd ddu ra}rthlm4ch diQn nh4n dugc bing c6ch ch4p hai
dAu vdo Ir,l, thinh mQt dAu vdo (ki hiQu I12) vd ehflp hai dAu ra O1, 02
thdnh mQt ctau ra (ki hiQu op) sE rro thdnh m4ch di€n song song c6
dAu vdo Ir2 vitdAu ra O12, ki'higu pGrG2.
@ c{re
sgg
Pgg
PSggg
SpSgggpgg
MOt.t0p c6c di€m duoc goi ld 4p dQc lap n6u n6 kh6ng chua hai di6m ndo
,:
.!
c6 ddy ddn truc ti€p. Hdy x6c dinh s6 lugng si6m trong tap doc lap lon
nh6t cua m6t mang di6n cho boi 'x6u ki tg g6m cdc chir cii p,s, g theo quy
t6c trdn.
Input:rTQp vdn brin INDEP.INp g6q mQt dong chrla kh6ng qu6 106 ki tu.
output:
TQp van ban
INDEP.our ghi s6 luong di6m trong t4p d6c
l6p
lcrn nhAt.
VidU:
6.39. BOi
-
INDEP;OUT
2
nhti nhiit
cho s6 nguy€n duong n vd mQt tflp s c {0, ...,9}. Hay rim mQt s6 nguydn
ducrng m tqo thdnh tu crlc ehir s6 thuQc s tho6'mdn c6c di6u kign sau aay,
,'
, n.
m chia hdt cho
o
l6
sO
INDEP.INP
sPSgggPgg
.
mnho nh6t c6 th6.
2.r
co
Input:
tai
.
o
r2
co
TQp vdn brin
LM.INP bao gdm:
^
duong n < 1'06;
uong I cnua so nguyen
Ddng 2 chta kh6ng que l0 ki tu li6n nhau, m6i t
;
|
,-
z
s6
trong tflp S.
Output: TCp vin btin LM.OUT gom mQt dong duy nhAt chria sO m tim
duoc. NtSu kh6ng t6n t4i sa5 m thori mdn c6c yQu cAu dat ra thi ghi trOn
t^
dong nay mot so u.
vi ds:
LM.INP
LM.OUT
7
42
24
18
L44
14
10
0
L234
5.10. TrQn tldo.
o
Tdn NgQ Kh6ng len viro vudn diro cua Vucrng Mdu vd nhin thdy mQt cdy
dio triu qua. NgQ Kh6ng bdn niEm chu ggi Th6 Dia l€n hoi thi dugc bi6t:
CAy ddo qdy c6 n qup d6nh sd tt I tdi n, quh thu r phai t6i thoi diOm t;
m6i chin vd c6 gi6 trf ki (giit tri fti cho bitit khi 6n qu6 dlro do thi tu6i tho
sd t[ng l€n k; ndm, cdn gqi ld gi6 tri cua qua ddo).
n
v
Tai m6i thoi di6m, NgQ Kh6ng c6 th6 chori m6t quri ddo chin d6 h6i. Vi€c
h6i m6i qua ddo m6t dring mQt don vi thoi gian, NgQ Kh6ng phAi h6i tung
qud mQt.
Ngodi ra, d6 tr6nh bi ph6t hiQn, NgQ Kh6ng du ki6n s6 chi h6i ddo tt thdi
di6m a tdi h6t thoi di6m b (qua dio cu6i cung phai dugc h6i xong kh6ng
mu6n hon thoi di6m b).
Hdy chi cho NgQ Kh6ng mQt c6ch chgn c6c qua ddo de h6i sao cho t6ng
gi6 trf nhirng quri ddo dugc chon ld l6n nh6t c6 th0.
lnput:
o
.
TQp vdn ban PEACH.INP bao gdm:
Dong I chria ba s6 nguy€n duong n,a,b (n
-LCTHBTQ2-A
<
10s; a
1b < 10e).
t7
.
Dong thri I trong n dong ti€p theo chria hai sd nguy0n diong ti,k1
(ti,ki. < LOe,vi: L < t < n).
Output: Tgp vdn brin PEACH.OUT g6m mQt s6 nguy€n duy nh6t ld t6ng
gi6tri nhfrng qui ddo trong phucrng 6n tim dugc.
Vi du:
PEACH.INP
PEACH.OUT
514
36
110
L5
L5
320
4 100
Giai thich: Phuong 6n tdi uu
Thoi tti6m
ldr:
l: H6i quri ddo I (gi6 tri l0);
Thoi di6m 2: Hdi quri ddo 3 (giit tri 6);
Thoi di€m 3: Hrii qua ddo 4 (gitt tri 20);
Thoi di6m 4: Roi khoi vuon ddo.
T6ng gi6 tr!: l0 + 6 +
20:36.
Lo, y;Kh6ng h6i duoc quri ddo thu 5 vi Ng6 Kh6ng h6i ddo trong khodng
thdi gian tir thdi ditlm I dL{n tru6c thoi di6m 4 md dring thoi dicm 4 quh
ddo thf 5 m6i chin (m4c
{i gi6 tri cua qua ddo thri 5 c6 trre rAt ton;.
6Al. CAu cting
'
MQt c6ng bi6n c6 m ceu cring d6 ti6p nhan c6c tdu cfp btin. Tai m6t thoi
di6m, m6i cAu cing chi c6 th€ ti6p nhfln kh6ng qu6 mQt idrr. Bar, dAu c6c
.-l
\
,
cdu cdng d6u trong vi c6 n tdu xin rl6ng ki c6p b6n, tdu thri I mu6n d0u o
cdng tri ngay sau thcvi di6m s; t6i hot thoi di6m fi.cd th6 coi thoi gian tdu
thir j muon dOu o cang ld mot khoan g Gt, fil tr€n tryc thoi gian. Tiu dd
.l
:
vdro c6u ciing ndo thi s€ dflu o d6 trong su6t thcri'gian nim
cring.
YOu cAu: Hdy cho ui6t vol m cau cilng dd, cho, c6 the ti6p nhdn t6i da bao
nhi6u tdu vd chi ra iich trinh ti6p nhpn t4i m6i cAu cang.
Input:
18
TCp
vln b6n SEAPORTS.INP
bao g6m:
.
o
Ddng I: Chria hai s6 nguydn duong m,n 1L0s:
Dong thri I trong n dong titip theo chria hai s6 nguy6n s;, fi
(0 < si <.n < 10t).
Output:
o
o
'
TQp van b6n SEAPORTS.OUT bao g6in:
Ddng L: Ghi sO luong tdu dugc ti6p nhin phuc vp;
Dong 2: Ghi n s6 nguy6n, s6 thu i ld s6 hiQu cAu ciing sE ti6p nh4n tdu
thri i trong truong hop tdu thri i dugc ti€p nhdn, cdn n6u tdu thri i
kh6ng duqc tiCp nh4n thi s6 thu t le 0.
Cac s6 tAn mQt ddng ct)a tQp Input va Output dwqc ghi cach nhau ft nhd:t
m\t ddu
cach.
Vf du:
SEAPORTS.INP
25
03
35
o2
25
14
SEAPORTS.OUT
4
LL22O
6.12. Thdng Bdm vd Phir Ong
rg
la
ii
rc
o
u
a
Bom thing Phri Ong trong mQt cuQc tl6nh cugc vir buQc Phir Ong phai dai
rugu. Phri Ong bdn biry ra mQt ddy n chai chria ddy rugu vir noi v6i Bom
,t \ .t.:'
..i
, .' I
r
+!. 1
-)
-t
ndo thi phtii uong
rdng c6 thd u6ng bao nhi€u tuy y, nhung d6'chon chai
n6t va kh6ng dugc u5ng cv k chai lidn nhau bdi d6 ln di€u xui xeo.
B4n hdy chi cho Bom c6ch u6ng dugc nhiAu ruqu nh6t.
Input: T€p vdn ban BOTTLES.INP bao gdm:
o
o
Dong 1 chira hai s6 nguy€n n,k (!'3 n < 4.10s;2 < k ( 4,10s);
Ddng 2 chisan s6 nguydn duong (nho hcrn hopc bing 10e) ld dung
tich cua b5c chai ruqu Phri Ong bdy ra, theo thri tg fiet kC tu chai thf
nh6t t6i chai thri n.
Output:
o
o
TQp vdn ban
BOTTLES.OUT bao g6m:
Dong I ghi sd chai dugc chon vd lugng ruqu t6i tla c6 th6 u6ng;
Dong 2 ghi chi s5 crha c6c chai dugc chgn theo thri'ty t[ng dAn.
t9
Cdc s6 tr€n mQt ddng cila tQp Input vd Outp,ut duqc ghi cdch 4hau ft nhdt
^ dau
': cach.
mot
..:
Vi dg:
BOTTLES.INP
BOTTLES.OUT
53
51010131010
440
2356
6.43. Trdo bdi
Phir Ong c6 bQ bdi gom n 16 bdi. Phri Ong xtip chring thdnh t4p vd ghi vdo
rn6i 16 bdi s6 thri ttr ban dAu cria 16 bdi d6 trong t4p bdi (vi tri c6c 16 bdi
duqc d.anh s6 tu I toi n, tir trCn xu6ng du6i).
Ti0p theo Phri Ong ti6n hdnh tr6o tQp bdi, m6i phdp trrlo ki hi6u boi s(t,7'):
rut ra 16 bdithu i vd chdn l€n trdn 16 bei thf i trong s6 n-'1.16 bdi coniui
(l < t,j 3 n),quy u6c ring n6u ='nthi 16 bdi thf r s6 duoc dit vdo vi
tri du6i cing cira tpp bdi"
j
Vi du v6in
*
6:
(t,As,4,s,6)fB
1r,s,
@ +,s,0)
(8"3,2,4,s,6) :93 (s,El z,q,s,6)
. s(4.s) .
-:
EI !,2,s,4,6) g
(3,L,2,14.1,
(
s,o)
(3,t,2,5,
EI 6)
(t,2,s,4,6,@).
x phdp tr6o, Phir Ong dua cho Bom t4p bdi vd th6ch Bom dung it ph6p
tr6o nhAt dc xep l4i c6c 16 bdi ve vi tri ban dAu. Hay girip Bom thgc hiQn
Sau
di€u d6.
Input:
o
o
TQp vdn ban SHUFFLE.INP bao g6m:
Dong I chua hai s6 nguy€n duong n,x (n,x < 10s);
Ddng thri p trongx dong ti6p theo chria.hai s6 nguydn ir, jo cho bi6t
ph6p 116o thri p cua phf Qng ld, S(ip, jp)
Output:
o
20
TQp van
bin SHUFFLE.OUT bao g6m:
Dong I ghi s6 y ln s6 phdp tr6o cAn thqc hien dc dua
tri ban dAu:
ctrc 16
bdi v6 vi
thtit
o
Doqg thft q trongy dong titip theo ghi hai sO nguy€n
phdp tr6o thri q cua Bom ld S(t*
Cdc s6
t€n
n6t dau
mQt ddng
cila
tQp
io,j,
cho bi6t
j)
Input vd Output dtqc ghi cach nhqu it nhdt
cach.
Vi du:
SHUFFTE.INP
64
23
IZ
SHUFFTE.OUT
2
63
54
45
IO
6.11. Nhirng hitn tilio
Ban dd m6t trang trai ld mQt hinh chfr nhAt kich thu6c m x n duoc chia
thdnh lu6i c6c 6 vu6ng don vi. Cdc hdng cua lu6i
i I toi m
'luoc d6nh s "u'
tir tr6n xu6ng du6i vd c6c cQt cua luoi clugc d6nh s6 tu I t6i n tu tr6i qua
a-.
phai. O giao cta hdng x, cQt y dugc ggi ld 6 (x,y) vi 6 d6 c6 dQ cao h,n.
Trong nhfr'ng nghy mua tAm t6, muc nudc ddng l€n vir trang trai bi ngap
dAn trong nu6c. N6u muc nu6c lir k thi nhirng 6 c6 d6 cao nho hon hoic
, +^
r J
t +
r,
r
t
bang k dugc coi ld ngQp nu6c cdn nhfr'ng 6^ c6 d0 cao l6n hon k duoc cor
ld chua ngQp nu6c. Nhfr'ng 6 chua ngdp nu6c tao thdnh nhirng "ddo", dinh
nghTa: Hai 6 chua ngdp nu6c duoc gqi ld cung d6o nOu ta c6 the di tu'6
ndy t6i 6 kia bing c6ch di chuy6n qua c6c 6 kd cpnh chua ngAp nuoc,
nguoc l4i hai 6 d6 dugc coi ld nim tr€n hai d6o kh6c nhau.
Vi du v6i b6n d6 dudi ddy, tac6 b6n d6o khi muc nu6c bing2,c6 hai clao
khi muc nu6c bing
7.
I
5
A
7
I
5
5
7.
7
I
I
I
6
6
J
3
I
6
J
J
I
6
6
t,-1
2l
YGu cAu: Gi6 sri trong nhfrng ngey mua, muc nu6c d6ng d6n l€n cho t6i
khi todn bQ c6c 6 dou ngdp nu6c. X6c dinh so dao tai mQt thoi ditim trong
nhtng ngdy mua md tai thoi di€m d6 c6 nhi6u drio nh6t.
Input:
o
o
TQp vdn bdn
ISLANDS.INP bao g6m:
Dong I chria hai s6 nguypn duong m,n (m,n < L000);
Ddng thri i trong m dong ti6p theo chuan s6 nguydn duong, so ttru;
lir
hij (hij S
106).
t€n m6t ddng ct)a tQp dwq'c ghi cdch nhau ir nhat n6t ddu coch.
output: Tgp vdn ban ISLANDS.OUT g6m mQt s6 nguy0n duy nhAt ld s6
Cdc
sij
dao t4i thcyi cli6m c6 nhi6u drio nh6t.
Vf ds:
ISTANDS.INP
ISLANDS.OUT
66
918154
718155
778111
111166
33166L
331611
6.45. Qudn l{ Iwong
'
MOt c6ng ti c6 n nguoi ddnh s6 tir I t6i n, nguoi thf i c6 luong lit wi.
T6ng Gi6m d6c cdng ti duoc d6nh s6 l, m6i nguoi tri 2 tdi n c6 dring m6t
thri truong tluc ti6p ctra minh. Ta n6i ring nguoi r qu6n li nguoi n6u t6n
t4i
ddy
i = x1,x2,...,xn= jsao
7
cho ngudix; ld thri truong tnrc ti6p cua
ngudi r;*1.
u6i
nguoi dugc quyon tdng ho{c tru luong cira t6t ci rnoi ngucri trong
li cua minh. Ban cAn viet mqt chu
quydn quiin
hai lo4i tdc vg:
.
.
22
p I x: Nguoi A tdng luong cria t6t cii nhirng ngucri trong quy6n qu6n li
cua minh thOm x d6ng (-10000 < x < L0000);
u A: Cho bi6t lucmg cira nguoi l.
7.
Chi i: Luong cira m6i ngudi trong su6t qu6 trinh qudn li ndm trong pham
:.^
vi c6c s6 nguy6n c6 ddu 32bit.
Input:
o
TQp vdn
Ddng
bin SALARY.INP bao g6m:
l: Chria.hai s6 nguy6n duong n,m(n,m (
5.10s), trong d6m
ld sO c6c t6c vg;
o
.
Ddng thri f trong n dong ti€p theo chria lucrng khoi di€m vd s6 hiqu
thri truong cira nguoi r. Ri€ng voi nguoi l, ddng tuong ung s0 chi c6
lucrng khdi di6m.
..4
m dong ti€p, m6i dong chfa mQt t6c vu.
Output: T€p vdn bin SALARY.OUT: Vdi m6i t6c vg loai u, in ra k6t quii
tr€n m6t dong.
Vi ds:
SALARY.INP
SALARY..O.UT
61
7
5
4L
32
73
23
35
p32
p24
u3
u6
P5-2
u5
u1
9
7
5
CHUYEN DE 7. DO THI
Cho d6 thi c6 hudng G = (V,E) kh6ng c6 chu trinh 6m. Hdy tim thu4t
.
todn c6 tlQ phric tap O(lYllEl) dC tinh t6t ch iitc d.(u) = min{d(r,v)}
/)
vetl \ \
z5
7.2.
Cho d6 thi c6 hucrng G
= (V,E) gdm n dinh
vd m cung.
H6y tim thu4t to4n c6 d0 phuc t4p O(nm) dtl x6c dinh d6 thf c6 chu trinh
6m hay kh6ng vd chi ra mQt chu trinh 6m n6u c6.
7.3.
HQ
rdng buQc
Cho x1, x2, ... , xn lir circ bi6n s6, cho m rdng buOc, m5i reng buQc c6 dang:
t_i
xj-xi.wtj(w;;eR)'
Hdy tim c6ch g6n gi6 tr! cho c6c bi€n x1, x2, ... , xn thoh mdn t6t
bu0c dd cho.
7.4.
ci cilc rdng
Cdi ti€n ciia Yen cho tha\t torin Bellman-Ford
thi c6 hudng G = (V,E) g6m n dinh, ta d6nh s6 c6c dinh tu I toi n.
Chia tap cung E ldm hai tdp con: E, g6m c6c cung nOi ttr dinh c6 chi sO
nho t6i dinh c6 chi s6 l6n vd E2 gdm c6c cung nOi tu dinh c6 chi s6 lon toi
' dinh c6 chi s6 nho. DAt G1 : (V,Er) vd Gz = (V,l2),haid6 thi ndy li hai
d6 thi c6 hudng kh6ng c6 chu trinh duoc bi€u diOn bdng danh s6ch kO.
Voi
dO
Thudt to6n Bellman-Ford sau d6 dugc thgc hi0n nhu sau:
repeat
bcop :: true;
for u :: 1 to n - 1 do
for Vv: .(u, v) €E1 do
if Relax(u, v) then Stop:= Fal-se;
foru:=ndownto2do
for Vv: (u, v) CE2 do
if Rel-ax (u, v) then Stop :: False;
until Stop;
B€n trong vdng l{p repeat...untitldhai pha t6i uu nhtn: pha thri nhAt xdt
c6c dinh theo thri tu rang dAn con pha thu hai x6t c6c dinh theo thir tg gi6m
.' dAn cua chi s6. M6i khi mot dinh durvc x6t vd thuc hiQn phdp co theo t6t
"a
c6c cung di ra khoi u, mbi pha thuc hi€n tuong tu nhu thuflt to6n rim
duong di ngin nh6t trdn d6 thi kh6ng c6 chu trinh.
24
1-(
Hdy chi ra ring vong'l{p repeat...until trong cdi ti6n cta Yen lap kh6ng
. | lt ! t, qua, rlnl | ,.
- Ian. Cdi ddt thu0t to6n vlr so s6nh vdi c6ch cdi d[t chuAn cua thuat
^ lzl
todn Bellman-Ford.
?-5. Arbitrage ld mQt c6ch su dr,rng sr,r bdt hqp li trong trOi eoai ti6n tE eC n€m
loi. Vi du ntlu l$ mua dugc 0.7f, lf mua dugc 190Y, lY mua duqc 0.009$
thi tir 1$, ta c6 th€ d6i sang 0.7f, sau d6 sang 0.7 x 190: 133Y, r6i e6i tai
sang 133 x 0.009
Gi6 sir ring c6
ti gi6 h6i do6i:
:
1.197$. Kitim dugc 0.197$ lAi.
I t6i n. BtngR = {r,;},rr,, cho bi6t
m6t don vi ti€n t d6i duoc 4; don vi ti6n 7.
n
loaiti€n te d6nh s6 tir
H6y tim rhuat toAn d6 x6c dinh xem c6 th€ ki0m loi tu bdng ti gi6 h6i do6i
.
,J
nay
Dang pnuong ph6p Arbitrage hay kh6ng? N€u c6 th6 su dpng
Arbitrage; hdy chi ra mQt c6ch ki6rn loi.
a.6.
ThuQt todn Karp tim chu trinh cd trung binh trgng
Cho dO thi c6 huong G
= (V,E)
si5
nhti nhdt
g6m rt dinh, hdm trqng s6 w: E -+
IR'.
Ta dinh nghia tmng binh trgng s6 cria m6t chu trinh C g6m c6c cung
(e1, €2, "' ,€ul nhu sau:
k
s=;K /-/) w(e).
l-
LT(C)
;-a
D1t tt* = min{r(c)}
.
co 1t(C): p- gQi ld chu trinh c6 trung binh trong s6
nho nh6t (minimum mean-weight cycle). chu trinh niry c6 nhi€u 1f nghia
trong c6c thuat to6n tim ludng voi chi phi cuc ti6u.
Khi d6 chu trinh
C
Kh6ng ldm m6t tinh t6ng qu6t, gi6 su mQi dinh u e 7 ddu d6n dugc tir mQt
dinh s e V (ta c6 th6 th€m mQt dinh gi6 s vd cung trong sO O nOl tu s tdi
mgi dinh kh6c, s khdng nim ff€n chu trinh don ndo ndn kh6ng dnh huong
toi tinh dring d6n cua thu4t to6n).'Ept d(y) le d9 ddi ducrng di ngin nh6t tu
s t6i u. Dat dr(u) le do ddi duong di ng6n nh6t trong s6 c6c duong di tu s
tbi v qua dirng k cung (ta c6 ttr6 th6m vdo c6c cung trong s6 du l6n d6 v6i
25