P. 1
هياكل البيانات

هياكل البيانات

5.0

|Views: 3,278|Likes:
منشور بواسطةMohammed Mohyeldin Siddig

More info:

Published by: Mohammed Mohyeldin Siddig on Oct 21, 2011
حقوق الطبع:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

05/24/2013

pdf

text

original

ميحرلا نمحرلا هللا مسب

تايمزراوخلا
تايمزراوخلا ليلحت :
1 لوصحلا نكميو ةيلاع ةءافك تاذ جمانرب ىلع لوصحلا وه ر!لا "
"#ني$%ير&ب ةيلا'لا ةءافكلا تاذ جمانر(لا ىلع
) جمانر(لا *يف+, نم- .يل%, /0
) ةركا*لا نم ةم12$3ملا ة4ا3ملا .يل%, /5
"#نم6لا 7اي8
/ 0 oh notation 9
:
;م <+ع ر('نو O(N ) =
9
:
;م <+ع ر('نو >;ير?وللا /5 log
2
)
@ب جمانر(لا نم- 7اي8 نكمي O(N تاAارك, B1ع ةCر'مب Dلذو =
) جمانر(لا .EاB تاAا('لا
FناG لامهH FIي *يف+$لا نم- ة&Jاوب جمانر(لا ةءافك 7اي8 1+عو
1+ع ة4ا3ملل ا
:
Aا($عا K&', L M0 ةركا*لا KC ةم12$3ملا ة4ا3ملا
# نم6لاب ةءافكلا 7اي8
"# ة1عا8
O(N
1
) + O(N
2
) = O(MAX(N
1
,N
2
==
Nاك اذH N
1
>>N
2
)
F340 # لا;م oh notation # Kلا$لا جمانر(لل
For i :=1 to N do
Read (x O =
وه جمانر(لا نم ء6Iلا ا*ه *يف+, نم- NPC O(N ) =
# Kلا$لا جمانر(لا *يف+, نم- F340 # لا;م
For i:= 1 to n do
For j :=1 to n do
Read (x O =
وه جمانر(لا *يف+, نم- NPC O(N
2
) =
# لا;م
For i:= 1 to n do
Read(x O =
Read(y O =
وه جمانر(لا نم- NPC O(N)+O(1 ) =
Qناك اذH ة1عا%لا F34و N2>>N1 N0 لو%+C R%C ر(كSا *ETن <نPC
وه جمانر(لا *يف+, نم- O(N ) =
# نيرم,
F340 oh notation # ةيلا$لا تاAا('لل
1 O / !" = #
$or % := 1 to n do
!" := !" + 1 O
1
U / O & := n ' n
$or % := 1 to n do
$or j :=1 to & do
O !" := !" + 1
V / $or i:= 1 to n do
$or j := 1 to % do
!" := !" + 1 O
نم6لا Aاي'مب ةءافكلا ني3ح$ل لا;م
7Sا ةيم-AاوE
Nاك اذH N NاC KGو- B1ع X
N
= X
N(2
) X
N(2
Nاك اذH N NاC MBرC B1ع X
N
= X
N*1(2
) X
N*1(2
) X
F3حي جمانرب F$ك0 # لا;م X
N
ةرمو تا%لحلا ا
:
م12$3م ةرم
F340و 7Sا ةيم-AاوE ا
:
م12$3م WرE0 oh notation .ك KC
X ةرم
F!n+tion ,o-er(x,n:integer ):real O
.ar
/!":real O
% : integer O
0egin
/!":= # O
For % := 1 to n do
!" := !" 1 x O
,o-er := !" O
end O
# 7Sا ةيم-AاوE Yا12$Jاب
F!n+tion ,o-er(x,n:integer):real2
0egin
%$ n = # then ,o-er :=1
3le
%$ odd (n) then
4o-er := x ' 5r(,o-er (x,n di6 2 ==
3le
4o-er :=5r (,o-er (x,n di6 2 O ==
3nd O
U
5ا34 >, اذH ني%با3لا نيلا;ملا .يلح, X
72
*يف+, نم- NPC
لوSا جمانر(لا O(72 KC 7Sا ةيم-Aاو2ب جمانر(لا *يف+, >$ي ام+يب =
"# R%C تاو&E ة3مE

X
72
= X
81
) X
81
X
81
= X
19
) X
19
) X
X
19
= X
:
) X
:
) X
X
:
= X
8
) X
8
) X
X
8
= X
2
) X
جمانر(لا *يف+, نم- NذH X
72
Mوا3, 7Sا ةيم-Aاو2ب O(9 ) =
KC 9
:
;مC جمانر(لا *يف+, نم- 7اي8 KC ا
:
Zي0 >;ير?وللا Y12$3ن
[لا )\ ] لا1ب^ا ] لاEB^ا ] _ا%فلا ` ةفل$2ملا Fي,ر$لا تايم-AاوE
aف$, اbن0 cم ةءافكلا ةي4ان نم dل$2, تايم-Aاو2لا e*ه NPC =
Y0 ا
:
ي1عاص, رfا+'لا Fي,ر, Kهو K3يgرلا اbC1ه KC اb'يمG
) ا
:
يل-ا+,
7ا%ي hح(لا Dل*كو >;ير?وللاب Fي,ر$لا تايم-AاوE ة+م-0 iي%+C
) >;ير?وللاب
تاسدكملا STACKS
-: ةسدكملا فيرت
اbيC >$ي ةي&E ة%ير&ب jفح, تاناي(لا نم ةمgا8 Kه ةJ1كملا
` ةم%لا ىم3ي 14او kرl نم تاناي(لا ةCاmHو k*4 ;O4 ) =
تاناي(لا نم _و+لا ا*ه ىلع aل&يو <%FO @ل Aاص$Eا <A/; %N
F%R/; O=; ) L
:
و0 nAا2لا وه ا
:
ريE0 .Eا1لا ]
ة(ي%حلا .EاB ة'$مSا Fي,ر, ةيلم'لا ةايحلا KC ةJ1كملل لا;م
nر2ي ام لو0 وه اboلم 1+ع ة(ي%حلا KC <'Zن ام رEp Nوكي hي4
) اb!يرف, 1+ع اb+م
-: ةسدكملا تايلم!
1 * جمانر(لا ة&Jاوب >$,و ) ةJ1كملل رص+ع ةCاmH ةيلمع
Kعرفلا 4=/> )
ةلا1لا Kهو ة1عا3م ةلاB ىلH nا$ح, ةCاm^ا ةيلمعو F=<< hي4
ةلا4 KC <نS ] L Y0 ةoل$مم ةJ1كملا Qناك اذH Aا($Eا اb$فيqو
ةلكrم ىلH MBsي Dلذ NS 1ي1G رص+ع ةCاmH نكمي L ء9$مLا
NاZيفلا /;A?@ O.3R F<OA )
V
U جمانر(لا ة&Jاوب >$,و ) ةJ1كملا نم رص+ع k*4 ةيلمع"
Kعرفلا 4O4 )
ةلا1لا Kهو ة1عا3م ةلاB ىلH nا$ح, k*حلا ةيلمعو 3M4;B hي4
نكمي L <نS ) L Y0 ة?AاC ةJ1كملا Qناك ام اذH Aا($Eا اb$فيqو
Qt14 LH و ة?AاC ةJ1كم نم رص+ع k*4 /;A?@ =NC3R
F<OA )
-: تاسدكملا "ا#ب
1 " ) ةJ1كملا رfا+ع ءاو$4L ل-ا+مك تاCوفصملا .م'$3,
U " ) ةJ1كملا ةم8و ةCوفصملا Rبرل ت9I3لا Y12$3,
ر(ك0و k*حلاو ةCاm^ا ةيلمع *يف+, ءا+t0 ةJ1كملا B1م$,و uمك+,
ةJ1كملل ا
:
فلJ B1حملا W1ملا وه ةJ1كملا ةيلH .ص, W1م
MAX/;A?@ ةJ1كملا _ا8 وه ةيلH .ص, W1م .80و
0O;;OMMO/; Yا12$Jاب ةJ1كملا ةم%ل Kلاحلا c8وملا B1حيو ]
@لا ;O4 )
"# تاJ1كملا dير',
?ON/;
MAX/;A?@ = 1## O
;B43
/;A?@ = R3?ORC
%;3M : ARRAB D1))MAX/;A?@E OF %N;3F3R O
;O4 : #))MAX/;A?@ O
3NC O
.AR
/ : /;A?@ O
ةلادلا EMPTY -:
" ) L Y0 ةيلاE Kه .ه ةJ1كملل Aا($Eا ةلاB Kه
" ) رفصلا Mوا3, ةم%لا Qناك اذH ةيلاE ةJ1كملا Nوك,و
ةلا1لا cGر,و 3M4;B ةمي8 ;R=3 و ةيلاE ةJ1كملا Qناك اذH
FA</3 ) Dلذ ري? Qناك اذH
" ) R%C ةJ1كملا Kه RgاJولا
" RيJولا _ون .A<=3 4ARAM3;3R Aا($Eا ةلاB اbنS
) R%C
"# ةلا1لا ةبا$ك
F!n+tion e",ty (:ta+&):Goolean 2
0egin
%$ )to, = # then
3",ty := tr!e
3le
3",ty := $ale 2
v
3nd2
"# K,wاك ةلا1لا Aا($Eا >$يو
%$ e",ty () then -rite(Hta+& i e",tyH) 2
ةلا1لا F!ll "#
" ) L Y0 ةoل$مم ةJ1كملل Aا($Eا ةلاB Kه
" @لا Mوا3, ةم%لا Qناك اذH ةoل$مم ةJ1كملا Nوك,و
MAX/;A?@ )
" ةلا1لا cGر,و F=<< ةمي8 ;R=3 ةJ1كملا Qناك اذH
و ةoل$مم FA</3 ) Dلذ ري? Qناك اذH
" @لا RgاJولا 4ARAM3;3R ) R%C ةJ1كملا Kه
" RيJولا _ون .A<=3 4ARAM3;3R Aا($Eا ةلاB اbنS
) R%C
F!n+tion $!ll (:ta+&):Goolean 2
0egin
%$ )to, = "axta+& then
F!ll := tr!e
3le
F!ll := $ale 2
3nd2
"# K,wاك ةلا1لا Aا($Eا >$يو
%$ $!ll () then -rite (Hta+& i $!llH) 2
ةلادلا stacktop
) ةJ1كملا ةم8 KC ةBوGوملا ةمي%لا ةعا(&ب Yو%,
F!n+tion ta+&to, (:ta+& ) : integer 2
0egin
%$ e",ty () then -rite (Hta+& i e",tyH)
3le
/ta+&to, := )ite" D)to,E 2
3nd 2
-: $ا%م
eرqا+ي ام ىلH Mرr'لا Yاx+لا نم B1ع .يوح$ب Yو%ي جمانرب F$ك0
X Mرrع KJا13لا Yاx+لا نم
لحلا
ة14ولا Y12$3ن ta+& ةيعرفلا جمار(لا .ك ىلع Mو$ح, K$لا
) ةJ1كملاب ةfا2لا
=e ta+& 2
0egin
y
Ariteln(H in,!t yo!r de+i"al n!"Ger H) 2
Read(n) 2
Re,eat
X := N "od 17 2
4!h (,x) 2
N := n di6 17 2
=ntil n = # 2
Ariteln(H yo!r n!"Ger in hex*de+i"al i H) 2
Ahile not e",ty () do
0egin
?ae ,o,() o$
1# : -rite (HAH) 2
11 : -rite (H0H) 2
12 : -rite (H?H) 2
18 : -rite (HCH) 2
1I : -rite (H3H) 2
19 : -rite (HFH) 2
ele -rite (,o,()) 2
end 2
end 2
readln 2
end )
"ار&'ا addtop
) ةJ1كملا KC نيرص+ع ىلع0 cمG وه ءارG^ا ا*ه ةفيqو
"# ةيم-Aاو2لا
O,
2
= ,o,()
%$ e",ty() then
3le"ent = tr!e
4!h( ,o,
2
)
3le
O,
1
= ,o, ()
3le"ent = $ale
4!h ( ,o,
1
+o,
2
)
ةJ1كملا Qناك اذH / # Kه

ءارG^ا ءا1ن NPC addto, # ةJ1كملاب K&',

z
ةلا1لا و0 ءارG^ا addta+&
) ةJ1كملا تايو$حم cمG Kه اb$فيqو
"# ةيم-Aاو2لا
Re,eat
Addto, 2
=ntil ele"ent 2
"# لا;م
ةJ1كملا Qناك اذH / "# Kه

ةلا1لا ءا1ن NPC addta+& "# ةJ1كملاب ى&',

