Sunteți pe pagina 1din 23

ACM-lCC 1halland CenLral Croup 8

1ralnlng rogram
[
laculLy of lC1, Mahldol unlverslLy
Aplrak Poonlor
!uly 14, 2012
1
CredlLs
Pallm eL al., Compeuuve rogrammlng
Sklena eL al, rogrammlng Challenges
Mlguel A. 8evllla, ArL of rogrammlng ConLesL
uva ACM-lCC quesuons archlve
2
Compeuuve rogrammlng
3
Compeuuve rogrammlng
Clven !"##$%&'!& CompuLer Sclence problems,
Solve Lhem as fasL as posslble
Soware developmenL processes ! lorgeL abouL lL.
Cood sLyle of programmlng wlLh commenLs !
LlmlLed amounL of commenLs.
Well-known == noL research problems
Pas soluuons
Can be solved uslng known algorlLhms.
4
1lps Lo be CCML1l1lvL
5
1lp 1: 1ype lasL & CorrecL
no klddlng, Lhls can be vL8? lmporLanL.
hup://www.LyplngLesL.com
lamlllarlze yourself wlLh Lhe posluons of
[ } [ ] < > ' " & | ! , , . ( )
eLc
Pow Lo be beuer?
rogram more. A loL more.
6
1lp 2: Culckly read Lhe quesuon and
ldenufylng problem Lypes
Ad Poc
CompleLe Search
ulvlde and Conquer
Creedy
uynamlc rogrammlng
7
Craph
MaLhemaucs
SLrlng rocesslng
CompuLauonal CeomeLry
CLhers
undersLandlng Lhe quesuon
8ead Lhe problem carefully
Sklm Lhe quesuon Lo lLer ouL background/
hlsLory LhaL does noL lmpacL Lhe soluuon.
ay auenuon Lo lnpuL/ouLpuL descrlpuon and
Lhe sample lnpuL/ouLpuL.
uo noL assume ([usL because Lhe examples
have cerLaln properues)
Make noLe of speclal lnpuLs (break sequence, end
of llne, eLc)
8
1lp 3: uo AlgorlLhm Analysls
ConsLralnLs ln Lhe problem sLaLemenL
MuS1 knCW 1PLM
uon'L be afrald Lo use Lhe slmplesL LhaL works
Lmclency ls key??? noL really
Check maxlmum posslble slze and declde lf bruLe
force wlll be ok.
AL leasL do some baslc analysls Lo convlnce
LhaL lL wlll work 8LlC8L begln codlng.
9
1lp 4: MasLer rogrammlng Language(s)
Coal: Able Lo CulCkL? LranslaLe Lhe soluuon
lnLo a bug-free code.
MasLer A1 LLAS1 CnL programmlng languages
Save ume of checklng references
use shorLcuLs, macros
use llbrarles whenever posslble
know baslc funcuons by hearL
10
11
rogrammlng PlnLs
use symbollc consLanLs and varlable names
lour sulLes of card club, hearL, spade, dlamond.
Slmply use C, P, S, u
Avold redundanL code
use subrouune
CeL Lo know Lhe debugger
ln long program, lL may help Lo wrlLe key commenLs
12
1lp 3: 8e masLer LesLer
ulumaLely we wanL AccepLed (AC)" verdlcL.
1esL Lhe glven lnpuL
1esL lncorrecL lnpuL
1esL boundary condluons
1esL examples LhaL you know of correcL answer
1esL 8lg Lxamples lf Lhe daLa can be produced
qulckly
13
1lp 6: racuce
14
Lngllsh
for
Compeuuve rogrammlng
15
)*+ ,-./0 $ 1+2"3 4+2" 5+&+6"7"&2
A dam has n waLer gaLes Lo leL ouL waLer when necessary. Lach waLer gaLe has lLs own capaclLy, waLer
paLh and aecLed areas ln Lhe downsLream. 1he aecLed areas may have a rlsk of ood when Lhe waLer
gaLe ls open. 1he cosL of poLenual damage caused by a waLer gaLe ls measured ln number calculaLed
from Lhe number of people and areas esumaLed Lo geL aecLed.
Suppose a waLer gaLe C
l
has Lhe volumeLrlc ow raLe of l
l
m
3
/hour and Lhe damage cosL of C
l
. ln
a cerLaln slLuauon, Lhe dam has Lhe volume v m
3
of waLer Lo ush ouL wlLhln 1 hours. ?our Lask ls Lo
manage Lhe openlng of Lhe waLer gaLes ln order Lo geL rld of aL leasL Lhe specled volume of waLer
wlLhln a llmlLed ume ln condluon LhaL Lhe damage cosL ls mlnlmlzed. lor example, a dam has 4 waLer
gaLes and Lhelr properues are shown ln Lhe followlng Lable.
1+2"3 4+2" 4
,
4
-
4
.
4
/

llow raLe (m
3
/hour) 720,000 30,000 130,000 1,200,000
CosL 120,000 60,000 30,000 130,000
Case 1: ?ou have Lo ush ouL Lhe waLer 3 mllllon m
3
wlLhln 7 hours. 1he mlnlmum cosL wlll be 120,000 by
lemng Lhe waLer gaLe C1 open for 7 hours.
Case 2: ?ou have Lo ush ouL Lhe waLer 3 mllllon m
3
wlLhln 30 hours. 1he mlnlmum cosL wlll be 110,000 by
lemng Lhe waLer gaLes C
2
and C
3
open, for example, C
2
ls open for 29 hours and C
3
ls open for 28 hours.
noLe LhaL each waLer gaLe ls lndependenL and lL can be open only ln a unlL of whole hour (no fracuon of
hour).
16
8&9:2; 1he rsL llne lncludes an lnLeger n lndlcaung
number of waLer gaLes (1 < n < 20). 1hen Lhe nexL n
llnes conLaln, ln each llne, Lwo lnLegers: l
l
and C
l

correspondlng Lo Lhe ow raLe (m
3
/hour) and Lhe
damage cosL of Lhe waLer gaLe C
l
respecuvely. 1he nexL
llne conLalns Lhe number m whlch ls Lhe number of LesL
cases (1 < m < 30). 1he followlng m llnes conLaln, ln
each llne, Lwo lnLegers: v and 1 correspondlng Lo Lhe
volume (m
3
) of waLer Lo leL ouL wlLhln 1 hours.
(1 < l
l
,v, C
l
< 10
9
, 1 < 1 < 1000)
<:29:2; lor each LesL case, prlnL ouL Lhe mlnlmum cosL ln
Lhe exacL formaL shown ln Lhe sample ouLpuL below. lf
lL ls noL posslble Lo leL ouL Lhe waLer of volume v ln 1
hours from Lhe dam, prlnL ouL lMCSSl8LL" (wlLhouL
quoLauon marks).
17
Sample lnpuL Sample ouLpuL
4 Case 1: 120000
720000 120000 Case 2: 110000
30000 60000 Case 3: lMCSSl8LL
130000 30000
1200000 130000
3
3000000 7
3000000 30
63000000 24
18
1halland nauonal ConLesL 2009 - My AncesLor
?our greaL greaL ... greaL grandfaLher '1lger' was a famous
Muay 1hal (1hal 8oxlng) masLer. Pe was renowned for hls
Lrademark '1eh Chlang' (ulagonal klck). 1hls sklll has surely
well preserved wlLhln your famlly as you have lnherlLed hls
1eh Chlang plus all oLher skllls from your ancesLors and many
more, e.g. Chok (unch), 1ee Sok (Llbow), 1eh (klck), 1ee kao
(knee), 1eep (looL-LhrusL) Lechnlques. ?our famlly has a very
good Muay 1hal Lralnlng program so LhaL all chlldren ln your
famlly always have sLrlcLly greaLer level of sklll Lhan Lhelr
parenLs. Clven a famlly sLrucLure (only males are llsLed) and
Lhe sklll level of each famlly member, you wanL Lo know
whlch of your greaLesL ancesLor (Lhe man closesL Lo '1lger' ln
famlly sLrucLure and may lnclude yourself) ls Lhe rsL man Lo
reach some level of sklll.
19
lnpuL and CuLpuL
1he lnpuL conLalns muluple LesL cases, please process each LesL case unul end of
le. 1here ls =< blank llne beLween LesL cases.
1he rsL llne of each LesL case conLalns Lwo lnLegers: n, C.
n denoLes Lhe number of person ln your famlly. 1 < n < 200000.
C denoLes Lhe number of querles. 1 < C < 100000.
1hen, n-1 llnes appear, llne 1 unul llne n-1.
1he l-Lh llne conLalns Lwo lnLegers: pr
l
and lv
l
. 0 < pr
l
< n-2, 1 < lv
l
< 2
31
-1.
1hls means person l has sklll level lv
l
and has pr
l
as hls parenL.
llnally, C llnes appear, llne 1 unul llne C.
1he l-Lh llne conLalns Lwo lnLegers: person and . 0 < person < n-1, 1 < <
lv
person
.
lor each query, please prlnL Lhe lndex of greaLesL ancesLor of person wlLh sklll level
greaLer Lhan or equal Lo .
1lger has lndex = 0, pr
0
= -1, and lv
0
= 1, buL Lhere wlll be no query wlLh person = 0,
so Lhls boundary case can be lgnored.
20
21
Lxplanauon:
Cbserve LhaL Lhere ls no blank llne beLween
Lhe ouLpuLs of Lhese Lwo ldenucal LesL cases!
1lps and 1rlcks
LocaLe keywords:
for comparlson/boundary: greaLer Lhan, sLrlcLly
less Lhan, more Lhan, aL leasL, aL mosL, exacLly.
for quanucauon: cosL, value, age, raLe, level.
for lnpuL/ouLpuL: lgnored, llnes, appear, number,
prlnL, no empLy llne.
for symbollc Lerms: n, s, lv, l, C, eLc.
Look for consLralnLs
1 < n < 200000
lgnore Lhe (borlng) sLory.
22
uva 389 8aslcally Speaklng
23

S-ar putea să vă placă și