نيرم,
ةيعرفلا جمار(لا F$ك0 addta+& , addto, ةيم-Aاو2لا ىلع ء:ا+ب
) ة%با3لا
( $)*ا +ي,-.لا/ ةسدكملا 0ل! تا1ي,-ت
1 @لاب ةبو$كم ةلBا'م ةمي8 BاIيH" ,ot$ix notation )
U @لH نم .يوح$لا " in$ix notation @لا ىلH ,o$ix notation )
Postfix notation -:
) ةيmايرلا ريبا'$لا ةبا$ك aيرl نم ة14او ر($',
"# 9
:
;م A + 0 F$ك, A 0 ) {
hي4 A 0 _ون نم ةAا(ع { 4ot$ix notation ى+'ملا iفن اbلو
ةلBا'ملل a+G
ةمي8 5ا34 KC ةJ1كملا Yا12$Jا نكمي A 0 5ا34 M0 {
4ot$ix Notation 2
ة%ير&ب ةبو$كم ةلBا'م ةمي8 5ا34 ةيم-AاوE 4ot$ix Notation "#
1 ةلBا'ملل ` K+يمي 7و8 dm0 " , )
U ةلBا'ملا رfا+ع جلاع " , ني,و&2لا ا
:
Aركم نيميلا ىلH Aا3يلا نم
V و v ) K+يميلا 7و%لا .ص, ى$4
V ةJ1كملا KC <'m .ما'م رص+'لا Nاك اذH " / )
v "# B ىلH 0 نم *فن = { ` Kبا34 6مA رص+'لا Nاك اذH "
ةJ1كملا KC نييول'لا نيرص+'لا k*40 / 0 / KC امb'm و
ري!$ملا O4
1
و O4
2 )
ةيبا3حلا ةيلم'لا MرG0 /5 O4
1
( + ) O4
2
)
) ةJ1كملا ةم8 ىلH = 5 ` ةو&2لا ةIي$ن dm0 /n
|
` ةو&2لا Kbنا /B v ) =
y ` ةو&2لا KC Aارك$لا Kbن0 " U ) =
z ةJ1كملا ةم8 Mوا3, 0 ةلBا'ملا ةمي8 .'G0 " / )
| ) ةياb+لا "
$ا%م ةلBا'ملا 5ا34 تاو&E }mوي # A 0 ةمي8 Qناك اذH { A =
9 و 0 = 8 2
1 U V
4!h ( / , A ) 4!h ( / , 0 ) O4
2
= 4O4(/ =
O4
2

z y v
O41 = 4O4(/) R3/=<; = O41 + O42 4=/>( /, R3/=<; =

-: $ا%م
ةمي8 F340 A0?+C
ةمي8 Qناك اذH A = 9 ] 0 = 8 ] ? = 1 ] C = 2 )
v V U 1
4=/> (/,A) 4=/>(/,0) 4=/>(/,? =

| z y
4=/> ( / , C (

نم ليوحتل !nfix notation "ل# Postfix notation $%
$% ليوحتل &'م()و*
JKLMNOKP MQRSK TUVK W JXRYZ[ J[\]U^ %n$ix )
1 * JKLMNOKP JRM_` aKb c ) c d JeSUOKP aKb c ( c fgh W )
~
2 * YiMQj kKMj W lP\ZmKP PnoYU^ TVOVKP aKb oMpVKP T^ 8 q 7 r\Us a]t
) JuoMv JeSUOKP
8 * T^ kKMNOKP YwQNKP rMx Pyb W aKb z{gh |^MN^ \} 4 ~]KP JKLMNOKP ~}d )
|•Os 4ot$ix )(
I * T^ kKMNOKP YwQNKP rMx Pyb W ) JeSUOKP aKb z{gh €oMpR •\‚
9 * T^ kKMNOKP YwQNKP rMx Pyb W *: (ƒ) d ( h ) lP\ZmKP „{` ~gMRo …^o
dh „V{Q]KP ~v ( + ) †\]pOKP ‡{` zK ~gMRo …^o |x JeSUOKP T^ ˆ„th ( h )
aKb z{ghd „V{Q]KP ~v zQ^ a‰jh 4 ) €oMpR •\‚ Šdh a]t
) JeSUO‰K ~gMRYKP …^YKP fgh( ƒ ) / ) ‹\ZmKP ~_`hd ( 9 ) (
7 * T^ kKMNOKP YwQNKP rMx Pyb W *: ƒ d h „{` ~QVOR •\‚ \}
JeSUOKP T^ ˆ„th ( h ) / aKb z{ghd ~[Mpt …^o |x 4 Šdh aKb |ws a]t
) €oMpR •\‚
) JeSUOKP T^ c ( c •\XKP ˆ„th (ƒ ) / aKb z{Œs • ( 4 ) ‹\ZmKP ~_`h Ž 7 (
)
: * ‹\ZmKP ~v oPYU]KP ~_`P 2 )
• * ) JRM_QKP
$% +,-م
JKLMNOKP Š\t A*(0+?)'C T^ %NF%X aKb 4O/;F%X )

1 2 8 I
4 = A 4 = A 4 = A 4 = A0
4=/> ( / , * ) 4=/> ( / , ? (

9 7 : •
4 = A0 4 = A0? 4 = A0? 4 = A0?+
4=/> ( / , + ) 4O4 ( / (

• 1# 11 12
4 = A0?+ 4 = A0?+ 4 = A0?+ 4 = A0?+C

4O4 ( / ) 4=/> ( / ( + ,

18 1I 19 17
P . A/C01 4=A0?+C' 4 = A0?+C' 4 = A0?+C '*
4O4 ( / ) 4O4 ( / (

$% ني),23
4 ' JKLMNOKP ƒMp‘[ ’‚ * A0 q ? (4O/;F%X NO;A;%ON (
“Vt ? = 2 , 0 = 8 , A = 1:
2 JKLMNOKP ƒMp‘[ ’‚ * A0?C + ' *
“Vt C = 18 , ? = 1• , 0 = 2 , A = 29
) JeSUOKP |U” M‘g\^
8 aKb |R\‘]KP JV^•oP\– Mn^Sm]p^ q 4O/;F%X NO;A;%ON
*: JVKM]KP l•LMNOKP |R\‘][ ’‚
( h A + 0 ' ?
) (ƒ A + 0) ' ?
) (— A ' 0 ( ? ) ( (A * C (
$%ني523
˜[ J[\]U^ JKLMN^ ƒMp‘[ ™\XR ~KM]KP ~jY{KP k^M`YšKP ,ot$ix k^M`YšKP „V{Q][ ’‚
) -اbIلا ىلع
‹oMšNKP ~QNs PyM^d .al!e :=Ord (+h)*ord(H# 2(H
4rogra" e6al!te 2
?ont
Maxarray = 9# 2
;y,e
Arr = array D 1))"axarray Eo$ +har 2
.ar
1€
X : arr 2
4oition : 1))"axarray 2
F!n+tion e6al ( x : arr ) :real 2
?ont
Maxta+& = "axarray 2
;y,e
/ta+& = re+ord
%te" : array D 1))"axta+& E o$ real 2
;o, : #))"axta+& 2
3nd 2
.ar
/t : ta+& 2
O,nd
1
, O,nd
2
, .al!e : real 2
?h : +har 2
4oition : 1))"axarray 2
F!n+tion ,o, (6ar :ta+&):real 2
› Gody o$ ,o, œ
4ro+ed!re ,!h (6ar :ta+&2x:real 2(
› Gody o$ ,!h œ
F!n+tion o,(+h:+har):Goolean 2
› Gody o$ o, œ
$!n+tion o,er (+h:+har 2o,
1
,o,
2
: real) : real 2
› Gody o$ o,er œ
Gegin œ$!n+tion e6al œ
t)to,:= # 2
,oition := 1 2
+h:=arrD,oition 2D
-hile +h • > H H do
Geginœ i $o!nd œ
i$ o,(+h)then
Gegin
6al!e :=ord(+h)*ord(H# 2(H
,!h(t,6al!e 2(
end
ele
Geginœo,erator i $o!nd œ
o,nd
2
:=,o,(t 2(
11
o,nd
1
:=,o,(t 2(
6al!e:=o,er (+h,o,nd
1
,o,nd
2
2(
,!h(t,6al!e 2(
end
i$ ,oition • "axarray then
Gegin
,oition:= ,oition + 1 2
+h:=arr D ,oition 2D
end
ele
+h 2 H H =:
end 2
e6al:= ,o,(t 2(
end 2
Gegin
-rite(Henter yo!r ,ot$ix ex,reion 2(H
$or i:= 1 to "axarray do
read ( arr D ,oition 2 ( D
readln 2
-riteln(H original ,ot$ix ex,reion i 2(H
$or ,oition:=1 to "axarray do
-rite ( arr D ,oition 2 ( D
-riteln(H6al!e o$ ,ot$ix i H,e6al(arr 2((
-riteln 2
end )
$% 678 9:,;ل ني52تل <=
1 * ةلا1لا O, L Y0 >8A .E1ملا 6مرلا .ه Aا($Eا ةلاB Kه
ةي%&+م ةIي$ن _اGAPب Yو%, hي4 tr!e و >8رلا ةلا4 KC Fale
) Dلذ k9E
Kلا$لا .كrلاب ةلا1لا F$كن N0 نكميو :
F!n+tion o, (+h:+har):Goolean 2
0egin
%$ (ord(+h)>=ord (H#H)) and (ord(+h)•= ord(H• ((H
;hen o,:=tr!e
3le o,:=$ale
1U
3nd >
2 * ةلا1لا 1Iن o,er Yو%, اbنPC KmايA 6مA رص+'لا Nاك اذH
) نييلا$لا نيرص+'لا ىلع ةيmايرلا ةيلم'لا ءارGPب
Kلا$لا .كrلا ىلع ةلا1لا ةبا$ك نكميC :
F!n+tion o,er (+h:+har 2o,1,o,2 : real ) : real 2
0egin
%$ +h in DH + H , H ' H , H * H , H ( H E then
?ae +h o$
: H+H o,er :=o,1 + o,2 2
: H'H o,er :=o,1' o,2 2
: H*H o,er :=o,1 * o,2 2
: H(H o,er :=o,1 ( o,2 2
end
ele
-riteln(Herror illegal o,erator 2 ( H
end 2
$% <8,-ل 9'?@تل
V " 5ا3حب Yو%ي جمانرب F$ك0 ةJ1كملا Yا12$Jاب
) 5ورZملا
v " "# ةJ1كملا Yا12$Jا Nو1ب جمانر(لا ةبا$ك L
:
و0
F!n+tion $a+torial ( n : integer ) : real 2
0egin
%$ n=# then $a+torial:=1
3le
Fa+torial := n'$a+torial ( n * 1 2(
3nd 2
&A6B2ل C6DتA,: Eم,85?ل &:,تF *:
$!n+tion $a+torial(n:integer ) : real 2
6ar
:ta+& 2
$a+t:real 2
Gegin
initialiže( 2 (
-hile (n>#) and (not $!ll()) do
Gegin
,!h(,n 2(
n:=n*1 2
end 2
1V
i$ (n>#) and ($!ll())then
Gegin
$a+torial:=*1 2
-riteln( H ta+& o6er $lo- 2 ( H
end
ele
Gegin
$a+t:= 1 2
-hile not e",ty() do
Gegin
,o,( 2 (
$a+t:=n'$a+t 2
end 2
$a+torial := $a+t 2
end 2
end 2
$%Gل,-ل 9'?@تل
™PSm]eM[ Y–Ÿ €LSj ™M` aKb €LSj ™M` T^ |R\‘]KP
) JeSUOKP
1 * ) ~¡MQ¢ aKb €Y£j T^
2 * ) ~`MO¢ aKb €Y£j T^
) €Y£j ~ePSe aKb €Y£j T^ *—
) €Y£j aKb ~¡MQ¢ T^ *L
) €Y£j aKb ~`MO¢ T^ *˜}
) €Y£j aKb €Y£j ~ePSe T^ *d
$% <H,IJ "ل# K5LM نم ليوحتل
$% &'م()وDل
1 * ’‚YKP a‰j LSNKP ’p‚h 2 ) JeSUOKP |–PL JOpXKP T^ ~‚MšKM[ ¤vLhd
2 * ) LSNKP rMU^ JOpXKP —oM– |N¥P
) ‹\ZmKP „V{Qs ~v YO]eP 1 ) d ( 2 ) Y{i €dMps JOp‚ —oM– a‰j |w‘` a]t (
8 * ) J”M£KP a‰j M_Nš¦hd JeSUOKP §RY{][ ’‚
$% لحل
ة14ولا Y12$3, /ta+& ةيعرفلا جمار(لا .ك ىلع Mو$ح, K$لا
ةJ1كملاب ةfا2لا )
1v
=e ta+& 2
0egin
Arite (Hin,!t yo!r de+i"al n!"Ger 2(H
Read (n 2(
Re,eat
X:=n "od 2 2
4!h (,x 2(
N:= n di6 2 2
=ntil n=# 2
Ahile not e",ty () do
Ariteln(,o,( 2 ((
Readln 2
3nd )
ىلع ةم3%لاب Yو%, Kنامt ىلH Mرrع نم .يوح$لا ةلا4 KC ~ L
:

>8رلا نم U )
$%ني523
1 * eريxن ىلH Mرr'لا >8رلا .يوح$ب Yو%ي جمانرب F$ك0
>8رلا ةx49م cم ) ةJ1كملا Yا12$Jاب Mرrع KJا13لا 1€
kر4 <لبا%ي A >8رلاو 11 @لا kر4 <لبا%ي 0 ) [لا ))) و
U " ) Mرr'لا eريxن ىلH Kنام;لا >8رلا لوحي جمانرب ةبا$كب >8
"# e1عا3م
ةلا1لا Y12$Jا Addto, ةلا1لا و Addta+& )
3و45لا NOPOPs
نكمي hيحب ةي&E ة%ير&ب jفح, رfا+'لا نم ةمgا8 وه dصلا
70رلا و0 ةم1%ملا ىم3ي 14او kرl نم رfا+'لا k*4
head,$ront ىم3يو رEwا kر&لا نم رfا+'لا ةCاmH >$ي ام+يب
.ي*لا و0 ةرEsملاب tail,rear @لاب ىم3يو F%FO )
` $irt in $irt o!t =
ا
:
Zي0 ةيلع aل&يو L
:
و0 nAا2لا وه L
:
و0 .Eا1لا $irt in $it er6i+e
) L
:
و0 Y12ي L
:
و0 YBا%لا
$% QوRSل Tو8U
1 * ~ZmKP fwKP linear W!e!e )
2 * €Y¡PSKP fwKP ?ir+!lar W!e!e )
$% QوRSل ل'-2تل V'W,Rم
1y
fw‰K JV¡PS][•P JKM‘KP ˆ¨]–M[ z[ JiMmKP lMV‰ONKP ©dY”d ˆ\{wKP |V•Os f‰]ms
*: JVKM]KP ’V}M{OKP |V•Os TUOVv
1 * ) Y{i €dMps ‹Y–ªOKP €dMps J^SXOKP JV¡PS][•P JKM‘KPd €Y¡PSKP fwKP
Rear = $ront = #
2 * €dMps ‹Y–ªOKPd StP\KP €dMps J^SXOKP JV¡PS][•P JKM‘KPd €Y¡PSKP fwKP
) Y{wKP
Rear = #
Front = 1
8 * ) Y{i €dMps ‹Y–ªOKP €dMps J^SXOKP JV¡PS][•P JKM‘KPd ~ZmKP fwKP
Rear = #
Front = #
I * €dMps ‹Y–ªOKPd StP\KP €dMps J^SXOKP JV¡PS][•P JKM‘KPd ~ZmKP fwKP
) Y{wKP
Rear = #
Front = 1
) Y{i €dMps ‹Y–ªOKP €dMps J^SXOKP M_Vv JV¡PS][•P JKM‘KPd €Y¡PSKP fwKP


$% 9:,;ل K5H6ل XSل ل'Yح3
1 * ) Y{wKP aKb rPYV£s ‹Y–ªOKPd J^SXOKP rb \} JV¡PS][•P JKM‘KP
2 * ) ~KM– ¤‚\^ aKb YV£s MnO¡PL ‹Y–ªOKP
8 * MOx ¤‚\OKP ‡{` aKb rPYV£s J^SXOKPd ‹Y–ªOKP «`Mx Pyb ~KM– fwKP r\UR
) Jp^MmKP JKM‘KPd aKd¬P JKM‘KP ~v
I * fwKP Š\¦ T^ StP\[ |‚h fwKP YiMQj LSj rMx Pyb -‰]O^ fwKP r\UR
) JQ^M•KP JKM‘KP ~v MOx ~‰UKP
9 * *: ®[MpKP €Y¡PSKP fwKP a‰j lMV‰ONKP ¯Y”
1 ) rPYV£s J^SXOKPd ‹Y–ªOKP JV¡PS][•P JKMt ~v fwKP aKd¬P JKM‘KP
) Y{wKP aKb
2 ) YwQNKP JvMgb «Os JV`M•KP JKM‘KP A «xYtd fwKP ‹Y–ª^ aKb
) ™M^°K ‹StPd ‹\Z– ‹Y–ªOKP
1z
8 ) YwQNKP JvMgb «Os J•KM•KP JKM‘KP 0 «xYtd fwKP ‹Y–ª^ aKb
) ™M^°K ‹StPd ‹\Z– ‹Y–ªOKP
I ) YwQNKP ˆ„t ’s JN[PYKP JKM‘KP A €Y¡PSKP fwKP J^SX^ T^
) ™M^°K ‹\Z– J^SXOKP «xYtd
9 ) YwQNKP ˆ„t ’s Jp^MmKP JKM‘KP 0 ±RY‘sd fwKP J^SX^ T^
) JVKM– «‘ših ~]KP J^SXOKP €dMps ‹Y–ªOKP MQ}) ™M^°K ‹\Z– J^SXOKP
7 ) YwQNKP JvMgb ’s JeLMpKP JKM‘KP A «xYtd fwKP ‹Y–ª^ aKb
) ™M^°K ‹StPd ‹\Z– ‹Y–ªOKP
: ) YwQNKP JvMgb «Os JN[MpKP JKM‘KP 0 «xYtd fwKP ‹Y–ª^ aKb
) ™M^°K ‹StPd ‹\Z– ‹Y–ªOKP
• ) YwQNKP JvMgb «Os JQ^M•KP JKM‘KP ? «xYtd fwKP ‹Y–ª^ aKb
) ™M^°K ‹StPd ‹\Z– ‹Y–ªOKP
$% K5H6ل XSل "YM Z,'Y2[ل Tو8U
1 * JvMg²P JV‰Oj %nert LPYOKP YwQNKP Y–³Pd fwKP Šd¬P TVZVed „–´sd
fw‰K z]vMgb
%nert(W,A 2 (
2 * ˆ„‘KP JV‰Oj Celete YwQNKP ˆ„t ’]R “Vt fwKP \}d StPd µVed „–´sd
J^SXOKP ~v L\¥\OKP
Celete(W 2(
$% XSل نM \]M^
|¶p[ fwKP |•O` rh TUOR JKM‘KP ·„} ~vd lMv\{wOKP ™PSm]eM[ fwKP |V•Os TUOR
‹Y–ªOKPd J^SXO‰K TV‰Xtd Jv\{wOKP |V•O]K |Xt a‰j €\]‘R rear , $ront “Vt
$ront d fwKP ~v Šd¬P YwQNKP ¤‚\^ aKb rear YwQNKP f‰– ¤XR ¤‚\OKP aKb
) JO¡PL J{w[ MnVKM– r\UR J`h €b ‹Y”Mš^ fwKP ~v YV–¬P
?ont
MaxW!e!e=1## 2
ty,e
W!e!e=re+ord
%te" :array D #)) MaxW!e!e*1E o$ integer 2
Rear , $ront :integer 2
3nd 2
.ar
W:W!e!e 2
&ل6ل P_pt` $%
1|
1 * µ¡Me\KP ,ara"eter ) µXv fwKP ~}
2 * µVe\KP ¸\` 6al!e ,ara"eter * Ž fwKP †\]‘^ a‰j Y¢ªR • oMš]–•P r¬
8 ) • ™h ~KM– fwKP rMx Pyb oMš]–P J{V¹\KP*
I ) ¤‚\OKP ‡{` aKb rPYV£s ‹Y–ªOKPd J^SXOKP rh \} ©Y£KP*
9 JVXZQ^ JOV‚ ¤¥Ys ~KM– fwKP rMx Pyb :JV^•oP\mKP* tr!e ¤¥Ys •b d $ale
*:JKPSKP J[M]x
F!n+tion e",ty( W:W!e!e):Goolean 2
0egin
%$ W)rear := W)$ront then e",ty := tr!e
3le
3",ty:= $ale 2
3nd 2
&ل6ل C6DتA P_pt` $%
ˆ„‘KP JV‰Oj rºv ~KM– fwKP rMx Pyºv ˆ„t JV‰Oj €h |š‚ fwKP oMš]–• —M]‘`
aOpR M^ aKb €Lªs =nder $lo- )
*: ~s³Mx k^M`YšKP |–PL M}»PS` TUOVv
%$ e",ty (W) then -riteln(HW!e!e e",ty 2 (H
&ل6ل fOaa $%
1 * P
˜KP µ¡Me\K ,ara"eter ) µXv fwKP ~}
2 * `
~} µVe\KP ¸\ 6al!e ,ara"eter ) fwKP †\]‘^ a‰j Y¢ªR • oMš]–•P r¬
8 * P
) • ™h -‰]O^ fwKP rMx Pyb oMš]–P J{V¹\K
I * P
rh \} ©Y£K rear +1 "od "axW!e!e = $ront )
9 * P
JVXZQ^ JOV‚ ¤¥Ys ©Y£KP ®X‘s Pyb*: JV^•oP\mK tr!e •bd $ale )
JKPSKP J[M]x F=<< *:
F!n+tion $!ll(W:W!e!e):Goolean 2
0egin
Aith W do
%$ rear+1 "od "axW!e!e = $ront then $!ll :=tr!e
3le
F!ll := $ale 2
3nd 2
&ل6ل C6DتA fOaa % *
1~
€h JvMgb rºv -‰]O^ fwKP rMx Pyºv JvMgb JV‰Oj €h |š‚ fwKP oMš]–• ™Sm]ps
aOpR M^ aKb €Lªs YwQj o6er $lo- ) ( rMŒV{KP )
*: ~s³Mx k^M`YšKP |–PL M}»PS` TUOVv
%$ $!ll (W) then -riteln (HW!e!e i $!ll 2(H
b5c^ insPdt $%
1 * P
˜KP µ¡Me\K ,ara"eter ) z]vMgb LPYOKP YwQNKPd fwKP MO}
2 * `
fwKP )µ¡Me\KP ¸\ 6ariaGle ,ara"eter †\]‘^ T^ YV¼s ˆ\e JvMg²P r¬
YwQNKP MOQV[ fwKP 6al!e ,ara"eter ) (
8 * P
) fwKP ‹Y–ª^ aKb YwQNKP ¤vS[ »PY¥²P ™\XR J{V¹\K
I * P
*: JV^•oP\mK
) ƒ d h „{` •bd ´Z– JKMeo ~Zjh -‰]O^ fwKP rMx Pyb Yš]–P *
) fwKP ‹Y–ª^ aKb YwQNKP SQeh *h
) ‹StPd oPSXO[ ‹Y–ªOKP ±RY‘][ ’‚ *ƒ
$% b5c^ &:,تF
4ro+ed!re inert (6ar W:W!e!e 2 x : integer 2(
0egin
%$ $!ll (W) then
Ariteln (HW!e!e i $!ll (H
3le
0egin
W)ite" DW)rearE:= x 2
W)rear := W)rear +1 "od "axW!e!e 2
3nd 2
3nd 2
&ل6ل dPaPtP $%
1 * P
˜KP µ¡Me\K ,ara"eter ) µXv fwKP \}
2 * `
: µVe\KP ¸\ 6ariaGle ,ara"eter fwKP †\]‘^ T^ YV¼R ˆ\e ˆ„‘KP r¬
1•
8 * P
) fwKP J^SX^ ~v L\¥\OKP YwQNKP ˆ„t : J{V¹\K
I * P
*: JV^•oP\mK
) ƒ d h „{` •bd ´Z– JKMeo ~Zjh ~KM– fwKP rMx Pyb Yš]–P *
) J^SXOKP ~v L\¥\OKP YwQNKP ˆ„th *h
) StPd oPSXO[ J^SXOKP ±RY‘][ ’‚ *ƒ
*: JKPSKP J[M]x
F!n+tion delete(6ar W:W!e!e):integer
0egin
%$ e",ty (W) then -riteln(HW!e!e i e",ty (H
3le
0egin
Celete :=W)ite"DW)$ront 2D
W)$ront :=W)$ront +1 "od "axW!e!e 2
3nd 2
3nd 2
b5c^ !nitiaaieP $%
1 *
µVe\KP ,ara"eter ) µXv fwKP \}
2 *
µVe\KP ¸\` 6ariaGle ,ara"eter )
8 *
JKM‘KP aKb M_jM¥ob €h fwKP lMR\]‘^ ½PYvb J{V¹\KP
) JV¡PS][•P
I *
) Y{wKP aKb rPYV£s ‹Y–ªOKPd J^SXOKP |N¥ \} ©Y£KP
4ro+ed!re initialiže ( 6ar W : 5!e!e 2(
0egin
W)$ront:= # 2
W)rear:= # 2
3nd 2
U€
b5c^ dispaa` $%
1 * Yj
) fwKP lMR\]‘^ ¾
2 * Y£KP
) fwKP ~v J¿‰]OOKP lM`MmKP LSN[ JRoPYUs zX‰t : ©
8 * P\mKP
JRoPYUs JX‰‘[ ’]s YwQNKP JjMš¦ rºv ‹Y–ªOKP T^ Yšxh J^SXOKP «`Mx Pyb JV^•o
) JVK•MQs $or do-nto ) JRSjMws JRoPYUs JX‰‘[ ’]s YwQNKP JjMš¦ •bd ( $or*
to ) (
˜KP : µVe\KP ,ara"eter ) µXv fwKP \}
I * ¸\`
µVe\KP 6al!e ,ara"eter ) fwKP lMR\]‘^ a‰j Y¢ªs • JjMšZKP r¬
*: k^M`YšKP J[M]x
4ro+ed!re di,lay (W:W!e!e 2(
.ar
%:integer 2
0egin
%$ W)$ron > W)rear then
For % := W)$ront do-nto W)rear do
Arite ( W)ite"Di 2 ( H H,D
3le
$or % :=W )$ront to W)rear do
-rite (W)ite"Di 2 (H H, D
end 2
U1
<@Dل XSل
) YwQNKP €dMps ‹Y–ªOKPd StP\KP €dMps J^SXOKP JV¡PS][•P JKM‘KPd ~ZmKP fwKP
*: z¦dY”
1 JV¡PS][•P JKM‘KP * rear=# , $ront = 1 )
2 ‹Y–ªOKP T^ Yšxh J^SXOKP «`Mx Pyb ~KM– fwKP r\UR * rear • $ront )
8 ) fwKP Š\¦ €dMps ‹Y–ªOKP «`Mx Pyb -‰]O^ fwKP r\UR *
Rear = "axW!e!e
) ®[MpKP ~ZmKP fwKP ©dY” Àg\s JVKM]KP JOeYKPd
) µXv YiMQj JpO– a‰j €\]‘R fwKP Š\¦


$%<@Dل XSل "YM Z,fg]م
1 * Pyb
) µXv StPd YwQj a‰j €\]‘R fwKP rºv ‹Y–ªOKP J^SXOKP ldMe
2 * TUOR
) JX[MpKP JKM‘KP ~v MOx JVKM– lM`M– ±KMQ} r\Usd »¨]^•P ©Y” ®X‘]R rh
8 * ~v
YwQNKP ˆ„t ˆ„‘KP ~v MOQV[ JV`M¢ YwQNKP ŠM–Lb ’¢ •n dh ‹Y–ªOKP ÁY‘s JvMg²P
)J^SXOKP ÁY‘s ’¢ •n dh
I * ªOKP
) fwKP lMQ¡Mx |x Š¨¼]eP TUOR ±K„[d ~KM– rMU^ aKb YV£s • ‹Y–
$%<@Dل XSل نM \]M^
T^ hSšs Jv\{wOKP Š\¦ JKM‘KP ·„} ~v µXv €Y¡PSKP fwKP Tj r¨j²P ‡{` \}
)Y{wKP T^ •n S[ StP\KP
UU
?ont
MaxW!e!e = 1## 2
;y,e
W!e!e = re+ord
%te":arrayD1))"axW!e!eE o$ integer 2
Rear , $ront : integer 2
3nd 2
.ar
W:W!e!e 2
&ل6ل hOaa $%
F!n+tion F!ll (W : W!e!e ):0oolean 2
0egin
%$ W)rear = "axW!e!e then $!ll := tr!e
3le
F!ll :=$ale 2
3nd 2
: †Y–h JXRYZ[ JKPSKP J[M]x TUORd
F!n+tion $!ll (W:W!e!e):Goolean 2
0egin
F!ll:=W)rear = "ax 2
3nd 2
&ل6ل P_pt` $%
F!n+tion e",ty(W:W!e!e):Goolean 2
0egin
%$ W)$ront > W)rear then e",ty := tr!e
3le
3",ty := $ale 2
3nd 2
$% &'ل,تل Z),?[ل,: &ل6ل &:,تF نB2يi
F!n+tion e",ty (W:W!e!e):Goolean 2
0egin
3",ty := (W)$ront=1) and (W)rear = # 2(
3nd 2
&ل6ل dPaPtP $%
$!n+tion delete (6ar W:W!e!e):integer 2
Gegin
i$ e",ty (W) then -rite (HW!e!e i e",ty (H
UV
ele
Gegin
delete :=W)ite"DW)$ront 2 D
W $ront :=W)$ront + 1 2
3nd 2
%$ W)$ront >W)rear then initialiže (W 2 (
3nd 2
$% &jوحYم
fwKP ¸M¥oº[ ™\X` JX[MpKP JVKMmKP lM`MmKP T^ ‹LM{]e¨K
) MnuoMv fwKP Àših M^ a]^ JV¡PS][•P JKM‘KP aKb
b5c^ initiaaieP
,ro+ed!re initialiže(6ar 5:5!e!e 2(
Gegin
5)rear:=# 2
5)$ornt:=# 2
end 2
% kلوYحم &Y-مU
) 1 ‹Y–ªOKPd J^SXOKP JV¡PS][•P JKM‘KPd €Y¡PSKP fwKP |Oj axM‘R k^M`Y[ Â]xP
) Y{wKP rMRdMps
Rear=$ront=#
* 2 J^SXOKP JV¡PS][•P JKM‘KPd aZmKP fwKP |Oj axM‘R k^M`Y[ Â]xP
Y{wKP †dMps ‹Y–ªOKPd StP\KP †dMps rear=# , $ront=1 )
* 8 )Jv\{wOKP ~v LS‘^ YwQj Tj “‘šKM[ ™\XR k^M`Y[ Â]xP
+,-م
)TV]v\{w^ k^S[ ™\XR k^M`Y[ Â]xP
TVZ– TV{i k^L zV^o•P\–
T^ |‚P( ‹Y¶‘KP ’‚o) Šd¬P fwKP ‹Y–ª^ ¤‚\^ q Šd¬P fwKP Š\¦ rMx Pyb
)•Ÿd k^SKP zV`MU^P ™SN[ JKMeo aZjh ~`M•KP fwKP Š\¦
1 ~`M•KP fwKP ‹Y–ª^ aKb ~`M•KP fwKP J^SX^ T^ oYx *
1 *
)Šd¬P fwKP J^SX^ ÁYt
2 *
)Šd¬P fwKP J^SX^ ~Kb LPSNKP YwQj SQeP
* 2 ) MÃuoMv ~`M•KP fwKP |N¥P
%Eم,85?ل
Uv
4ro+ed!re add5!e!e(6ar W1:5!e!e126ar W2:5!e!e2 2(
.ar
%:integer 2
0egin
%$ (Max5!e!e1*(W1)rear)•"ax5!e!e2)then
Arite(H+an not add5!e!e 2(H
3le
For %:=W2)$ront ;o W2)rear do
0egin
W1)rear:=W1)rear+1 2
W1)ite"DW1)rearE:=W2)ite"D% 2D
3nd 2
%nitialiže(W2 2(
Readln 2
end 2
%+,-م
aZmKP fwKP ~v YwQj Tj “‘šKM[ ™\XR k^M`Y[ Â]xP l
"@Dل XSل <= Gح?ل &'Y2M *:
4ro+ed!re ear+h(5:5!e!e2target:integer 2(
0egin
Ahile(5)$ront•=5)rear) do
0egin
%$ target=5)ite"D5)$rontE then -riteln(target,Ä i $o!nd (Ä
3le
5)$ront:=5)$ront+1 2
end 2
i$ 5)$ront>5)rear then -riteln(target,Ä i not $o!nd 2(Ä
end 2
$%<3m,: Cوn8 9:,;ل b5c^ ob,RF op,يqل
k^M`YšKP „–´R • a]t k^M`YšKP T^ —Ym` ’¢ ±K„[ JKMeo aZN` ˆS_KP a‰j o\•NKP SQj
) “‘šKP JV‰Oj ~v YšxP T^•
)“‘šKP JV‰Oj ~v oPYO]e¨K z¥\t ±KMQ} ‡V‰v ˆS_KP a‰j o\•NKP ’s Pyb z`¬
$%<3m,F Eم,85?ل +6['=
%$ target=5)ite"D5)$rontE then
Ariteln(target,Ä i $o!nd 2(Ä
Readln 2
3xit 2
Uy
3nd
3le
5)$ront:=5)$ront+1 2
‹oMšNKMv exit )k^M`YšKP T^ —dYm‰K †Lªs
Z5rs2ل PointPds
YV£s ~_v k^M`YšKP „V{Qs »MQ¢h ·YxP…KP lPY¶t aKb Š\i\KP TUOs lPYV¼]^ ~}
JxY‘]OKP lM`MVšKP zVQ[ ~v ™Sm]ps ~_v ·YxP…KP ~v TV`P\Qj aKb MÃO¡PL
) Cyna"i+ data ty,e )(
) PSXj aOps YiMQNKP T^ zj\O¶^ Tj ‹oMšj JxY‘]OKP lM`MVšKP JVQ[d node
)lPY”ªOKP JZeP\[ ÅNšKP M_ŒN[ ¤^ JZšsY^ l¨¶e JXVX‘KP ~v a}d
&F5حت2ل &F5حت2ل Z,8,'?ل k'I: Zq'2م
1 *
) k^M`YšKP „V{Qs »MQ¢h ÆOUQsd LSO]s rh TUOR
t $ K
z‰U£^ ÇS‘sM o6er$lo- ) ·LS‘^ YVu Jt\]{^ r\Us ·YxP…KP ~v JtMpOKP r•
%Z5rs2ل نM \]M^
.ar
4ointerÈ6ariaGle:Édata ty,e 2
dh
;y,e
4ointerÈ6ariaGle=Édata ty,e 2
:J‰•^h
;y,e
NodeÈ,ointer=Énode 2
Node=re+ord
Na"e:tring 2
Age:integer 2
3nd 2
.ar
4,R,W:nodeÈ,ointer 2
Uz
X:Éinteger 2
B:Étring 2
o),?[ل NPu
) k^M`YšKP „V{Qs »MQ¢h ·YxP…KP ~v TR…ms JtMp^ ÊVwms ™\Xs
%&م,[ل &v'Sل
Ne-(4ointerÈ6ariaGle 2(
:ŠM•^
Ne-(, 2(
Ne-(W 2(
Ne-(R 2(
*:~s³Mx ·YxP…KP ~v JX[MpKP ‹oMšNKP a‰j YVšN]KP TUOR
Ne-(, (
Ne-(W (
Ne-(R (
Z5rs2ل p,IA#
Y–Ÿ Y”ª^ zVKb YV£R €„KP rMUOKP ~Kb Y”ªOKP YV£R rh \}
:¨Ã •Ov
4:=W 2
U|
Na"e
Age
R
e"aN
egA
W
e"aN
egA
4
Y”ªOKP rh aQNR P„_v 4 zVKb YV£R €„KP ¤‚\OKP ‡{` aKb Y”ªR ÀšiP
YӻOKP W )
w6n[ل +وng xم لم,[تل
) ·SXNKP ~v TVN^ |X‘K Š\i\‰K J^MNKP J¼VwKP ,ointerÈna"eÉ)$ield (
|¶pKP ~v ’e•P |X‘K Š\i\‰K ¨Ã •^ 4 Â]U` 4É)na"e
|¶pKP ~v YONKP |X‘K Š\i\‰Kd 4 Â]U` 4É)age
% +,-م
ty,e
node,ointer =Énode 2
node=re+ord
na"e:tring 2
age:integer 2
end 2
6ar
4,R,W:node,ointer 2
0egin
Ne-(4 2(
Ne-(W 2(
Ne-(R 2(
4É)na"e:=Hali 2H
WÉ)na"e:=Hah"ed 2H
4É)age:=29 2
WÉ)age:=:9 2
4:=W 2
R:=4 2
Ariteln(4É)na"e,4É)age,WÉ)na"e,RÉ)na"e 2(
3nd )
*: ~s³Mx r\UR k^M`YšKP —Y– rMv
Ah"ed :9 Ah"ed Ali
U~
o),?[ل 1isposP
) ‹YxP„KP ~v Y”ªO‰K •\¶‘OKP rMUOKP ÊVwms »M_`º[ ™\XR »PY¥b \}
&م,[ل &v'Sل
Ci,oe(,ointerna"e 2(
&2'nل Nia
) »a”¨KP rP\Qj \}d ‹YxP„KP ~v JOV‚ |•Os
% ني523
lPY”ªOKPd JVKM]KP J‰w]OKP J‰p‰pKP 41,42,48,4I
Pyb »MZmKP Àgdd zV‰Oj €b »PY¥b ÅN[ J‰p‰pKP ’eo SjP
) lMV‰ONKP ~v »MZ– S¥d
1 ) 4I:=41É)data 2
t l 4IÉ)data:=41É)data2
4IÉ)next:=41 2
8 ) 4IÉ)next:=48É)next 2
48É)next:=42É)next 2
42É)next:=41É)next 2
I ) 42É)next)48É)data:=4É)data 2
U•
41 4I 48 42
rat
+at
e-e dog head
&YSت2ل VHوnل ainkPd aist y&nYvI2ل &حH]لz
)M_V‰s ~]KP ·SXNKM[ JZšsY^ ·SXj |x rh “‘[ SXNKP T^ z‰p‰e Tj ‹oMšj ~}
•hYKP aOps aKd¬P ·SXNKP Y”ª^d head aKb YV£s ·YV– •h ·SXNKPd nil
) zVKM– J‘¡• ˆY¦ a‰j |w‘` ±K„[d
|˜˜Xt T˜˜V‰Xt T˜˜^ r\˜˜U]R |¶˜˜e µ˜˜p[Pd J˜˜X‰¼QOKP À¡P\˜˜‰KP »MQ[ ~v l¨¶pKP ™Sm]ps
) ~KM]KP |¶pKP aKb Y”ª^ Y–Ÿ |Xtd lM`MVš‰K
’˜˜`d |V¼˜˜£]KP ’˜˜`d lM˜˜O¥Y]OKP ~˜˜v ŠM˜˜•OKP |Vše a‰j JX‰¼QOKP À¡P\‰KP ™Sm]ps
)lM`MVšKP SjP\‚ ‹oPLb
l ن'Yng نم k8وBم w6nM نM \]M^
;y,e
<,tr=Élitnode 2
<itnode=re+ord
Cata:integer 2
Next:l,tr 2
3nd 2
&nYvI2ل &حH]ل b,L8#
) JX‰¼QOKP J‘¡¨KP »M£`² JV^•oP\mKP
4 $
) ¸\` T^ Y”ª^ »M£`b <,tr
t $
) aKb YV£R Y”ªOKP |N¥P Nil
F!n+tion +reatlit:l,tr 2
.ar
4:l,tr 2
V€
0egin
Ne-(, 2(
?reatlit:=, 2
3nd 2
JKPSKP »MjS]eM[ ™\X` JX‰¼QOKP J‘¡¨KP »M£`²d +reatlit k^M`YšKP ~v
) ~pV¡YKP
) Â]UQv head:=+reatlit 2
) ˜KP aKd¬P ·SXNKP Y”ª^ »M£`b ’]Vv head
‹oMšNKP ÅN[ ~s³Mx ‹YxP„KP ~v Y”ªOKP |U” ’eo TUORd
) JX[MpKP
JKPSKP ®RY¦ Tj ·SRS¥ ·SXj »M£`º[ ™\X` MQ`ºv J‘¡¨K ·SXj €Ÿ JvMg²d getnode
¸\QKP r\Us ‹SRS¶KP ·SXNKP ·„}d litnode ¸\QKP T^ Y”ªO[ M_VKb YV£`d ne-l,tr
|X‘KP zOV‚ PYX`d data Y”ªOKP |N¶`d next ) JRM_QKP aKb YV£R nil
F!n+tion getnode:l,tr 2
.ar
Ne-l,tr:l,tr 2
.al!e:integer 2
0egin
Ariteln(Henter integer 6al!e to add to the lit 2(H
Read(6al!e 2(
Ne-(ne-l,tr 2(
Aith ne-l,trÉ do
0egin
Cata:=6al!e 2
Next:=nil 2
3nd 2
Fetnode:=ne-l,tr 2
3nd 2
%w6ي6c w6nM &=,{#
l &=,{^ &'م()و*
4 $ £`P
¸\˜˜QKP T˜˜^ ·S˜˜RS¥ ·S˜˜Xj »a litnode a˜˜Kb z]vM˜˜gb LPY˜˜OKP LS˜˜NKP a˜˜‰j †\˜˜]‘s
) J‘¡¨KP
V1
head
nil
2 * Pyb
‹SRS¶KP ·SXNKP fgh aKd¬P ·SXNKP ~v LSNKP T^ Y¼iP ‹SRS¶KP ·SXNKP ~v LSNKP rMx
) aKd¬P ·SXNKP |š‚
8 * Pyb
) Y˜˜¥P a˜˜Kd¬P ·S˜˜XNKP ~v LSNKP T^ YšxP ‹SRS¶KP ·SXNKP ~v LSNKP rMx 2 ·S˜˜XNKP ¤˜˜^ (
) J‘¡¨KP JRM_` aKb |ws ’KM^ JVKM]KP
I * Pyb
) J‘¡¨KP Y–Ÿ ~v ‹SRS¶KP ·SXNKP fgh J‘¡¨KP JRM_` «‰id
JvMg²P zKPL J[M]x TUOR ~sP„KP »PSQKP ™PSm]eM[ %<3m,F
4ro+ed!re add(6ar head,ne-,tr:l,tr 2(
.ar
;e",:l,tr 2
0egin
%$ head=nil then head:=ne-,tr
3le i$ (ne-,trÉ)data=headÉ)data) then -riteln(Hthi 6al!e i
already in the lit (H
3le i$ (ne-,trÉ)data•headÉ)data)then
0egin
;e",:=head 2
>ead:=ne-,tr 2
Ne-,trÉ)next:=te", 2
3nd
3le i$
(ne-,trÉ)data>headÉ)data)and(ne-,trÉ)data•headÉ)nextÉ)data)then
0egin
;e",:=headÉ)next 2
>eadÉ)next:=ne-,tr 2
Ne-,trÉ)next:=te", 2
3nd
3le
VU
Add(headÉ)next,ne-,tr 2(
3nd 2
&nYvI2ل &حH]ل <= Q6|ل نM Gح?ل %
% Gح?ل &'م()و*
4 $ Pyb
JOV‚ aZjh SXNKP T^ zVKM– J‘¡¨KP «`Mx nil TRY”ªO‰K target,,red
t $ a˜˜Kd¬P ·S˜˜XNKP ~˜˜v LS˜˜NKP †dMpR ˆS_KP rMx Pyb aKd¬P ·SXNKPd ˆS_KP TV[ roM‚
Y”ªOKP ¸L target |N¥Pd M_VKb YV£R ,red aKb YV£R nil
} $ Pyb
, J˜˜VKM]KP ·SXNKP ¤^ z]`oMXOK |X]`P aKd¬P ·SXNKP ~v LSNKP JOV‚ †dMpR • ˆS_KP rMx
Y˜˜”ªOKP ¸L JVKM]KP ·SXNKP ~v LSNKP JOV‚ †dMpR ˆS_KP rMx Pyb target M˜˜_VKb YV˜˜£R
Y”ªOKP ¸Ld ,red ) M_‰š‚ ~]KP ·SXNKP aKb YV£R
~ $ ) ‹\ZmKP oYx JVKM]KP ·SXNKP ~v (LSNKP)|X‘KP JOV‚ †dMpR • ˆS_KP rMx Pyb 8 )(
• $ b
JOV‚ aZjh ˆS_KP S¶s ’Kd J‘¡¨KP JRM_` aKb «‰id Py nil TRY”ªO‰K
target,,red )
Eم,85?ل
4ro+ed!re ear+h(head:l,tr2x:integer26ar target,,red:l,tr 2(
0egin
%$ head=nil then
0egin
Ariteln(x,Hi not $o!nd 2(H
;arget:=nil 2
4red:=nil 2
3nd
3le i$ headÉ)data=x then
0egin
;arget:=head 2
4red:=nil
3nd
3le i$ headÉ)nextÉ)data=x then
0egin
;arget:=headÉ)next 2
4red:=head 2
VV
3nd
3le i$ (x>headÉ)data)and(x•headÉ)nextÉ)data)then
0egin
Ariteln(x,Hi not $o!nd 2(H
;arget:=nil 2
4red:=nil 2
3nd
3le ear+h(headÉ)next,x,target,,red 2(
3nd 2
l &nYvI2ل &حH]ل نم w6nM Q€g
»PY¥²M[ ·SXNKP ±‰s Tj “‘šKP JV‰ON[ ™\X` J‘¡¨KP ~v ·SXj ˆ„‘K ear+h
|˜˜š‚ M˜˜^ aKb YV£s JV`M•KPd M_v„t LPYOKP ‹SXNKP aKb YV£s ·StPd TRY”ª^ ¤¥Ys ~]KP
) M_v„t LPYOKP ·SXNKP
‡˜˜{` a˜˜Kb YV˜˜£R M_v„t LPYOKP ·SXNKP |š‚ Y”ªOKP |N¥ \} TRY”ª^ L\¥\K ¾Y¼KP
LPY˜˜OKP ·S˜˜XNKP ˆ„˜˜t aQp]R a]t M_v„t LPYOKP ·SXNKP Y”ª^ M_VKb YV£R €„KP rMUOKP
JKPSKM[ M_v„t di,oe )
%&nYvI2ل &حH]ل نم w6nM Q€g &'م()و*
4 $ ]˜˜˜eP
TRY”ªOKP ’V‚ SRS‘s ~v “‘šKP k^M`Y[ ™Sm target,,red )
t $ Pyb
Y”ªOKP rMx target aKb YV£R nil M_v„t LPYOKP ·SXNKP L\¥d ™SN[ JKMeo ¤š¦P
)
} $ Pyb
Y”ªOKP rMx target aKb YV£R • nil Y”ªOKP MOQV[) ,red aKb YV£R nil M^ Y¥P
)( aKd¬P ·SXNKP ~} M_v„t LPYOKP ·SXNKP rh aQNR P„}d) ~‰R
Y”ªOKP ¸L*h head )JV`M•KP ·SXNKP aKb YV£R
JKPSKP ™Sm]eP*ƒ di,oe ) aKd¬P ·SXNKP »M¼K²
~ $ Pyb
rPY”ªOKP rMx target,,red aKb rPYV£R • nil )~‰R M^ Y¥P
4 l ˜˜N¥P
| ,redÉ)next=targetÉ)next )
t l ]˜˜˜eP
JKPSKP ™Sm di,oe ˜[ M_VKb oM£OKP ·SXNKP »M¼K² target )
l 9YvIم &حH• نم w6nM Q€g Eم,85:
4ro+ed!re delete(6ar head:l,tr2x:integer 2(
Vv
.ar
;arget,,red:l,tr 2
0egin
/ear+h(head,x,target,,red 2(
%$ target=nil then -riteln(Hthi node i not $o!nd (H
3le i$ (target=head)and(,red=nil)then
0egin
>ead:=headÉ)next 2
Ci,oe(target 2(
3nd
3le i$ targetÉ)next=nil then
0egin
4redÉ)next:=nil 2
Ci,oe(target 2(
3nd
3le
0egin
4redÉ)next:=targetÉ)nextr 2
3nd 2
3nd 2
&nYvI2ل &حH]ل Z,يوتحم &M,?‚ %
lMR\]‘^ JjMšZ[ ™\X` J`…‘OKP lM`MVšKP ¸M¥Y]e• J¥\‘KP SQj
)JX‰¼QOKP J‘¡¨KP
:JX‰¼QOKP J‘¡¨KP lMR\]‘^ JjMš¦ JV^•oP\–
Y”ªOKP rh MOKM¦ target †dMps • nil *:a‰RM^ Y¥P
1 ˜KP JOV‚ ¤š¦P) headÉ)data )
2 |N¥P) head=headÉ)next )
JX‰¼QOKP J‘¡¨KP lMR\]‘^ JjMšZK k^M`Y[
4ro+ed!re ,rintlit(head:l,tr 2(
0egin
Ahile head•>nil do
0egin
Vy
Ariteln(headÉ)data 2(
>ead:=headÉ)next 2
3nd 2
3nd 2
ƒل „'35تل Sodt
·„} ÂVsY]K ‹SRSj ËY¦ ±KMQ} rMv JQVN^ Jv\{w^ ~v YiMQNKP T^ Jj\O¶^ MQRSK rMx Pyb
)MVK•MQs dh MRSjMws YiMQNKP
ÂVsY]KP lMV^•oP\–
*:~‰–PSKP ÂVsY]KP)h
1 * ¸MX{KM[ ÂVsY]KP G!GGle ort
2 * oMV]–•M[ ÂVsY]KP ele+tion ort
8 * ŠM–L²M[ ÂVsY]KP inertion ort
I * ¤RYpKP ÂVsY]KP 5!i+& ort
9 * SVXOKP ÂVsY]KP hea, ort
7 * J¡…¶]KM[ ÂVsY]KP hell ort
*: ~¥oMmKP ÂVsY]KP)ƒ
k^SKM[ ÂVsY]KP \}d StPd ¸\` zVvd "erge ort
Vz
)k^PYšKP ‹»M{x ‹LMR• ~} lMV^•oP\mKP LSNs T^ ˆS_KP
T,nRل,: „'35تل …O……aP sodt
)Jv\{wOKP a‰jh ~v Jv\{wOKP YiMQj Y¼iP \{Zs ¸\QKP P„} ~v
Jv\{w^ MQRSK rh ¾Y{` A M}YiMQj LSjd N *: ~s•Ÿ ¤š]` ÂVsY]KP T^ ¸\QKP P„} ~v
1 ÂVsY]˜˜KP ~˜˜v zN˜˜Œ` “˜˜V‘[ ~`M˜˜•KP Y˜˜wQNKP ¤˜˜^ Šd¬P YwQNKP roM‚) AD1E•AD2 ’˜˜¢ D
ÂVsY]˜˜KP ~v ’_NŒ`d “KM•KP ¤^ ~`M•KP YwQNKP roMX` AD2E•AD8 J˜˜V‰ONKP ·„˜˜} YO]˜˜psd D
TRYwQj Y–Ÿ a]t ADN*1E•ADN Y˜˜wQj Yš˜˜xP r\˜˜UR ‹\ZmKP ·„} T^ »M_]`•P SQjd D
ÂVsYs Y–Ÿ ~v ¤gd S‚ ADN ) D
2 ) ‹\ZmKP oYx) 1 Yš˜˜xP \˜˜} r\˜˜UR M}S˜˜Qjd YV–¬P |š‚ YwQNKP SQj J`oMXOKP a_Qs “V‘[ (
YwQNKP SN[ Jv\{wOKP ~v YwQj ADN ) D
8 )‹\˜˜˜ZmKP oY˜˜˜x) 2 ( N*1 ~`M˜˜˜•KP ¤˜˜˜^ Šd¬P Y˜˜˜wQNKP J˜˜˜`oMXOK |˜˜˜w` a]˜˜˜t ‹Y˜˜˜^
AD1E•AD2 )«šsYs S‚ Jv\{wOKP r\Us M}SQjd D
$%T,nRل,: Gح?ل &'م()و*
1 TVs\ZmKP oYx) 2 d 8 “V‘[ @= 1 ;O N*1
2 |N¥P) Ì=1
8 “V‘[ ƒdh TVs\ZmKP oYx) Ì•=N*@
rMx Pyb ADÌE>ADÌ+1 )ÅN[ ¤^ TRYwQNKP ŠS[ D
|N¥P)ƒ Ì=Ì+1
I ‹\ZmKP ~v oPYU]KP »M_`b) 8 )
9 ‹\ZmKP ~v oPYU]KP »M_`b) 1 )
)lP\ZmKP ·„} MQK Àg\R ~KM]KP |U£KPd
9 I 1: • 12 :
1: 9 I 12 • :
1: 12 9 I • :
1: 12 • 9 I :
V|
AD1 E IEAE 8EAE 9EAE 2EAE 7EAE
1EAE IEAE 8EAE 9EAE 2EAE 7EAE
1EAE IEAE 8EAE 9EAE 2EAE 7EAE
1EAE IEAE 8EAE 9EAE 2EAE 7EAE
1EAE IEAE 8EAE 9EAE 2EAE 7EAE
AD1 D
AD1 D
AD1 D
AD1 D
1: 12 • : 9 I
4rogra" G!GGle 2
?ont
N=1# 2
.ar
A:array D1))nE o$ integer 2
%,j,&,te",:integer 2
0egin
For %:=1 to n do
Read(aD% 2(D
For &:=1 to n*1 do
0egin
Ì:=1 2
Ahile j•=(n*&) do
0egin
%$ aDjE>aDj+1E then
0egin
;e",:=aDj 2D
ADjE:=aDj+1 2D
ADj+1E:=te", 2
3nd 2
Ì: =j+1 2
3nd 2
3nd 2
For %:= 1 to n do
Arite(aD% 2(D
3nd )
LSj aKb —M]‘` JXRYZKP ·„} ~v n'(n*1)(2 aKb —M]‘` ±K„xd J`oMXOKP lMV‰Oj T^
)ÂVsY]KP lMV‰Oj ~v ŠMNv YVu ¸MX{KM[ ÂVsY]KP rMv ~KM]KMšv |RSš]KP lMV‰Oj T^ fwQKP
V~
AD1 D
;he orting
;he
intering
;he o!t,!t
sPaPction sodt ),'ت*•,: „'35تل
M}LSj LPSj¬P T^ Jj\O¶^ MQRSK rh ¾Y{QK n ŠMON]eM[ MRSjMws M_šsY` rh SRY`d
)oMV]–•M[ ÂVsY]KP
Šd¬P YwQNKP ¤^ M_KPSš]ePd Jv\{wOKP ~v YwQj Y¼iP Tj “‘šKP \} JVeMe¬P ‹YU{KP
aDi YiMQNKP TV[ YwQj Y¼iP Tj “‘š` ’¢ D aD2 aKb D aDn YwQNKP ¤^ M_‰RSš][ ™\X`d D
~`M•KP aD2 T^ YiMQNKP „–´` ’¢ D aD8 aKb D aDn ¤^ zKSš`d M}Y¼ih Tj “‘š`d D aD8 D
)JšsY^ Jv\{w^ a‰j |w‘Qv Jv\{wOKP YiMQj |x T^ ~_]Q` a]t P„U}d
)lP\ZmKP ·„} MQK Àg\R ~KM]KP |U£KPd
9 I 1: • 12 :
V•
AD1 D ADI D AD8 D AD9 D AD2 D AD7 D
AD1 E IEAE 8EAE 9EAE 2EAE 7EAE
AD1 D
9 : 1: • 12 I
12 : 1: • 9 I
12 • 1: : 9 I
12 1: • : 9 I
1: 12 • : 9 I
*:oMV]–•M[ ÂVsY]KP JV^•oP\–
1 ¤g) %=1
) 2 T^ Jv\{wOKP YiMQj T^ YwQj Y¼iP Tj “‘[P AD% aKb D ADN ) D
8 ¤^ Y¼i¬P YwQNKP |RSš][ ’‚) AD% )D
I |N¥P) %=%+1
9 rMx Pyb) %=N “Vt N •Ÿd «šso S‚ r\Us Jv\{wOKP rMv Jv\{wOKP YiMQj LSj
aKb Â}yP 2 )
4ro+ed!re ele+tort(6ar taGle:arrayD1))1#Eo$ integer2n:integer 2(
.ar
Max,otion:integer 2
F!n+tion $ind"ax,o(6ar taGle:arrayD1))1#Eo$ integer2n:integer):integer 2
.ar
%,"ax,o:integer 2
0egin
Max,o:=1 2
For %:=2 to n do
%$ taGleD%E>taGleD"ax,oE then "ax,o:=% 2
Find"ax,o:="ax,o 2
3nd 2
4ro+ed!re ex+hange(6ar x,y:integer 2(
.ar
;e",:integer 2
0egin
v€
AD1 E IEAE 8EAE 9EAE 2EAE 7EAE
1EAE IEAE 8EAE 9EAE 2EAE 7EAE
1EAE IEAE 8EAE 9EAE 2EAE 7EAE
1EAE IEAE 8EAE 9EAE 2EAE 7EAE
AD1 D
;e",:=x 2
X:=y 2
B:=te", 2
3nd 2
0egin
%$ n>1 then
0egin
Max,otion:=$in"ax,o(taGle,n 2(
3x+hange(taGleD"ax,otionE,taGleDn 2(D
/ele+tort(taGle,n*1 2(
3nd 2
3nd 2
*:†Y–h JXRYZ[ k^M`YšKP
?ont
N1=1## 2
;y,e
X=arrayD1))n1Eo$ integer 2
4ro+ed!re ele+tort(6ar a:x2n:integer 2(
.ar
%,j,,&:integer 2
0egin
For j:=1 to n*1 do
0egin
@:=j 2
/:=aDj 2D
For % :=j+1 to n do
0egin
%$ aD%E• then
0egin
/:=aD% 2D
@:=% 2
3nd 2
3nd2œ$or œ
/:=aDj 2D
ADjE:=aD& 2D
AD&E:= 2
3nd 2
v1
3nd2œend o$ ,ro+ed!re œ
+,*p^,: „'35تل !nsPdtion sodt
Y¼ih Tj “‘šKM[ •n dh ™\XQv oMV]–•M[ ÂVsY]KP JXRY¦ |Oj T^ »…¥ Yw]ms JXRYZKP ·„}
YiMQN‰K JtP•º[ ™\X` ’¢ Y–Ÿ YV¼]^ ~v z[ Í{]‘`d Jv\{wOKP ~v YwQj AD1 a]td D
™M^°K ‹\Z– YwQj |x ™SX]Vv Ž ·M`S¥d €„KP Y¼i¬P YwQNKP rMU^ |š‚ ¤XR €„KP YwQNKP
~v Y¼i¬P YwQNKP ¤Œ`d AD1 T^ YiMQNKP TR„–Ÿ JX[MpKP JV‰ONKP oYU` ’¢ Ž D AD2 D
) Jv\{wOKP ~v YwQj Y–Ÿ a]td
: JXRYZKP ·„} Àg\R ~s³P |U£KP
vU
AD1 E IEAE 8EAE 9EAE 2EAE 7EAE
9 I 1: • 12 :
9 1: • 12 : I
1: • 12 : 9 I
1: 12 • : 9 I
*: ŠM–L²M[ ÂVsY]KP JV^•oP\–
1 * ¤g %=1 )
2 * T^ Jv\{wOKP YiMQj T^ YwQj Y¼ih Tj “‘[h ADi aKb D ADn ) D
8 * €dMpR Y¼i¬P YwQNKP X )
I * T^ YiMQNKP ¤VO¶K JtP•b |Ojh ADi rMU^ |š‚ ¤XR €„KP YwQNKP a]td D
) ™M^°K ‹\Z– zQ^ |x ’RSX][ Y¼i¬P YwQNKP
9 * ¤g ADiE=X )
7 * ¤g %=%+1 )
: * rMx Pyb %=N ‹\ZmKP aKb Â}yh •bd «šso S‚ Jv\{wOKP rºv 2 )
+,*p^,: „'35تل Eم,85:
4ro+ed!re inertort(6ar taGle:arrayD1))1#Eo$ integer2n:integer 2(
.ar
Next,o,ne-,o, next6al:integer 2
4ro+ed!re hi$tGigger(6ar taGle:arrayD1))1#Eo$
integer2next,o,next6al:integer26ar ne-,o:integer 2(2
0egin
Ahile(next,o>2)and(taGleDnext,o*1E>next6al)do
0egin
;aGleDnext,oE:=taGleDnext,o*1 2D
Next,o:=next,o*1 2
vV
AD1 E IEAE 8EAE 9EAE 2EAE 7EAE
1EAE IEAE 8EAE 9EAE 2EAE 7EAE
1EAE IEAE 8EAE 9EAE 2EAE 7EAE
3nd 2
%$ taGleD1E>next6al then
0egin
;aGleD2E:=taGleD1 2D
Ne-,o:=1 2
3nd
3le
Ne-,o:=next,o 2
3nd2œend o$ ,ro+ed!re hi$t œ
0eginœGegin o$ inertort œ
For next,o:=2 to n do
0egin
Next6al:=taGleDnext,oE2œa6e next ele"ent œ
/hi$tGiggerDtaGle,next,o,next6al,ne-,o 2(
;aGleDne-,oE:=next6al 2
3nd 2
3nd 2
6يرسلا 7يتر.لا †Oick sodt
 ىلH اb(ي,ر, Bارملا Bا1عSا >ي3%$ب Yو%ي Fي,ر, نع ةAا(ع وه
Kم3ي c8اوملا 140 Aا$2ي >t نيم38 $irt |‚P ™M‚o¬P ¤ŒVv, z[ roMXRd
˜KP T^ $irt ˜KŸ oMpR aKb $irt ) zQVOR zQ^ Yšx¬P ™M‚o¬Pd
 $irt YwQj Šdh ¤‚\^
 <at YwQj Y–Ÿ ¤‚\^
 <o+ YwQj |‚P ¤‚\^
*:JV^•oP\mKP
 ’eM[ »PY¥b* W!i+& ort Tj ‹oMšj lPYV¼]^ zK x d $irt d lat
 ˜KŸ rMx Pyb $irt T^ Y¼iP lat ˜KŸ ’VpXs ’]R ,artition ) TVQ¢P aKb
vv

ŠM–L²M[ ÂVsY]KP dh oMV]–•M[ ÂVsY]KP ¤^ J`YX^ |Œ{KP »PL¬M[ JXRYZKP ·„} …VO]s
r\`MXKM[ ¤RYpKP ÂVsY]‰K k^M`YšKP „V{Qs T^• ƒMpt TUOR “Vt O(n log2 n (
ŠM–L²M[ ÂVsY]KP dh oMV]–•M[ ÂVsY]KP ÂVsY]‰K k^M`YšKP „V{Qs T^• ƒMpt ’]R MOQV[
r\`MXKM[
O(N2 (
˜KP JOV‚ rMx Pyb ¨•^ N †dMps 82 È:~s³Mx Âp‘s €oMV]–•P ÂVsY]KM[ T^…KP rMv
N
2
=82
2
=1#2I
~s³Mx Âp‘s ¤RYpKP ÂVsY]KM[ T^…KP MOQV[
N log
2
n=82 log
2
82=17#
-:6يرسلا 7يتر.لا "ار&8
4ro+ed!re 5!i+&Èort (6ar x: art 2 $irt , lat:integer O=
0egin
%$ ($irt •lat) then
0egin
4artion (x, $irt, lat, lo+ O=
W!i+&Èort (x, $irt, lo+*1 O=
W!i+&Èort (x, lo+ + 1, lat O=
3NC O
"ار&'ا padtition -:
,ro+ed!re ,artition(6ar x:art2 $irt , lat:integer2 6ar lo+:integer O=
.ar
4i6ot, i: integer O
0egin
%: =$irt O
<o+: =lat + 1 O
4i6ot: =x D$irt O•
Ahile % • lo+ do
03F%N
re,eat
%: =% + 1 O
=ntil x DiE >= 4i6ot
re,eat
%o+: =lo+*1 O
vy
=ntil x Dlo+E •= 4i6ot
%$ % • lo+ then
/-a, (x Dlo+E, x D% O= •
3NC O
/-a, (x Dlo+E , xD$irt O= •
3NC O
Art @لا ة%&+م KC اbفير', >, ةCوفصم نع ةAا(ع Kه ty,e )
*:ŠM•^
M_K ~¡M¶_KP ÂVsY]KP Âpt »MOe¬P ·„} ÂVsY][ ™\XRd »MOe¬P T^ JO¡M‚ PYXR k^M`Y[ Â]xP
)Y–Ÿ f‰^ ~v M_][M]x ’¢
)JVKM]KP J‘{wKP ~v k^M`YšKP
4rogra" ex+hangeort 2
?ont
Max=1## 2
;y,e
<it=arrayD1))"axEo$ tring 2
.ar
Na"e:lit 2
;e",:tring 2
%,j:integer 2
?hoi+e:Goolean 2
File1,$ile2:text 2
0egin
Aign($ile1,H+:Îdatain)dat 2(H
Aign($ile2,H+:Îdatao!t)dat 2(H
Reet($ile1 2(
Re-rite($ile2 2(
vz
%:=# 2
Re,eat
%:=i+1 2
Readln($ile1,na"eDi 2(D
=ntil eo$($ile1 2(
Re,eat
?hoi+e:=tr!e 2
For j:=1 to i*1 do
%$ na"eDjE>na"eDj*1E then
0egin
?hoi+e:=$ale 2
;e",:=na"eDj 2D
Na"eDjE:=na"eDj+1 2D
Na"eDj+1E:=te", 2
3nd 2
=ntil +hoi+e 2
For j:=1 to i do
Ariteln($ile2,na"eDj 2(D
?loe($ile1 2(
?loe($ile2 2(
3nd )
v|
9-خلا :ح,لا SPadc‡
)ة(,رم ري? ةCوفصم KC hح(ي ى&2لا hح(لا
K&'ن LHو <'8وم B1حن رص+'لا ان1Gو اذH ةCوفصملا KC hحب0
)eBوGو Y1'ب ةلاJA
: ةةةةةةةةةة
1 * ` <+ع hح(لا Bارملا رص+'لا dm0 ite" ةCوفصملا ةياbن ىلH =
ADN+1E=ite"
2 * .'G0 <o+ = 1 )
8 * hيحب 5 و 0 Aرك ite" AD<o+E ≠
1 * <o+ = <o+ + 1
U " Aارك$لا Kbن0
I * Nاك اذH <o+ = N + 1 F$كا LH و رص+'لا BوGو Y1'ب ةلاJA رbq0
@لا وهو رص+'لا c8وم <o+ )
: 9حي;وت $ا%م
V U 1
>8رلا نع hحب0 V
1
~
V 1
V
A
>8رلا V Kنا;لا c8وملا KC BوGوم N + 1 = I ⇒ N =
8
v~
V 1
~
V 1V
<o+=1
AD<o+E ≠ ite" ⇒ AD1E ≠ 8 ⇒ 18 ≠ 8
<o+ 2
AD<o+E ≠ ite" ⇒ AD2E = 8 ⇒ 8 = 8
˜KP ryb 8 )~`M•KP ¤‚\OKP ~v L\¥\^
v V U 1
>8رلا نع hحب0 V€
V

1
~
V 1
V
A
<o+ AD<o+E ≠ ite"
( AD1E ≠ 8# ) ⇒ ( 18 ≠ 8#) ⇒;r!e
<o+ AD2E ≠ ite"
( 8 ≠ 8#) ⇒ ;r!e
<o+ AD8E ≠ ite"
(1• ≠ 8#) ⇒ ;r!e
<o+ ADIE ≠ ite"
(8# ≠ 8#) ⇒ Fale
Nاك اذH ‚ <o+ = N+1 F$كا LH و رص+'لا BوGو Y1'ب ةلاJA رbqا
@لا وهو رص+'لا c8وم <o+ YwQNKP L\¥d ™SN[ JKMeo ¤šZs ±K„K ©Y£KP ®X‘s MQ}d
8# )
,rogra" tet 2
+ont
"=1# 2
6ar
A : array D1))1#E o$ integer 2
v•
1
2
8
I
<o+ , N , % , ite" : integer 2
0egin
N:= "*1 2
For %:= 1 to N do
0egin
Ariteln (Ïenter ADÏ,% 2 (ÄDÄ,
Readln(AD% 2 (D
3nd 2
Ariteln (Ïenter yo!r ite" 2 (Ä
Readln(ite" 2(
ADN+1E:= ite" 2
<o+:=1 2
Ahile AD<o+E •> ite" do
0egin
<o+ := <o+ + 1 2
3nd 2
%$ lo+=n+1 then
Ariteln(Ïite" i not $o!nd (Ä
ele
Arite (Ïite" <o+ = Ï,<o+ (
Readln 2
3nd )
(9<ا#%لا :ح,لا/ ة,ترم ة=و45م 9= :ح,لا
.@@4ارم ن@@م ة@@ل4رم .@@ك K@@C Yو%ن ا+نا KC ة%ير&لا e*ه ƒ2لن
ا@@ناك اذا R@@JوLا رص+'لا cم <+ع hح(لا Bارملا k1bلا ةنAا%مب hح(لا
y€
Nاك اذا اما ) <+ع hح(لا Bارملا رص+'لا c8وم B1ح, 18 Nوكي نييوا3$م
d@@ص+لا K@@C h@@ح(لا ا@@+يلع F@@IيC k1@@bلا ن@@م ر(@@كا R@@JوLا رص+'لا
ةCوف@@صلا ن@@م ر!@@fا R@@JوLا رص+'لا Nاك اذا اما ةCوفصملل .فJLا
ة@@يلم'لا e*@@ه Aر@@كن ةCوف@@صملا K@@C ىلعLا dص+لا KC hح(لا FIيC
Nا@@ك و@@ل ا@@مك <@@'م .@@ما'$نو ى@@لعLا dص+لاو .فJLا dص+لل ة(3+لاب
Y1@@ع ةلا4 KC hح(لا Kb$+ي وا k1bلا 1Iن ى$4 اb,ا*ب ةمgا8 ةCوفصم
) k1bلا ىلع انAو;ع
: ةيمزراوخلا
1 * ) RJوSا رص+'لل Kليل1لا ر„sملا ةمي8 F34ا
2 * نم ر(كا لوSا رص+'لا ` ةحلاf ري? ةCوفصملا kارl0 Qناك اذH
ةCوفصملا …9f Y1'ل ةلاJA و0 ةIي$+لا cGAا = ريESا رص+'لا
) hح(لل
8 * ر„sملا ةمي8 cGAا k1bلل ا
:
يوا3م RJوSا رص+'لا Nاك اذH
) RJوSا رص+'لل Kليل1لا
I * ةCوفصملا KC hحبا k1bلا نم ر(كا RJوSا رص+'لا Nاك اذH
رص+'لا` ىلH لوSا رص+'لا نم ةيليل1لا تار„sملا تاذ ةيg6Iلا
† RJوSا 1 ) =
9 * ةCوفصملا KC hحبTC k1bلا نم ر!fا RJوSا رص+'لا Nاك اذH
{ RJوSا رص+'لا` نم ةيليل1لا تار„sملا تاذ ةيg6Iلا 1 ىلH =
) ريESا رص+'لا
: 9حي;وت $ا%م
~ | z y v
V U 1




v

V
V
U
1
• V 1 A
ريESا
لوSا
y1
/ريESا c8وملا { لوSا c8وملا ‡ RJولا c8وملا U
#لا;م
رص+'لا نع hحبا VV )
#.حلا
RJولا c8وملا v /‡ U ` ~ { 1 =
target = 88
$ale %F(ADIE = target) ⇒
VV ‡ 21≠ ADI •
~ | z y
>8رلا نع hحب0 VV ةCوفصملا ىC




v

V
V
A
ريESا
لوSا
RJوSا c8وملا z /=‡ U ~ { y `
$ale ⇒ %F( AD7E = 88 =
AD7E= I# ≠ 88
z y
>8رلا نع hحب0 VV ةCوفصملا ىC
v

V
V
A
ريESا
لوSا
RJولا c8وملا y /=‡ U z { y `
;r!e ⇒ %F( AD9E = 88 =
AD9E= 88
رص+'لا VV c8وملا KC BوGوم y
,rogra" tet2 O
ty,e
data = arrayD1))1#E o$ integer 2
6ar
%, 0, target : integer 2
A : data 2
F!n+tion Ginear+h(6ar A:data 2 target : integer) : integer 2
.ar
<at, Firt, Midd : integer 2
0egin
Firt := 12
yU
<at := 1#2
Re,eat
Midd := (Firt + <at) di6 2 2
%$ ADMiddE > target then
<at := Midd q 1
3le
Firt := Midd + 1 2
=ntil (Firt = <at) or (ADMiddE = target)2
%$ ADMiddE = target then
0inear+h := Midd
3le
0inear+h := #2
3nd 2
0egin
For % := 1 to 1# do
0egin
Ariteln (Ïenter ADÏ,%,ÄEÄ) 2
Readln (AD%E) 2
3nd 2
Ariteln (Ïenter target : Ï) 2
Readln (target) 2
0:= 0inear+h (A,target) 2
Arite (Ïtarget = Ï,0) 2
Readln 2
3nd)
Stack ˆsin‰ Adda`
ty,e
?ont
Maxta+&=1## 2
;y,e
/ta+&=re+ord
%te":arrayD1))"axta+&Eo$ integer 2
;o,:#))"axta+& 2
3nd 2
.ar
/:ta+& 2
yV
%:integer 2
4ro+ed!re initialiže(6ar :ta+& 2(
0egin
)to,:=# 2
end 2
$!n+tion $!ll(:ta+&):0oolean 2
Gegin
i$ )to,="axta+& then $!ll:=tr!e
ele
$!ll:=$ale 2
end 2
$!n+tion e",ty(:ta+&):0oolean 2
Gegin
i$ )to,=# then e",ty:=tr!e
ele
e",ty:=$ale 2
end 2
,ro+ed!re ,!h(6ar :ta+&2x:integer 2(
Gegin
i$ $!ll() then -rite(Hta+& i $!ll (H
ele
Gegin
)to,:=)to,+1 2
)ite"D)to,E:=x 2
end 2
end 2
$!n+tion ,o,(6ar :ta+&):integer 2
Gegin
i$ e",ty() then -rite(Hta+& i e",ty (H
ele
Gegin
,o,:= )ite"D)to, 2D
)to,:=)to,*1 2
yv
end 2
end 2
$!n+tion ta+&to,(:ta+&):integer 2
Gegin
i$ e",ty() then -rite(Hta+& i e",ty (H
ele
ta+&to,:=)ite"D)to, 2D
end 2
,ro+ed!re di,lay(:ta+& 2(
6ar
%:integer 2
0egin
For %:= )to, do-nto 1 do
Arite()ite"D% 2(H H,D
3nd 2
Gegin
initialiže( 2(
,!h(,8 2(
,!h(,9 2(
,!h(,: 2(
,!h(,2 2(
di,lay( 2(
readln 2
end )
Stack ˆsin‰ PointPd
lPY”ªOKP ™PSm]eM[ JeSUOKP
=e +rt 2
;y,e
/ta+&=Édata 2
Cata=re+ord
%te": integer 2
Next:ta+& 2
3nd 2
.ar
/:ta+& 2
x:integer 2
yy
4ro+ed!re initialiže(6ar t:ta+& 2(
0egin
/t:=nil 2
end 2
$!n+tion e",ty(t:ta+&):0oolean 2
Gegin
i$ t=nil then e",ty:=tr!e
ele
e",ty:=$ale 2
end 2
,ro+ed!re ,!h(6ar t:ta+&2x:integer 2(
,:ta+& 2
Gegin
ne-(, 2(
,Éite":=x 2
,É)next:=t 2
t:=, 2
end 2
end 2
$!n+tion ,o,(6ar t:ta+&):integer 2
6ar
,:ta+& 2
Gegin
i$ e",ty(t) then
Gegin
,o,:=# 2
-rite(Hta+& i e",ty 2(H
end
ele
Gegin
,o,:= tÉ)ite" 2
,:=t 2
t:=tÉ)next 2
di,oe(, (
end 2
end 2
yz
$!n+tion ta+&to,(t:ta+&):integer 2
Gegin
i$ e",ty(t) then -rite(Hta+& i e",ty (H
ele
ta+&to,:=tÉ)ite" 2
end 2
,ro+ed!re di,lay(t:ta+& 2(
0egin
Ahile t•>nil do
0egin
Arite(tÉ)ite" 2(
/t:=tÉ)next 2
3nd 2
Gegin
initialiže( 2(
,!h(,8 2(
,!h(,9 2(
,!h(,: 2(
,!h(,2 2(
di,lay( 2(
readln 2
end )
ŠinPad NOPOP ˆsin‰ Adda`
% "@Dل XSل
JV¡PS][•P JKM‘KP rear=# d $ront=1
=e +rt 2
?ont
Max5!e!e=1# 2
;y,e
W!e!e=re+ord
%te":arrayD1))1#Eo$ integer 2
Rear,$ront:integer 2
3nd 2
.ar
y|
W:5!e!e 2
%:integer 2
4ro+ed!re initialiže(6ar W:5!e!e 2(
0egin
5)rear:=# 2
5)$ront:=1 2
end 2
$!n+tion $!ll(5:5!e!e):0oolean 2
Gegin
$!ll:=5)rear="ax5!e!e 2
end 2
$!n+tion e",ty(5:5!e!e):0oolean 2
Gegin
e",ty:=(5)$ront=1)and(5)rear=# 2(
end 2
,ro+ed!re ,!h(6ar 5:5!e!e2x:integer 2(
Gegin
i$ $!ll(5) then -rite(H5!e!e i $!ll (H
ele
Gegin
5)rear:=5)rear+1 2
5)ite"D5)rearE:=x 2
end 2
end 2
$!n+tion delete(6ar 5:5!e!e):integer 2
Gegin
i$ e",ty(5) then -rite(H5!e!e i e",ty (H
ele
Gegin
delete:= 5)ite"D5)$ront 2D
5)$ront:=5)$ront+1 2
end 2
y~
end 2
,ro+ed!re di,lay(5:5!e!e 2(
6ar
%:integer 2
0egin
For %:= 5)rear to 5)$ront do
Arite(5)ite"D% 2(H H,D
3nd 2
0egin
%nitialiže(5 2(
For %:=1 to I do
%nert(5,i 2(
Ci,lay(5 2(
Readln 2
3nd )
o57Lل tdPP
¸dY˜v ®˜RY¦ T˜j M˜_QV[ M˜OVv µšsYs lM`MVšKP lMVQ[ T^ ¸\` ±KMQ} Gran+he €h Ž
M˜˜_Vv Y˜˜wQNKP µ˜˜šsYR rh ‹odY˜˜ŒKM[ ‡V‰v Ž M_QR\Us ~v aZmKP ÂVxY]KP a‰j SO]Ns • M_`h
) z{‰– StPd YwQjd z^M^h StPd YwQN[
$% o57Lل Xي5[3
: “V‘[ Y•xP dh ·StPd ·SXj T^ r\U]s Ž ·LS‘^ ¸\O¶^ ~} ‹Y¶£KP
1 ) o„¶KP aOps ÐM– |U£[ zOOw^ ·SXj M_[ S¥\s root )
2 ) a˜˜Kb zOpX^ JV‚MšKP ‹SXNKP n>=# J˜˜j\O¶OKP T˜˜^ tl))tn |˜˜x “˜˜V‘[ J‰˜˜w{QOKP
·Y¶˜˜” r\˜˜Us lM˜˜j\O¶OKP ·„˜˜} T^ ·StPd th))tn lPYV¶˜˜” aO˜˜ps !Gtree T˜˜^
)o„¶KP
y•
M_[ zX[MpK P ‹Y¶£KP rb 18 ·SXj Node ·S˜˜Xj |U˜˜” ~˜˜v lM˜˜`MVšKP Y˜˜wQj †\˜˜]‘R
ˆY˜˜t a˜˜‰j M_Vv lM`MVšKP YwQj †\]‘R ~]KP ‹SXNKP ~} ‹Y¶£KP ·„} o„¥ )ˆYt a‰j A
rb ) oM¶˜˜”¬P ’˜˜eo ~v ¤š]OKP k_QKP \} P„}d Ž a‰jh ~Kb M}o„¥d ‹Y¶£KP MQOeo MQ`b Ít•
‹S˜XNKP J˜¥oL aO˜pR M˜^ ·S˜Xj T^ ¸Y{]s ~]KP lPYV¶£KP LSj degree o$ node J˜¥oSv
a‰j †\]‘s ~]KP ‹SXNKP a ~} 8 ˆY˜˜‘KP a˜˜‰j †\]‘s ~]KP ‹SXNKP —oL MOQV[ + ~˜˜} 1 M˜˜^h Ž
‹SXNKP J¥oL & J˜˜‚od aOps zRdMp^ M_]¥oL r\Us ~]KP ‹SXNKP rh Y{i ~_v lea$ ‹S˜˜XNKP dh
JV¡M_QKP ter"inal node › rM˜˜v ®š˜˜e M˜˜^ a‰j »MQ[ &,l,g,",%,j S˜˜XNKP zj\˜˜O¶^ ~˜˜} œ
J˜˜V‰–PL PÃS˜˜Xj aO˜˜ps Y{˜˜wKP †dM˜˜ps• M˜˜_]¥oL ~]˜˜KP S˜˜XNKP rM˜˜v ±˜˜Ky ‡˜˜UN[d Ž J˜˜V‚o\KP
nonter"inal node ) o„¶KP PSj M^
TU]Kd M^ ·SXj T^ ¸Y{]s ~]KP lPYV¶£KP o„¥ rb x »M˜˜Q[h aOps +hildren ˜˜˜˜˜˜˜˜˜K
x d x »MQ[h aOps ,arent »M˜˜Q[h rb ͘˜t¨` JX[M˜˜pKP ‹Y¶£KP aKb YQKM[d »MQ[¬P »•ª_K d
’} j,%,h ƒhd d \} a )
»MX”h r\OpR StP\KP ƒ¬P »MQ[h rb iGling rM˜˜v ±K„[d j,i,h ‡˜˜{` ’˜˜_K r• »MX˜˜”h ’˜˜}
ƒ¬P d )
o57Lل &c)p dP‰dPP of tdPP $%
~} JX[MpKP ‹Y¶£KP J¥oL rMv ~KM]KM[d ‹Y¶£KP ~v ·SXN‰K z¥oL a‰jh ~} 8 )
o6n[ل ‹وت;م aPŒPa of tdPP $%
†\]pOKP ~v M^ ‹SXj rb MQ‰‚ Pyb < †\]˜˜pOKP ~v r\`\UR ˆ\e M}»MQ[h rMv 1 + < Ž
†\]pOKP ~v ¤XR o„¶KP rh MQgY]vP Pyºv 1 T[•P rMv d †\]pOKP ~v ¤XR 2 )
o57Lل T,R3) ‡Pi‰‡t of tdPP $%
\} JX[MpKP ‹Y¶£KP ¸M{soP r\UR Ž ‹Y¶£KP ~v ·SXj M}„–´s rh TUOR †\]p^ a‰jh \} 8 )
&:,vل fodPst *:
LSj M_[ zj\O¶^ ~} n>=# Pyb M˜˜Q`h ͘˜t• ) M_˜˜ŒN[ T˜˜j J‰˜˜w{QOKP oM¶˜˜”¬P T˜˜^
Pyb JX[M˜pKP ‹Y¶˜£KP ~˜v ŠM˜•OKP |Vš˜e a‰j z[Mu a‰j |w‘` ˆ\e MQ`ºv ‹Y¶£KP o„¥ MQK•h
MQK•h a ) oM¶”h J¢¨¢ T^ z`\U^ z[Mu a‰j |w‘` ˆ\e
&'H,I-ل ),7r• …inad` tdPP *:
z€
€h rM˜˜[ fw]s a}d ) oM¶”¬P lMVQ[ ~v JO_OKP ¸P\`¬P T^ JV¡MQ•KP oM¶”¬P Yš]Ns
Yš˜˜xP M˜˜_]¥oL ·SXj M_[ S¥\s • z`P €h Ž rMjYv Y•x¬P a‰j M_RSK r\UR rh TUOR M_Vv ·SXj
T^ 2 ) ‹StP\KP ·SXN‰K †YpVKP ‹Y¶£KPd aQOVKP ‹Y¶£KP TV[ …VO` MQ`ºv JV¡MQ•KP oM¶”¬P ~v
"#dير',
dh Ž zVKM˜˜– r\˜˜Us rh M˜˜^h J˜˜j\O¶OKP ·„}d Ž SXNKP T^ ‹LdS‘^ zj\O¶^ ~} JV¡MQ•KP ‹Y¶£KP
†Y˜˜pVKP ‹Y¶˜˜£KP rMVO˜˜ps TV]‰˜˜w{Q^ Y•˜˜x¬P a˜˜‰j TVsYV¶˜˜”d o„˜˜¥ a‰j †\]‘s M_`h le$t
!dtree aQOVKP ‹Y¶£KPd right !dtree |˜˜xd Ž o„˜˜¶KM[ rM]‰˜˜w]^ rMsYV¶˜˜£KP rM˜˜sM}
) ‹Y¶” |•Os MO_Q^
lM˜˜`MVšKP ¸P\˜˜`h T˜˜^ ¸\Qx JV¡MQ•KP ‹Y¶£KP SRS‘]Kd LY¶^ fRYNs \} fRYN]KP P„} rh Ít•
a˜˜‰j ‹Y¶˜˜£KP a˜˜‰j †Y˜˜¶s rh TUOR ~]KP lMV‰ONKP ~} M^ Àg\` rh MQV‰j ¶R Ž ‹LY¶OKP
) ‹Y¶£KP |–PL SXNKP JXRY¦ MQK TVšR ’K fRYN]KP P„} rh MŒRh Ít• ) JV¡MQ•KP ‹Y¶£KP
‹Y˜˜X{KP ~˜˜v ¾YN]p` ˆ\ed ) “‘šKP M_QOg T^ ¾PYuh ‹SNK ™Sm]ps JV¡MQ•KP oM¶”¬P rh
) ÂVsY]KPd “‘šKP lMV‰Oj ~v JV¡MQ•KP oM¶” •P ¸P\`h T^ ¸\` zVK´]KP
&'H,I-ل Gح?ل o57r
…inad` sPadc‡ tdPP
¯M˜˜]{^ a˜˜‰j †\˜˜]‘s M˜˜_Q^ ·S˜˜Xj |˜˜x dh zVKM– r\Us rh M^Ÿ ~]KPd Ž JV¡MQ¢ ‹Y¶” ~}
: JVKM]KP ©dY£KP ®X‘R
) o„¶KP ~v ‹SXNKP ¯M]{^ T^ Y¼iPŽ †YpVKP ‹YV¶£KP ~v ( lS¥d rh ) ÀVsM{OKP |x
a˜˜QOVKP ‹Y¶˜˜£KP ) a˜˜QOVKP ‹Y¶˜˜£KP ~˜v (S˜¥d rh ) À˜˜VsM{OKP |˜x T˜˜^ o„¶KP ~v ‹SXNKP ¯M]{^
‹Y¶˜˜” |U˜˜” À˜˜g\R ~KM˜˜]KP ŠM˜˜•OKP ) JX[M˜˜pKP ©dY£KP Y{` MO_V‰j ®šZQs †YpVKP ‹Y¶£KPd
:JV¡MQ•KP “‘šKP
z1
o„¶KM[ J‰w]OKP aQOVKP ‹Y¶£KP ~v ¤Xs ~]KP ÀVsM{OKP YšxP rh Ít¨` 9 T^ YšxP ~} 9 rPd Ž
o„¶KM[ J‰w]OKP †YpVKP ‹Y¶£KP ~v ¤Xs ~]KP ÀVsM{OKP |x 9 T˜˜^ Y¼˜˜iP ~} Ž 9 dh ±˜˜Kyd Ž
) TVsYV¶£KP T^ ‹Y¶” |x M`„–Ÿ 2 ( ; d ; (: |V_˜˜p]K Jt¨OKP ‡{` Ít¨` ˆ\e MQ`ºv (
™Sm]˜˜p` ˆ\˜˜e Ž M˜˜_‰V•O]K J˜˜xY‘]^ lM˜˜`MV[ J˜˜VQ[ ™Sm]˜˜p` ˆ\˜˜e M˜˜Q`ºv ‹YV¶˜˜£KP ¤^ |^MN]KP
˜˜˜˜K P\˜˜} Y˜˜”ªOKP ±K„K LM]NOKP ’e•P rh ‹Y¶£KP rMU^ a‰j MQKSVK Y”ª^ YV¼]OKP Root z˜˜`•
‹Y¶£KP o„¥ aKb YV£R ˆ\e
rMv zVKM– ‹Y¶£KP rMx Pyb root=nil )
T˜UOR M˜˜^ P„˜˜}d Ž †Y˜pR ‹YV¶˜”d a˜˜QOR ‹YV¶˜˜” M˜_K J˜˜V¡MQ•KP “˜˜‘šKP ‹Y¶˜” ~˜˜v ·S˜˜Xj |x
) k^M`YšKP T^ »…¶KP P„} ~v lPY”ªOKP ŠMON]eM[ zV‰j Š\w‘KP
;y,e
4=Énodety,e 2
Nodety,e=re+od
%n$o : integer 2
<e$t, right:, 2
3nd 2
.ar
Root:, 2
o57r w6nM b,L8# :
“˜˜‘šKP ·Y¶˜˜” a˜˜Kb M_]vM˜˜g² PÃS˜˜V_Os ·S˜˜Xj »M˜˜£`b z˜˜V{Vx M˜˜QK À˜˜g\s J˜˜VKM]KP J˜˜KPSKP
JKPSKP ™PSm]eM[ Y”ª^ »M£`b ’]R “VtŽJV¡MQ•KP ne- a˜˜‰j †\˜˜]‘R |¶˜˜e a˜˜Kb YV£R €„KPd Ž
¸\˜˜QKP T˜˜^ “KM˜˜•KP Ž lPY˜˜”ª^ T˜˜j ‹oMšj MO_^ TVQ¢P Ž Š\Xt J¢¨¢ integer zKPS˜˜[ r…˜˜mVK
) ¯M]{OKP
$!n+tion +reat,tr(r:integer):,2
6ar
,tr:,2
Gegin
ne-(,tr)2
-ith ,trÉ do
Gegin
in$o:=r2
le$t:=nil2
zU
right:=nil2
end2
+reat,tr:=,tr2
end2
$% &'H,I-ل Gح?ل o57r "ل# w6nM &=,{#
·„˜˜} Ž ®[M˜˜pKP ŠM˜˜•OKP ~˜˜v ~]˜˜KP J˜˜V¡MQ•KP “‘šKP ‹Y¶” aKb ·SXj fVŒ` rŸ SRY` MQ`b ¾Y{QK
¯M]{OKP a‰j †\]‘s ‹SXNKP 8 J˜˜`oMXO[ •dh hSšQv Ž 8 o„˜˜¶KP ¯M˜˜]{^ ¤˜˜^ 9 rh S˜˜¶` 8 > 9 r³P
¯M]{OKP a‰j †\]‘R M}o„¥ rh Ít¨` o„¶‰K †YpVKP ‹Y¶£KP ~KŸ |X]Q` 2 Y¼iP \} €„KPd
T^ 8 z˜]OV‚ ¯M˜]{OKP rh S˜¶` Ž a˜QOVKP ‹Y¶˜˜£KP ~KŸ r³P z¶]` ±K„K Ž I T˜˜^ Yš˜˜xP \˜˜}d Ž 8 Ž
aKb YV£R ‹SXNKP ·„_K YpR¬P Y”ªOKP r•d nil : ~s³Mx ‹Y¶£KP Àšw]K Ž
&'H,I-ل Gح?ل o57r "ل# w6nM &=,{# %$
Y”ªOKM[ M_VKb oM£OKP ‹SXNKP JvMg² €oPYU]KP »PSQ‰K ™Sm]pR ~KM]KP k^M`YšKP ,tl aKb
Y”ªOKM[ M}o„¥ aKb oM£OKP ‹Y¶£KP ,t )
,ro+ed!re add(6ar ,t,,t1:,)2
Gegin
i$ ,t=nil then
,t:=,t1
ele
i$ ,trÉ)in$o • ,tÉ)in$o then
add(,tÉ)le$t,,tr)
ele
add(,tÉ)right,,tr)2
end2
zV
&'H,I-ل Gح?ل o57r <= Ž,تRم نM Gح?ل %$
¯M˜˜]{^ Tj “‘šKP ~v €oPYU]KP »PSQKP ™PSm]eP JXRY¦ Àg\R ~KM]KP k^M`YšKP Ñ ‹Y¶˜˜” ~˜˜v
aKb Y”ªRd Ž ·L\¥d JKMt ~v ¯M]{OKP a‰j †\]‘s ~]KŸ ‹SXNKP aKb oM£^ JV¡MQ¢ “‘[ nil ~v
) ¯M]{OKP L\¥d ™Sj JKMt
$!n+tion ear+h(,t:,2 ž:integer) :,2
Gegin
i$ ,t=nil then
Gegin
-riteln(Ñ,Äi not $!ond H)2
ear+h:=nil2
end
ele
i$ ,tÉ)in$o = ž then ear+h:=,t
ele
i$ ž•,tÉ)in$o then ear+h:=ear+h(,tÉ)le$t,ž)
ele
ear+h:=ear+h(,tÉ)right,ž)2
end2
$% &'H,I-ل Gح?ل o57r نم o6nM Q€g
~˜˜v Y˜˜^¬P rM˜˜v z˜˜‚od T˜˜j ‹oMšj ‹Y¶£KP T^ M_v„t LPYOKP ‹SXNKP «`Mx Pyb
a˜‰j †\˜]‘s ~]˜KP ‹S˜XNKP ˆ„˜t “˜Vt Ž ’eYKP ~v Àg\^ \} MOx JK\_pKP JRMu
) ¯M]{OKP 8 dS˜˜šR Y^¬P rMv †YpRd aQOR rMsYV¶” M_RSK ‹SXNKP ·„} «`Mx Pyb M^h (
) »~£KP ÅN[ PÃSXN^
¯M˜˜]{OKP a‰j †\]‘s ~]KP ‹SXNKP ˆ„t SRY` MQ`h ¾Y{QK ·M`Lh |U£KP ~v 2 Ž
‹SXNKP rMU^ †YpVKP ‹Y¶£KP ¤vY` rh \} z‰Oj MQV‰j €„KP |Uv 2 ‹S˜˜XNKP |˜˜N¶`d Ž
M_v„˜˜t LPY˜˜OKP ·S˜˜XN‰K oM˜˜pVKP 2 ~‰{˜˜pKP T˜VOVKP a˜˜w‚h ¤˜˜Xs ~]˜˜KP ‹S˜˜XNKM[ |˜˜w]s
‹YV¶£‰K I **: ~s³Mx M`SQj |U£KP ÀšwRd
zv
‹SXNKP ˆ„t SRY` MQ`h ¾Y{QK r³P : rh \˜˜} z˜˜‰Oj ˜˜¶R €„˜˜KMv ·¨jh |U£KP T^
‹SXNKP ¤vY` •
‹SXNKP r\Usd a‰jh ~Kb ( aQOVKP ‹SXNKP) 7 ) ¯M˜˜]{O‰K †Y˜˜pR ‹YV¶” Tj ‹oMšj • (
* : ~KM]KP |U£KP ~v Àg\^ \} MOx
$ % Q€حل &nي5‚ •{وي <ل,تل Eم,85?ل
,ro+ed!re delete (root:,2 6ar $o!nd:Goolean2&:integer)2
6ar
te",:,2
Gegin
i$ rootÉ)in$o=& then $o!nd:=tr!e
ele
i$ rootÉ)in$o > & then delete(rootÉ)le$t,$o!nd,&)
ele
delete(rootÉ)right,$o!nd,&) 2
i$ ($o!nd) and (&•>rootÉ)in$o) then
Gegin
i$ rootÉ)le$tÉ)in$o =& then
Gegin
zy
te",:=rootÉ)le$t2
rootÉ)le$t:=te",É)le$t2
end
ele
Gegin
te",:=rootÉ)right2
rootÉ)right:=te",É)le$t2
end 2
i$ te",É)right •> nil then
add(root,te",É)right)2
$o!nd:=$ale2
di,oe(te",)2
end2
end2
;B43
,=Énode2
node=re+ord
in$o:integer2
le$t,right:,2
end2
6ar
,tr, ne-,tr ,root:,2
$!n+tion +reat,tr(r:integer):,2
6ar
,tr:,2
Gegin
ne-(,tr)2
-ith ,trÉ do
Gegin
in$o:=r2
le$t:=nil2
right:=nil2
end2
zz
+reat,tr:=,tr2
end2
,ro+ed!re add(6ar ,t,,tr:,)2
Gegin
i$ ,t=nil then
,t:=,tr
ele
i$ ,trÉ)in$o • ,tÉ)in$o then
add(,tÉ)le$t,,tr)
ele
add(,tÉ)right,,tr)2
end2
$!n+tion ear+h(,t:,2 ž:integer) :,2
Gegin
i$ ,t=nil then
Gegin
-riteln(Ñ,Äi not $!ond H)2
ear+h:=nil2
end
ele
i$ ,tÉ)in$o = ž then ear+h:=,t
ele
i$ ž•,tÉ)in$o then ear+h:=ear+h(,tÉ)le$t,ž)
ele
ear+h:=ear+h(,tÉ)right,ž)2
end2
,ro+ed!re delet (root:,2 6ar $o!nd:Goolean2&:integer)2
6ar
te",:,2
Gegin
i$ rootÉ)in$o=& then $o!nd:=tr!e
ele
i$ rootÉ)in$o > & then delet(rootÉ)le$t,$o!nd,&)
ele
delet(rootÉ)right,$o!nd,&) 2
i$ ($o!nd) and (&•>rootÉ)in$o) then
Gegin
i$ rootÉ)le$tÉ)in$o =& then
Gegin
z|
te",:=rootÉ)le$t2
rootÉ)le$t:=te",É)le$t2
end
ele
Gegin
te",:=rootÉ)right2
rootÉ)right:=te",É)le$t2
end 2
i$ te",É)right •> nil then
add(root,te",É)right)2
$o!nd:=$ale2
di,oe(te",)2
end2
end2 œdelete› 2
Gegin
root:= ne-(,tr)2
root:=nil2
$or %:=1 to 1# do
Gegin
ne-,tr:+reat,tr2
add(root, ne-,tr)2
end2
readln2
end)
&MوIتم ني),23
»M£`² k^M`Y[ Â]xP)' lin&ed lit ~`M•KPd ’e¨K Šd¬P Š\Xt J¢¨¢ a‰j M}SXj †\]‘s
JVKM]KP ‹SXN‰K Y”ª^ “KM•KPd YON‰K
)J{‰]m^ »MOe´[ SXj ‹Y£j |–LPÈ
’‚o ‹SXNKP ˆ„tPÈ 9 )
)ˆ„‘KP SN[d |š‚ lMR\]‘OKP ¤š¦P
z~
*:~s³P LSt ®[MpKP |U£KP T^'
1 o„¶KP \} M^È ROO;
È 2 »MQ[h F )PdS¥d rb
8 ˜K †\]pOKPÈ ? )
È I )ËPod¬P |U[ JO¡M‚
9 ˜‰K †YpVKP ‹Y¶£KP ’eoPÈ F )
È 7 )‹Y¶£KP r•d
: )»MX”¬P ’} T^È
z•

k := n * n ; /2 for I := 1 to n do for j :=1 to k do sum := sum + 1 ; for i:= 1 to n do /3 for j := 1 to I do ;sum := sum + 1 ‫مثال لتحسين الكفاءة بمعيار الزمن‬ ‫خوارزمية الس‬ N N/2 N/2 X =X .X ‫ عدد زوجي فان‬N ‫إذا كان‬ N N-1/2 N-1/2 X =X .X .X ‫ عدد فردي فان‬N ‫إذا كان‬ N ‫ مرة مستخدما ً الحلقات ومرة‬X ‫مثال : أكتب برنامج يحسب‬ ‫ في كل‬oh notation ‫أخرى مستخدما ً خوارزمية الس وأحسب‬ ‫مرة ؟‬ ; Function power(x,n:integer ):real Var ; Sum:real ; I : integer Begin ; Sum:= 0 For I := 1 to n do ; sum := sum × x ; power := sum ; end : ‫باستخدام خوارزمية الس‬ Function power(x,n:integer):real; Begin If n = 0 then power :=1 Else If odd (n) then ((Power := x * sqr(power (x,n div 2 Else ; ((Power :=sqr (power (x,n div 2 ; End

2

You're Reading a Free Preview

تحميل
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->