Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Problem A: Movie: 38" ACM International Collegiate

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

38"th ACM

38 ACM International
International Collegiate
Collegiate
Programming Contest,
Programming Contest, 2013–2014
2013-2014
Asia Region,
Asia Region, Tehran
Tehran Site
Site
Sharif University
Sharif University of
of Technology,
Technology, 19–20
19-20 December
December 2013
2013

Problem A:
Problem A: Movie
Movie
The
The “Last
“Last ACM
ACM Contest”
Contest” movie
movie hashas been
been recently
recently released
released andand got
got the
the record
record of
of the
the highest
highest worldwide
worldwide gross.
gross. Gabby
Gabby
has been so busy due to his practicing for the ACM programming contest in Tehran site and hence, he
has been so busy due to his practicing for the ACM programming contest in Tehran site and hence, he has
has not
not
succeeded to
succeeded to watch
watch the
the movie
movie at at aa cinema.
cinema. Like
Like many
many other
other people, he has
people, he has legally
legally downloaded
downloaded thethe movie
movie toto watch
watch itit
using aa smart
using smart phone
phone oror aa tablet
tablet onon the
the way
way back
back home
home fromfrom the
the Sharif
Sharif University
University onon 20
20 December
December 2013,
2013, the
the contest
contest
day. Unfortunately,
day. Unfortunately, hehe has
has neither
neither aa smart
smart phone
phone nor nor aa tablet.
tablet. He
He therefore
therefore has
has decided
decided toto buy
buy aa device
device from
from the
the
following smart
following smart phones
phones oror tablets
tablets that
that are
are available
available inin the
the market.
market.

Device
Device Price
Price Resolution
Resolution
iPad Mini
iPad Mini 319
319 1024x768
1024×768
Galaxy Tab
Galaxy Tab 419
419 1024x600
1024×600
iPhone 4S
iPhone 4S 450
450 960x640
960×640
iPad4
iPad4 519
519 2048x1536
2048×1536
iPhone 5C
iPhone 5C 599
599 1136x640
1136×640
Galaxy Tab2
Galaxy Tab2 600
600 1280x800
1280×800
Galaxy S4
Galaxy S4 630
630 1080x1920
1080×1920
iPhone 5S
iPhone 5S 719
719 1136x640
1136×640

As you
As you can
can see,
see, each
each device
device hashas aa known
known “height
“height × < width”
width” resolution
resolution whichwhich is is the
the number
number of of distinct
distinct pixels
pixels in in each
each
dimension that
dimension that can
can bebe displayed.
displayed. LikeLike thethe above
above devices,
devices, each
each movie
movie has has aa known
known resolution
resolution H x W and and video
video players
players
equally scale
equally scale both dimensions of
both dimensions of the
the movie
movie resolution
resolution by by aa factor
factor of of c to
to display
display it it in
in aa [CH] x |[cW] window window where where [x]
is the
is the largest
largest integer
integer not not greater
greater than
than x and and c is is aa rational
rational number
number such such that
that cH and and cW are are not
not greater
greater than
than the
the height
height
and width
and width resolutions
resolutions of of the
the display,
display, respectively.
respectively. Indeed,Indeed, video
video players
players zoomzoom aa movie
movie in in or
or out
out while
while preserving
preserving the the
aspect ratio
aspect ratio (the
(the ratio
ratio ofof the
the height
height resolution
resolution to to the
the width
width resolution)
resolution) of of the
the movie.
movie. When When aa videovideo player
player displays
displays aa
movie in
movie in the
the full-screen
full-screen window
window in in aa device,
device, somesome partsparts of
of the
the device
device display
display may remain “blank”
may remain “blank” (or (or unused)
unused) due due to to
the difference
the difference in in the
the aspect
aspect ratios
ratios ofof the
the device
device display
display andand the
the movie.
movie. ForFor aa movie
movie resolution
resolution and and aa display
display resolution,
resolution,
the usage
the usage ratio
ratio is is defined
defined to to be
be the
the ratio
ratio ofof the
the non-blank
non-blank area area ofof the
the full-screen
full-screen video-player
video-player window window showing
showing the the
movie to
movie to the
the area
area of of the
the device
device display.
display. As As all
all above
above devices
devices can can rotate
rotate the
the whole
whole screen
screen 90 90 degrees,
degrees, they they may
may increase
increase
their usage
their usage ratio
ratio forfor aa movie.
movie. For For instance,
instance, if if the
the movie
movie resolution
resolution is is 720 x 480, , the the usage
usage ratioratio ofof iPad4
iPad4 is is the
the
maximum of
maximum of (2048 * 1365 )/ (2048 * 1536) and and (1536 * 1024 )/ (1536 * 2048) (the (the latter
latter isis due
due toto aa 90-degree
90-degree
rotation) which
rotation) which is is 1365/1536. .

Gabby
Gabby isis now
now looking
looking for
for aa device
device with
with the
the highest
highest usage
usage ratio
ratio for
for the
the “Last
“Last ACM
ACM Contest”
Contest” movie
movie which
which is
is stored
stored with
with
the resolution H x W. He kindly asks you to report him this device before the end of the contest.
the resolution . He kindly asks you to report him this device before the end of the contest.

Input (Standard
Input (Standard Input)
Input)
There are
There are multiple
multiple test
test cases
cases inin the
the input.
input. Each
Each test
test case
case consists
consists of
of aa line
line containing
containing two
two integers
integers 1 < H,W < 5000
which is
which is the resolution of
the resolution of the
the “Last
“Last ACM
ACM Contest”
Contest” movie.
movie. The
The input
input terminates
terminates with
with “0
“0 0”
0” which
which should
should not
not be
be
processed.
processed.

Output (Standard
Output (Standard Output)
Output)
For each
For each test
test case,
case, output
output aa line
line containing
containing the
the price
price of
of the
the device
device which
which has
has the
the highest
highest usage
usage ratio
ratio for
for the
the given
given movie
movie
resolution. In
resolution. In case
case of
of aa tie,
tie, output
output the
the smallest
smallest price.
price. For
For example,
example, if
if both
both iPad4
iPad4 and
and iPad
iPad Mini
Mini have
have the
the highest
highest usage
usage
ratio, output
ratio, output 319.
319.

Problem A
Problem A -- Page
Page 1| of
of 2
Sample Input
Sample Input and
and Output
Output
Standard Input
Standard Input Standard Output
Standard Output

720 480
720 480 450
450
640 320
640 320 630
630
800 600
800 600 319
319
2500 2500
2500 2500 319
319
00 00

Problem A-
Problem A- Page
Page 2
2 of
of 2
2
38"th ACM
38 ACM International
International Collegiate
Collegiate
Programming Contest,
Programming Contest, 2013–2014
2013-2014
Asia Region,
Asia Region, Tehran
Tehran Site
Site
Sharif University
Sharif University of
of Technology,
Technology, 19–20
19-20 December
December 2013
2013

Problem B:
Problem B: SMS
SMS Poll
Poll

‎TThehe popular
popular TV show, , “‎
TV show‎ 90%”, ‎iiss conducting
“90%”, conducting aa live
live SMS
SMS poll
poll every week. . ‎E
every week‎ Each poll simply
ach poll simply consists
consists of
of aa question
question
followed by
followed choices, , ‎n
by k choices‎ numbered
umbered form to k. ‎. ‎T
form 1 to The audiences are
he audiences are asked
asked to
to vote
vote to
to the
the choices
choices by
by sending
sending aa number
number from
from
1 to
to kK to
to the
the program's
program's phone
phone number
number via SMS. . ‎T
via SMS‎ The poll statistics
he poll statistics is
is shown
shown instantly
instantly during
during the
the program‎
program. .

‎The
T Parliament has
he Parliament has recently
recently got
got suspicious
suspicious over
over the
the statistics
statistics broadcasted
broadcasted by by this
this TV
TV show
show last week, , ‎aand
last week‎ has hired
nd has hired aa
committee to
committee to investigate
investigate the issue. . ‎T
the issue‎ The committee has
he committee has obtained
obtained the
the list
list of
of all
all SMSs
SMSs sent
sent to
to the
the last
last program
program of 90%, , ‎aand
of 90%‎ nd
is going
is going to
to process
process the
the data
data for investigation. . ‎T
for investigation‎ The poll under
he poll under investigation
investigation has
has only
only four
four choices‎
choices. .

‎Since
S the number
ince the number of of SMSs
SMSs is is pretty high, , ‎tthe
pretty high‎ committee is
he committee is asking
asking you
you toto write
write aa computer
computer program
program to to extract
extract the
the poll
poll
statistics from
statistics from thethe SMS
SMS raw data. . ‎T
raw data‎ The data is
he data is provided
provided to to you
you asas aa list
list of
of phone:content pairs‎, ‎w
phone:content pairs, where
here phone
phone is is the
the
sender’s‎
sender’s phone‎
phone number‎
number, , ‎aand content is
nd content is the
the SMS content, , ‎p
SMS content‎ resumably‎ containing‎
presumably containing the‎
the sender’s‎
sender’s vote‎
vote. . ‎Y
Your task is
our task is to
to
compute the
compute the percentage
percentage ofof votes
votes forfor each
each of of the choices, , ‎aand
the choices‎ report it
nd report it to
to the committee. . ‎T
the committee‎ There are some
here are some points
points toto be
be
considered in
considered in processing data: :
processing data‎

e In its
In its most
most general
general form‎form, , ‎aa phone
phone numbernumber is is of
of the form “A
the form “A B C”, , ‎w
B C”‎ where
hereAA is is the
the country
country code‎ code, , ‎BB isis the
the area
area
code, , ‎a
code‎ ndC C is
and is the
the local number. . ‎F
local number‎ For example, , ‎iinn‎ +‎
or example‎ 98 (21)
+98 6616-6645, , ‎ ‎ the
(21) 6616-6645‎ country code
thecountry code is‎is 98, the ‎ area
98, the‎ area code
code isis
21, , ‎aand
21‎ the local
nd the local number
number is is 6616-6645‎
6616-6645. . None None of of A,A, B,B, and
and C C can
can start
start with
with aa 0 0 digit.
digit.
e The country
The country code code is is optional,
optional, and and is is always
always preceded
preceded by by aa + + sign.
sign. If If the
the country
country code code is is not
not given,
given, it it is
is
considered as 98. The country code consists of at most 3 digits.
considered as 98. The country code consists of at most 3 digits.
e If the
If the country
country code code is is given,
given, the the number
number must must also also contain
contain an an areaarea code.
code. However,
However, if if the
the country
country code code isis
omitted, the
omitted, the area
area codecode is is optional.
optional. But But if if the
the area
area codecode is is provided,
provided, it it must
must be be preceded
preceded by by either
either aa 0 0 digit
digit or
or itit
is surrounded
is surrounded by by a a pair
pair of of parentheses
parentheses (for (for example,
example, either either 02166166600
02166166600 or or (21)66166600).
(21)66166600). If If the
the area
area code
code is is
not given,
not given, it it must
must be considered as
be considered as 21.
21. The The area
area code
code consists
consists of of at at most
most 3 3 digits.
digits.
e Local numbers
Local numbers can can have
have various
various lengths,
lengths, from from 3 3 to
to 88 digits.
digits. Examples
Examples are are 6616,
6616, and and 66166600.
66166600.
e A A phone
phone number number may may havehave been been recorded
recorded in in various formats. . ‎F
various formats‎ For example, , ‎0
or example‎ 09128122190,
9128122190‎, +‎ +98(912)812-2190,
98(912)812-2190‎,
‎aand 0912-812-2190 all
nd 0912-812-2190 all refer
refer to to the
the same
same phone number. . ‎H
phone number‎ Here are the
ere are the format
format rules‎ rules: :
o
o The area
The area code
code can can be be optionally
optionally surrounded
surrounded by by aa pair
pair ofof parentheses‎
parentheses. .
o
o There might
There might be be aa dash
dash (-) (-) between
between any any twotwo digits
digits in in the
the local
local number
number or or between
between two two partsparts of of the
the
three parts
three parts namely
namely the the country
country code, code, thethe area
area code,
code, and and thethe local
local number‎
number. . A A dash
dash and and aa parenthesis
parenthesis
can’t‎be‎adjacent.
can’t be adjacent.
e YouYou can assume
can assume that that all
all phone
phone numbers
numbers in in the
the raw
raw datadata are are valid
valid and and strictly
strictly follow
follow the the rules
rules specified
specified above‎
above. .
‎M Moreover, you can assume that any phone number in the world has a unique complete form. For example, , ‎w
oreover‎ , ‎
y ou can assume that any phone number in the world has a unique complete form‎ . ‎
F or example‎ wee
cannot simultaneously have two different numbers (218)4460 and (21)84460, as they both have the same
cannot simultaneously have two different numbers (218)4460 and (21)84460‎ , ‎
a s they both have the same
complete form‎
complete form +982184460.
+982184460.
e TheThe only only valid
valid content
content for for an an SMS
SMS is is aa number
number from from 1| to to 4‎4 (not
(not surrounded
surrounded by by white spaces). . ‎A
white spaces)‎ Any other
ny other
content, , ‎ssuch
content‎ uch as as letters
letters and‎
and + + ‎ssign,
ign‎, ‎mmakes
akes the the SMS invalid, , ‎aand
SMS invalid‎ such SMSs
nd such SMSs must must be be discarded‎
discarded. .
e A Asender
sender might might have have sentsent several
several SMSs SMSs from from aa single
single phone number. . ‎IInn this
phone number‎ case, , ‎o
this case‎ only the first
nly the first valid
valid SMSSMS
must be
must considered, , ‎aand
be considered‎ nd allall others
others must must be be discarded‎
discarded. .
e Discarded SMSs
Discarded SMSs must must not not be be included
included in in the
the total
total number
number and and inin the
the percentages‎
percentages. .

Input (Standard
Input (Standard Input)
Input)

There are
There are multiple
multiple test
test cases
cases in
in the input. . ‎T
the input‎ The first line
he first line of of each
each test
test case
case contains
contains aa positive
positive integers
integers n ((1S n<
10,000), which indicates the number of SMSs in the list. The next n lines, each contains a pair a: b, ‎, ‎w
)‎, ‎
w hich indicates the number of SMSs in the list‎. ‎
T he next lines‎ , ‎
e ach contains a pair where
here a is
is aa phone
phone
number‎ ‎ is the SMS content‎. ‎
number, and b is the SMS content. The content of each SMS is at most 30 characters where each character is
, a nd T he content of each SMS is at most 30 characters where each character is an
an
alphanumeric‎character‎or‎belongs‎to‎the‎set‎{“:”,‎“+”,
alphanumeric character or belongs to the set {“:”, “+”, “-”,‎‎“(”,‎“)”}‎ “-”, “(”, “)”}. . ‎T The input terminates
he input terminates with
with aa line
line containing
containing aa
single‎“0”‎which‎should‎not‎be‎processed.
single “0” which should not be processed. To To make
make your
your life life easier,
easier, itit is
is guaranteed
guaranteed that
that there
there is
is no
no space
space character
character in in
the input.
the input.

Output (Standard
Output (Standard Output)
Output)
For each
For each test case, , ‎o
test case‎ output four lines
utput four lines on
on the
the standard output, , ‎w
standard output‎ where line i contains
here line contains the
the percentage
percentage of
of votes
votes given
given to
to the
the
choice‎. ‎T
i choice. The percentages must
he percentages must bebe truncated
truncated toto integers‎ Then, , ‎ooutput
integers. . Then‎ the total
utput the total number
number ofof participants
participants in
in the
the poll,
poll,

Problem B
Problem B -- Page
Page 1| of
of 2
discarding duplicates
discarding duplicates SMSs.
SMSs. The
The format
format of
of the
the output
output must
must conform
conform to
to the
the format
format indicated
indicated in
in the
the Standard
Standard Output
Output
below‎
below. .

Sample Input
Sample Input and
and Output
Output

Standard Input
Standard Input Standard Output
Standard Output

‎6
6 50%
50%
09128122190:1
09128122190:1 50%
50%
+98211100:+1
+98211100:+1 0%
0%
+98 (912) 812-2190:4
+98(912)812-2190:4 0%
0%
+311 (20)590-4359:2
+311(20)590-4359:2 Participants:5
Participants:5
6616:1
6616:1 66%
66%
02166-16-00-22:2
02166-16-00-22:2 0%
0%
88 33%
33%
6616:1
6616:1 0%
0%
0216616:2
0216616:2 Participants:5
Participants:5
+98216616:3
+98216616:3
+98(21)66-16:4x
+98 (21) 66-16:4x
9090:1
9090:1
8080:2:
8080:2:
+1(519) 708-2040:3
+1(519)708-2040:3
(519) 708-2040:3:1
(519)708-2040:3:1
00

Problem B-
Problem B- Page
Page 2
2 of
of 2
2
38"th ACM
38 ACM International
International Collegiate
Collegiate
Programming Contest,
Programming Contest, 2013–2014
2013-2014
Asia Region,
Asia Region, Tehran
Tehran Site
Site
Sharif University
Sharif University of
of Technology,
Technology, 19–20
19-20 December
December 2013
2013

Problem C:
Problem C: Traitor
Traitor
According
According to to an
an intelligence
intelligence source,
source, we’ve
we’ve gotgot aa traitor
traitor inin ACM
ACM Security
Security Agency
Agency (ASA).
(ASA). ASAASA has
has a a hierarchical
hierarchical
structure where each agent has a manager and there are also some (at least one) top managers who are not
structure where each agent has a manager and there are also some (at least one) top managers who are not managed
managed byby
anyone.
anyone. Our
Our source
source doesn’t
doesn’t exactly
exactly know
know the
the traitor,
traitor, but
but he
he has
has aa list
list of
of suspects.
suspects. Therefore,
Therefore, all
all we
we know
know is is that
that there
there is
is
exactly one
exactly one traitor
traitor in
in our
our agency
agency and
and we
we have
have a alist of suspects.
list of suspects. In In order
order toto find
find that
that traitor,
traitor, we
we want
want to
to assign
assign aa watcher
watcher
for each
for each suspect,
suspect, satisfying
satisfying the
the following
following three
three conditions:
conditions:

1.
1. Two suspects
Two suspects cannot
cannot watch
watch each
each other.
other.
2.
2. Each suspect
Each suspect should
should be
be watched
watched byby either
either his
his manager
manager or
or one
one of
of his
his direct
direct employees.
employees.
3.
3. Nobody can
Nobody can watch
watch more
more than
than one
one suspect.
suspect.

If we
If we want
want to to satisfy
satisfy all
all above
above conditions,
conditions, it it may
may be be impossible
impossible to to watch
watch allall suspects.
suspects. Therefore,
Therefore, you
you should
should write
write aa
program that
program that gets
gets the
the structure
structure of of ASA
ASA and and the
the list
list of
of suspects
suspects as as the
the input
input and
and finds
finds the
the maximum
maximum number
number ofof suspects
suspects
for whom
for whom the the watcher
watcher assignment
assignment is is possible.
possible. In In the
the following
following figure
figure that
that illustrates
illustrates thethe organizational
organizational structure
structure of of
ASA with
ASA with twotwo top
top managers
managers and and eleven
eleven agents,
agents, the the suspects
suspects are are indicated
indicated with
with gray
gray color.
color. One
One watcher
watcher assignment
assignment
covering 7
covering 7 out
out of
of 88 suspects
suspects isis possible
possible in in this
this case
case which
which is is shown
shown by by arrows.
arrows. An An arrow
arrow from
from agent
agent xx to
to agent
agent y,y, means
means
agent xx is
agent is supposed
supposed to to watch
watch agent
agent y.y. It
It can
can bebe shown
shown that that in
in this
this example
example there
there is is no
no watcher
watcher assignment
assignment covering
covering allall
suspects.
suspects.

4 11

3 2 8 9 10

1 6 7

Input (Standard
Input (Standard Input)
Input)
There are
There are multiple
multiple test
test cases
cases in in the
the input.
input. The
The first
first line
line ofof each
each test
test case
case contains
contains two
two integers
integers n ((1 < n < 10,000) )
specifying the
specifying the number
number of of agents,
agents, andand k ((1 < k < n)) specifyingspecifying thethe number
number of of the
the suspected
suspected agents.
agents. TheThe agents
agents are
are
numbered from 1 to n. On the second line there are n space-separated integers, where the i‘* number is the number of
numbered from to . On the second line there are space-separated integers, where the number is the number of
agent who
agent who isis the
the manger
manger of of the
the agent
agent i.. AA zero
zero means
means the the agent
agent i is
is aa top
top manager.
manager. OnOn the
the third
third line
line there
there areare k positive
positive
integers S,,S2,...,S,, , indicating
integers indicating the the numbers
numbers of of the
the suspected agents. The
suspected agents. The input
input terminates
terminates with
with “0 0” which
“0 0” which should
should not
not
be processed.
be processed.

Output (Standard
Output (Standard Output)
Output)
For each
For each test
test case,
case, output
output in
in aa line
line the
the maximum
maximum number
number of
of suspects
suspects for
for whom
whom the
the watcher
watcher assignment
assignment is
is possible.
possible.

Sample Input
Sample Input and
and Output
Output
Standard Input
Standard Input Standard Output
Standard Output

111 88 77
WWE

3 4 44068
0 6 8 991111110
11 11 11 0 11
ONFNN SBS

3 2 88965
9 6 5 77 11
11
2 2
ODRON

0 1
1 2
0 0

Problem C
Problem C -- Page
Page 1| of
of 1|
38"th ACM
38 ACM International
International Collegiate
Collegiate
Programming Contest,
Programming Contest, 2013–2014
2013-2014
Asia Region,
Asia Region, Tehran
Tehran Site
Site
Sharif University
Sharif University of
of Technology,
Technology, 19–20
19-20 December
December 2013
2013

Problem D:
Problem D: Intelligent
Intelligent Traffic
Traffic Surveillance
Surveillance
Recently, Mr.
Recently, Mr. Hamsadeh
Hamsadeh is is working
working as as an
an ITIT engineer
engineer in in the
the central
central traffic
traffic control
control department
department of of Tehran
Tehran municipality.
municipality.
He is
He is now
now in in charge
charge of of aa newnew traffic
traffic surveillance
surveillance system
system withwith which
which the the law-breaking
law-breaking vehicles
vehicles are are going
going to to be
be fined
fined
through automatic
through automatic plate plate detection
detection and and issuing
issuing penalty
penalty tickets.
tickets. TheThe system
system has has been
been running
running for for aa fewfew weeks
weeks in in the
the
production environment
production environment and and now now is is the
the time
time for
for Mr.
Mr. Hamsadeh
Hamsadeh to to issue
issue thethe tickets.
tickets. While
While he he was
was preparing
preparing the the system,
system,
the persistence
the persistence storage
storage of of thethe database
database server
server came
came intointo aa fatal
fatal hardware
hardware crash crash andand all
all its
its data
data was
was disastrously
disastrously lost. lost.
Laughing at
Laughing at his
his own
own misery,
misery, misfortunate
misfortunate Mr. Mr. Hamsadeh
Hamsadeh startedstarted inspecting
inspecting the the other
other systems
systems in in hope
hope of of data-recovery.
data-recovery.
The only
The only available
available pieces
pieces of of information
information are are the
the log
log files
files remaining
remaining in in the
the application
application server.
server. Fortunately,
Fortunately, all all forms
forms of of
data-entry are logged in the service layer of the application server, and thus, the lost information can be totally survived
data-entry are logged in the service layer of the application server, and thus, the lost information can be totally survived
using the
using the log
log files.
files. But
But such
such data-recovery
data-recovery tasks tasks are
are too
too complicated
complicated for for Mr.
Mr. Hamsadeh
Hamsadeh and and there
there is is not
not enough
enough timetime to to
setup aa new
setup new database,
database, import
import the the recovered
recovered data data and
and reconfigure
reconfigure the the whole
whole architecture
architecture for for issuing
issuing the the tickets.
tickets. WithWith
deep feelings
deep feelings of of being
being squashed,
squashed, Mr. Mr. Hamsadeh
Hamsadeh is is asking
asking you you forfor help.
help. YouYou havehave toto write
write aa program
program that that reads
reads allall the
the
log files
log files and
and issues
issues the
the penalty
penalty tickets.
tickets. InIn the
the following
following paragraphs,
paragraphs, Mr. Mr. Hamsadeh
Hamsadeh describes
describes Tehran
Tehran traffic
traffic regulations,
regulations,
services provided
services provided by by the
the traffic
traffic surveillance
surveillance system,
system, thethe format
format of of log
log files,
files, and
and thethe rules
rules for
for issuing
issuing tickets.
tickets.
Each road
Each road isis considered
considered in in one
one ofof the
the three
three traffic-specific
traffic-specific zones
zones of of Tehran:
Tehran:
e Central Traffic
Central Traffic Restricted
Restricted Zone
Zone (CTRZ)
(CTRZ)
e Even/Odd Restricted
Even/Odd Restricted Zone
Zone (EORZ)
(EORZ)
e Unrestricted Zone
Unrestricted Zone (UZ)
(UZ)
Ordinary personal
Ordinary personal vehicles
vehicles are
are not
not normally
normally permitted
permitted to to enter
enter CTRZ
CTRZ
during these
during these time
time intervals:
intervals: ae
e = Saturday
Saturday through
through Wednesday
Wednesday from from 06:30
06:30 toto 17:00
17:00
e = Thursday
Thursday from from 06:00
06:00 toto 13:30
13:30
Ordinary personal
Ordinary vehicles whose
personal vehicles whose vehicle
vehicle registration
registration number
number ends ends with
with
an even
an even digit
digit are
are not
not permitted
permitted to to enter
enter EORZ
EORZ during
during these
these time
time intervals:
intervals:
e Sunday and
Sunday and Tuesday
Tuesday from
from 06:30
06:30 to
to 19:00
19:00
e =©Thursday from
Thursday from 06:30
06:30 toto 17:00
17:00
Ordinary personal
Ordinary personal vehicles
vehicles whose
whose vehicle
vehicle registration
registration number
number endsends with
with an
an odd
odd digit
digit are
are not
not permitted
permitted to
to enter
enter EORZ
EORZ
during these
during these time
time intervals:
intervals:
e =©Saturday, Monday
Saturday, Monday and and Wednesday
Wednesday from from 06:30
06:30 to
to 19:00
19:00
So, none
So, none of of these
these zone
zone restrictions
restrictions are are applied
applied onon Fridays
Fridays (official
(official weekends
weekends in in Iran).
Iran). Some
Some vehicles
vehicles (including
(including public
public
transportation and
transportation and emergency
emergency services)
services) are are allowed
allowed to to enter
enter CTRZ
CTRZ andand EORZ
EORZ anytime
anytime withwith nono restrictions.
restrictions. Ordinary
Ordinary
vehicles can
vehicles can also
also enter
enter these
these zones
zones if if they
they buy the permission
buy the permission for for aa single
single day,
day, butbut you
you areare not
not to
to consider
consider buying
buying
permissions in
permissions in this
this problem.
problem. If If aa vehicle
vehicle enters
enters CTRZ
CTRZ or or EORZ
EORZ unlawfully,
unlawfully, it it should
should be be fined.
fined. Each
Each vehicle
vehicle must
must bebe
fined at
fined at most
most onceonce aa day
day forfor zone
zone restriction
restriction violations.
violations. If If both
both CTRZ
CTRZ and and EORZ
EORZ violations
violations happen
happen forfor aa vehicle
vehicle onon
aa single
single day,
day, the
the CTRZ
CTRZ violation
violation is is considered
considered which
which hashas naturally
naturally aa higher
higher penalty.
penalty. EachEach road
road isis initially
initially considered
considered to to
be in
be in UZ,
UZ, butbut this
this state
state may
may change
change based
based onon announcements.
announcements. The The new
new rules
rules of of all
all such
such announcements
announcements must must be
be applied
applied
from the
from the day
day after
after the
the announcement.
announcement. In In the
the same
same way,
way, no no vehicles
vehicles are
are initially
initially exempt
exempt fromfrom zone
zone regulations,
regulations, but
but the
the
exceptions are
exceptions are added
added andand removed
removed over over time.
time. Such
Such modifications
modifications must must also
also bebe considered
considered from from the
the next
next day.
day.
The following
The following are are the
the services
services provided
provided by by the
the application
application server.
server. Each
Each service
service has
has aa corresponding
corresponding log
log message
message which
which
is written
is written onon aa single
single line
line starting
starting with
with the
the service
service name
name followed
followed by by thethe parameters.
parameters. AsAs you
you will
will see
see in
in service
service
definitions and
definitions and examples,
examples, the the parameters
parameters of of aa service
service are
are printed
printed in
in aa specific
specific order.
order. Independent
Independent ofof the
the service
service type,
type, the
the
parameter list of each log message starts with a special pair of parameters (called timestamp): “day” and “time”.”.
parameter list of each log message starts with a special pair of parameters (called timestamp): “ day ” and “ time
A timestamp
A timestamp naturally
naturally shows
shows thethe exact
exact time
time of of its
its corresponding
corresponding service
service request. Parameter ““day”
request. Parameter day” isis aa positive
positive integer
integer
showing the number of days passed from the day of system deployment (called day 0). Parameter “time”” shows
showing the number of days passed from the day of system deployment (called day 0). Parameter “ time shows thethe
time
time of
of the
the request
request on on that
that day
day in
in ““HH: mm: ss”” formatformat ((00 < HH < 23,00 , <mm,ss < 59).).
e setRoadZone(day,
setRoadZone(day, time,
time, zone,
zone, roads)
roads)
Parameter
Parameter ““zone”
zone” can
can bebe ““UZ”,
UZ”, ““CTRZ”,
CTRZ”, or
or ““EORZ”.
EORZ”. Parameter
Parameter ““roads”
roads” contains
contains aa non-empty
non-empty list
list of
of not-necessarily-
not-necessarily-
distinct road
distinct road names.
names. From
From thethe beginning
beginning of of the
the next
next day,
day, the
the corresponding
corresponding roads
roads must
must be
be considered
considered in in the
the given
given
“zone”
“zone” (overriding
(overriding their
their former
former zone
zone states).
states). Log
Log examples:
examples:

> setRoadZone
setRoadZone 2
2 "09:12:53"
"09:12:53" "CTRZ"
"CTRZ" "Enghelab"
"Enghelab" "Sa'di"
"Sa'di" "Ferdowsi"
"Ferdowsi" "Ferdowsi"
"Ferdowsi"

> setRoadZone
setRoadZone 5
5 "14:32:01"
"14:32:01" "EORZ"
"EORZ" "Resalat"
"Resalat"

> setRoadZone
setRoadZone 12
12 "00:00:59"
"00:00:59" "UZ"
"UZ" "Persian_Gulf"
"Persian Gulf"
e addZoneException(day,
addZoneException(day, time,
time, vehicles)
vehicles) | removeZoneException(day,
removeZoneException(day, time,
time, vehicles)
vehicles)

Problem D
Problem D -- Page
Page 11 of
of 3
Parameter
Parameter ““vehicles”
vehicles” contains
contains aa non-empty
non-empty list
list of
of not-necessarily-distinct
not-necessarily-distinct vehicle
vehicle registration
registration numbers.
numbers. From
From the
the
beginning of
beginning of the
the next
next day,
day, the
the corresponding
corresponding vehicles
vehicles {must
{must not
not | must}
must} bebe fined
fined in
in the
the case
case of
of entering
entering CTRZ
CTRZ oror
EORZ during
EORZ during the
the forbidden
forbidden times.
times. These
These commands
commands override
override the
the older
older state
state of
of the
the given
given vehicles
vehicles (which
(which might
might be
be the
the
same state).
same state). Log
Log examples:
examples:

> addZoneException
addZoneException 11 "09:00:13"
"09:00:13" "1234567"
"1234567" "9876543"
"9876543"

> removeZoneException
removeZoneException 33 "15:33:02"
"15:33:02" "1234567"
"1234567" "9876345"
"9876345"
e addPhotoInfo(day,
addPhotoInfo(day, time, time, photoId,
photold, road,
road, vehicles)
vehicles)
This is
This is one
one ofof the
the key
key services
services ofof the
the system.
system. It It is
is not
not called
called from
from user
user interface
interface layer.
layer. It
It is
is called
called by by another
another complex
complex
system: the
system: the external
external image
image processing
processing server
server which
which analyzes
analyzes thethe pictures
pictures taken
taken by by the
the surveillance
surveillance cameras.
cameras. This
This
service is
service is called
called when
when aa photo
photo isis taken
taken and and analyzed.
analyzed. TheThe identifier
identifier of
of the
the analyzed
analyzed photophoto is is given
given inin parameter
parameter
““photold”,
photoId”, aa positive
positive integer.
integer. Parameter
Parameter ““road”road” holdsholds the
the name
name of of the
the road
road from
from which
which the photo was
the photo was taken.
taken. The
The
image processing server detects the vehicles in the photo and extracts their registration number from their plates.
image processing server detects the vehicles in the photo and extracts their registration number from their plates.
Parameter ““vehicles”
Parameter vehicles” provides
provides the the list
list of
of these
these vehicle
vehicle registration
registration numbers.
numbers. It It is
is possible
possible for for this
this list
list to
to be empty
be empty
there might be no vehicles in the photo. The “ timestamp ” parameter here refers
as there might be no vehicles in the photo. The “timestamp” parameter here refers to the moment of taking the
as to the moment of taking the
photo. Log
photo. Log examples:
examples:

> addPhotoInfo
addPhotoInfo 18 "03:18:43"
18 "03:18:43" 3324249
3324249 "Pastor"
"Pastor" "6256256"
"6256256" "8888310"
"8888310"

> addPhotoInfo
addPhotoInfo 4 "20:47:31"
4 "20:47:31" 54 "Mokhberoddoleh,_sar-e_Sa'di"
54 "Mokhberoddoleh, sar-e Sa'di"

> addPhotoInfo
addPhotoInfo 27 "06:39:14"
27 "06:39:14" 112385612
112385612 "17-e shahrivar"
"17-e_shahrivar" "1006016"
"1006016"
You can
You can assume
assume the the following
following statements:
statements:
e Parameter
Parameter objectsobjects are are always
always in in one
one of of the
the following
following forms:forms:
> Integer
Integer numbers:
numbers: All All nonnegative
nonnegative
> Strings:
Strings: Always
Always surrounded
surrounded with with quotation
quotation marksmarks (("" ))
> Lists:
Lists: Always
Always appearing
appearing as as the
the last
last parameter,
parameter, consisting
consisting of of pace-separated
pace-separated stringsstrings through
through the the end
end of of the
the line
line
e All
All tokens
tokens including
including service
service names
names and and parameter
parameter objectsobjects (strings,
(strings, integers,
integers, andand list
list members)
members) are are separated
separated with with aa
single space.
single space.
e The
The ““time”
time” parameter
parameter which which is is inin ““HH:mm:ss” ” format, format, has has exactly
exactly 88 characters
characters and and all
all its
its three
three parts
parts are
are exactly
exactly
22 digits
digits (padded
(padded with with ““0” 0” in
in the
the case
case of of being
being less
less than
than 10).
10).
e Road
Road names
names consist
consist of of English
English alphabet
alphabet (both (both lowercase
lowercase and and uppercase
uppercase letters),
letters), digits,
digits, dash
dash (( - - ),
), underscore
underscore (( _ _),),
dot (( .. ),
dot ), comma
comma ((,, ), ), and
and single
single quotation
quotation mark mark (( '' ). ). Road
Road namesnames are are nonempty
nonempty and and no no longer
longer than
than 100 100 characters.
characters.
 All
All vehicle
vehicle registration
registration numbers
numbers are are strings
strings of of exactly
exactly 77 digits.
digits.
 Each
Each road
road or or vehicle
vehicle is is always
always referenced
referenced with with the
the same
same string.
string.
 No
No two two timestamps are exactly the same.
timestamps are exactly the same.
 IfIf there
there are are conflicting
conflicting commands
commands on on thethe same
same day,day, the the newer
newer command
command (one (one with
with the
the bigger
bigger timestamp)
timestamp) must must
override the older one. You can assume that the “addPhotoInfo” service is called at most once for each photoId.
override the older one. You can assume that the “ addPhotoInfo ” service is called at most once for each photold.
 Each
Each vehicle
vehicle registration
registration number
number appears appears at at most
most once
once in in each
each photo.
photo.
e The
The running
running time time of of the
the system
system is is atat most
most 300 days. days.
e AA vehicle
vehicle must must be be fined
fined forfor outlawed
outlawed zone zone entrance
entrance even even if if its
its photo
photo waswas taken
taken on on the
the time
time boundaries
boundaries (e.g. (e.g. at
at
Monday 6:30:00).
Monday 6:30:00).
The vehicles
The vehicles must
must be be fined
fined based
based on on photo analysis results.
photo analysis results. AsAs stated
stated before,
before, zone-entrance
zone-entrance penalty tickets should
penalty tickets should be be
issued for
issued for each
each vehicle
vehicle atat most
most once
once aa day.
day. All
All photos
photos ofof such
such aa violation
violation in in aa day
day must
must bebe attached
attached toto its
its corresponding
corresponding
penalty ticket.
penalty ticket. Photo
Photo attachments
attachments of of aa ticket
ticket must
must bebe sorted
sorted inin ascending
ascending orderorder based
based onon their
their times.
times. Note that if
Note that if both
both
CTRZ and
CTRZ and EORZ
EORZ entrances
entrances happen
happen together
together forfor aa vehicle,
vehicle, all
all photos
photos of of both
both violations
violations must
must bebe attached,
attached, but
but the
the ticket
ticket
should be
should be issued
issued with
with the
the CTRZ
CTRZ penalty.
penalty. The The order
order of
of printed tickets is
printed tickets is also
also important.
important. Tickets
Tickets must
must bebe primarily sorted
primarily sorted
based on
based on their
their vehicles
vehicles such
such that
that their
their registration
registration numbers
numbers appear
appear in in lexicographic
lexicographic order.order. Tickets
Tickets ofof aa vehicle
vehicle must
must then
then
be sorted
be sorted inin ascending
ascending order
order based
based on on the
the day
day ofof offence.
offence.

Input (Standard
Input (Standard Input)
Input)
The input
The input consists
consists of
of several
several testtest cases.
cases. Each
Each test
test case
case starts
starts with
with aa line
line containing
containing thethe single
single integer
integer N, , the
the total
total number
number
of lines in the log files (1 < N < 1000). The second line contains a string W followed by two positive integers CTP
of lines in the log files ( ). The second line contains a string followed by two positive integers
and
and EOP. . String
String W is is one
one of of the words “
the words Saturday”, ““Sunday”,
“Saturday”, Sunday”, ““Monday”,
Monday”, ““Tuesday”,
Tuesday”, ““Wednesday”,
Wednesday”, ““Thursday”,
Thursday”,
or “ Friday ”,
or “Friday”, specifying the weekday of the deployment day (day 0). Numbers CTP and EOP are the penalty values
specifying the weekday of the deployment day (day 0). Numbers and are the penalty values for
for
outlawed entrance to CTRZ and EORZ respectively (EOP < CTP). Each of the next N lines contains a service log
outlawed entrance to CTRZ and EORZ respectively ( ). Each of the next lines contains a service log in
in
the format
the format specified
specified before.
before. The The logs
logs are
are not
not necessarily
necessarily sorted
sorted in
in any
any special
special order
order as as it
it is
is the
the concatenation
concatenation result
result of
of
many log
many log files.
files. Each
Each line
line has
has atat most
most 1000 characters.
characters. All
All integers
integers in in the
the input
input are
are less
less than
than 10°. . The
The input
input terminates
terminates
with a line containing “0” (omit
with a line containing “0” (omit the quotes).the quotes).

Output (Standard
Output (Standard Output)
Output)
For each
For each test
test case,
case, you
you have
have to
to print
print the
the penalty
penalty tickets
tickets in
in the
the formerly-specified
formerly-specified order.
order. Each
Each ticket
ticket has
has aa main
main body
body and
and
one or
one or more
more attached
attached photos.
photos. The
The main
main body
body must
must be
be printed
printed first
first in
in aa single
single line
line with
with the
the following
following format:
format:

Problem D-
Problem D- Page
Page 2
2 of
of33
vehicle: ""NUMBER",
vehicle: NUMBER", day:
day: DAY, offence: ""OFFENCE",
DAY, offence: penalty: PENALTY
OFFENCE", penalty: PENALTY
Here is
Here is the
the meaning
meaning of
of the
the placeholders
placeholders inin this
this template:
template:
e NUMBER:
NUMBER: The The registration number of the law
registration number of the law breaking vehicle
breaking vehicle
e DAY: The day
DAY: The day of
of offence;
offence; numbered
numbered like
like timestamp
timestamp parameters
parameters
e OFFENCE:
OFFENCE: ItIt specifies
specifies the
the type
type of
of offence
offence andand is
is always
always one
one of
of the
the following
following strings:
strings:

> Outlawed
Outlawed entrance
entrance to
to CTRZ
CTRZ

> Outlawed
Outlawed entrance
entrance to
to EORZ
EORZ

> Outlawed
Outlawed entrance
entrance to
to CTRZ && EORZ
CTRZ EORZ
e PENALTY: The price
PENALTY: The price that
that the
the vehicle
vehicle driver
driver is
is charged
charged in
in this
this ticket
ticket
Each photo
Each photo attachment
attachment must
must appear
appear inin aa separate
separate line.
line. They
They must
must be
be printed
printed in
in the
the order
order specified
specified before.
before. Photo
Photo
attachments
attachments must
must have
have the
the format
format below:
below:
photo: PHOTO-ID,
photo: time: "
PHOTO-ID, time: "TIME",
TIME", road:road: " "ROAD"
ROAD"
Here is
Here is the
the meaning
meaning of of the
the placeholders
placeholders in in the
the template
template of of photo
photo attachments:
attachments:
e PHOTO-ID:
PHOTO-ID: It It is
is the
the identifier
identifier of
of the
the attached
attached photo.
photo. ItIt is
is used
used by
by the
the real
real ticket
ticket renderer
renderer to
to print the photo.
print the photo.
e TIME: in “ ” format (exactly the
TIME: The time of taking the corresponding photo in “HH: mm: ss” format (exactly the same as timestamp)
The time of taking the corresponding photo same as timestamp)
e ROAD:
ROAD: NameName ofof the
the road
road on
on which
which the
the corresponding
corresponding photo
photo was
was taken
taken
Follow the
Follow the specified
specified formats
formats precisely,
precisely, especially
especially the
the order
order of
of parameters,
parameters, spacing,
spacing, punctuations, and quotation
punctuations, and quotation marks.
marks.
Print
Print aa line
line containing
containing “““###”
###” between
between every
every two
two consecutive
consecutive test
test cases.
cases.

Sample Input
Sample Input and
and Output
Output
Standard Input
Standard Input
66
Friday 30000
Friday 30000 25000
25000
setRoadZone
setRoadZone 11 "09:12:53"
"09:12:53" "CTRZ"
"CTRZ" "Enghelab"
"Enghelab" "Ferdowsi"
"Ferdowsi" "Behesht"
"Behesht"
setRoadZone
setRoadZone 11 "14:32:01"
"14:32:01" "EORZ"
"EORZ" "Resalat"
"Resalat" "Damavand"
"Damavand"
addZoneException
addZoneException 11 "09:00:13"
"09:00:13" "1000100"
"1000100" "1000200"
"1000200" "1000300"
"1000300"
addPhotoInfo
addPhotoInfo 22 "13:18:43"
"13:18:43" 1004 "Enghelab"
1004 "Enghelab" "1000100"
"1000100" "1000200"
"1000200" "1000400"
"1000400" "1000105"
"1000105"
addPhotoInfo
addPhotoInfo 33 "11:55:12"
"11:55:12" 1003 "Behesht"
1003 "Behesht™ "1000400"
"1000400" "1000105"
"1000105"
addPhotoInfo
addPhotoInfo 22 "06:30:00"
"06:30:00" 1002
1002 "Resalat"
"Resalat™ "1000100"
"1000100" "1000105"
"1000105" "1000120"
"1000120" "1000400"
"1000400"
33
Sunday
Sunday 100
100 9090
setRoadZone 4
setRoadZone 4 "01:02:03"
"01:02:03" "CTRZ"
"CTRZ" "16-e_Azar"
"l6-e Azar"
addPhotoInfo
addPhotoInfo 44 "10:15:13"
"10:15:13" 211 "16-e_Azar"
211 "l6-e Azar" "1010101"
"1010101"
addPhotoInfo
addPhotoInfo 55 "20:21:42"
"20:21:42" 212 "16-e_Azar"
212 "1l6-e Azar" "2020202"
"2020202"
66
Monday 1000
Monday 1000 900900
setRoadZone
setRoadZone 13 "09:00:00"
13 "09:00:00" "CTRZ"
"CTRZ" "Azadi"
"Azadi"
addPhotoInfo
addPhotoInfo 13 "10:00:00"
13 "10:00:00" 101 "Azadi"
101 "Azadi" "1000001"
"1000001"
addPhotoInfo
addPhotoInfo 14
14 "10:00:00"
"10:00:00" 102
102 "Azadi"
"Azadi" "1000001"
"1000001"
setRoadZone
setRoadZone 15 "09:00:00"
15 "09:00:00" "UZ" "Azadi"
"UZ" "Azadi"
addPhotoInfo
addPhotoInfo 15
15 "10:00:00"
"10:00:00" 103
103 "Azadi"
"Azadi" "1000001"
"1000001"
addPhotoInfo
addPhotoInfo 16 "10:00:00"
16 "10:00:00" 104 "Azadi"
104 "Azadi" "1000001"
"1000001"
00

Standard Output
Standard Output

vehicle:
vehicle: "1000105",
"1000105", day:
day: 2,
2, offence:
offence: "Outlawed
"Outlawed entrance
entrance to
to CTRZ",
CTRZ", penalty:
penalty: 30000
30000
photo:
photo: 1004,
1004, time:
time: "13:18:43",
"13:18:43", road:
road: "Enghelab"
"Enghelab"
vehicle:
vehicle: "1000105",
"1000105", day:
day: 3,
3, offence:
offence: "Outlawed
"Outlawed entrance
entrance to
to CTRZ",
CTRZ", penalty:
penalty: 30000
30000
photo:
photo: 1003,
1003, time:
time: "11:55:12",
"11:55:12", road:
road: "Behesht"
"Behesht"
vehicle:
vehicle: "1000120",
"1000120", day:
day: 2,
2, offence:
offence: "Outlawed
"Outlawed entrance
entrance to
to EORZ",
EORZ", penalty:
penalty: 25000
25000
photo: 1002,
photo: 1002, time:
time: "06:30:00",
"06:30:00", road: "Resalat"
road: "Resalat"
vehicle:
vehicle: "1000400",
"1000400", day:
day: 2,
2, offence:
offence: "Outlawed
"Outlawed entrance
entrance to
to CTRZ
CTRZ & & EORZ",
EORZ", penalty:
penalty: 30000
30000
photo: 1002,
photo: 1002, time:
time: "06:30:00",
"06:30:00", road: "Resalat"
road: "Resalat"
photo:
photo: 1004,
1004, time:
time: "13:18:43",
"13:18:43", road:
road: "Enghelab"
"Enghelab"
vehicle:
vehicle: "1000400",
"1000400", day:
day: 3,
3, offence:
offence: "Outlawed
"Outlawed entrance
entrance to
to CTRZ",
CTRZ", penalty:
penalty: 30000
30000
photo: 1003,
photo: 1003, time:
time: "11:55:12",
"11:55:12", road: "Behesht"
road: "Behesht"
HHH
###
Hitt
###
vehicle:
vehicle: "1000001",
"1000001", day:
day: 14,
14, offence:
offence: "Outlawed
"Outlawed entrance
entrance to
to CTRZ",
CTRZ", penalty:
penalty: 1000
1000
photo:
photo: 102,
102, time:
time: "10:00:00",
"10:00:00", road:
road: "Azadi"
"Azadi"
vehicle:
vehicle: "1000001",
"1000001", day:
day: 15,
15, offence:
offence: "Outlawed
"Outlawed entrance
entrance to
to CTRZ",
CTRZ", penalty:
penalty: 1000
1000
photo:
photo: 103,
103, time:
time: "10:00:00",
"10:00:00", road:
road: "Azadi"
"Azadi"

Problem D-
Problem D- Page
Page 3
3 of
of 3
38"th ACM
38 ACM International
International Collegiate
Collegiate
Programming Contest,
Programming Contest, 2013–2014
2013-2014
Asia Region,
Asia Region, Tehran
Tehran Site
Site
Sharif University
Sharif University of
of Technology,
Technology, 19–20
19-20 December
December 2013
2013

Problem E:
Problem E: BitTorrent
BitTorrent

The BitTorrent
The BitTorrent isis aa protocol
protocol forfor transferring
transferring large
large files
files running
running overover aa peer-to-peer
peer-to-peer network
network in in which
which nodes
nodes actact as
as both
both
clients and servers, in contrast to the centralized client-server architecture where client nodes request central servers to
clients and servers, in contrast to the centralized client–server architecture where client nodes request central servers to
get resources.
get resources. Indeed,
Indeed, thethe protocol
protocol allows
allows users
users toto establish
establish aa group
group of of hosts
hosts to
to download
download and and upload
upload files
files from
from each
each
other simultaneously.
other simultaneously. Precisely,
Precisely, the
the whole
whole package
package of of files
files (so-called
(so-called torrent)
torrent) isis segmented
segmented into
into pieces
pieces as as depicted
depicted in in the
the
figure. For
figure. For instance,
instance, aa 1010 MB
MB package
package might
might bebe segmented
segmented into into exactly
exactly tenten 1M-size
1M-size pieces
pieces or
or exactly
exactly forty
forty 256KB-size
256KB-size
pieces. As
pieces. As each
each host
host (or
(or peer)
peer) receives
receives aa new
new piece
piece of of the
the torrent
torrent itit becomes
becomes aa source
source ofof that
that piece
piece for
for other
other hosts
hosts
willing to
willing to have
have that
that piece.
piece. Pieces
Pieces are
are typically
typically downloaded
downloaded non-sequentially
non-sequentially and and are
are rearranged
rearranged intointo the
the correct
correct order
order by by
hosts. Each
hosts. Each host
host independently
independently manages
manages which
which pieces
pieces mustmust bebe downloaded.
downloaded. Pieces
Pieces are
are of
of the
the same
same size
size throughout
throughout aa
single torrent
single torrent download
download except
except the
the last
last piece
piece which
which maymay havehave aa smaller
smaller size.
size.

You want
You want toto download
download aa package
package of
of files,
files, but
but you
you are
are approaching
approaching your
your monthly
monthly Internet usage limit
Internet usage limit and
and you
you don’t
don’t
want to
want to wait
wait till
till the
the next
next month.
month. You
You want
want toto download
download the
the maximum
maximum number
number of
of files
files with
with the
the bandwidth
bandwidth left.
left. Which
Which
pieces must
pieces must bebe downloaded?
downloaded?

Piece1
Piecel Piece2
Piece2 Piece3
Piece3 Piece4
Piece4 PieceS
Piece5 Piece6
Piece6
C Y ¥ Y Y
File1
Filel File2
File2 File3
File3

The package
The package of
of files
files (torrent)
(torrent)

Input (Standard
Input (Standard Input)
Input)
The input
The input contains
contains multiple
multiple test
test cases.
cases. Each
Each test
test case
case starts
starts with
with three
three space-separated
space-separated integers
integers N, , P, , and
and L where
where N is is
the number
the number of of files
files inin the
the torrent
torrent ((1 < N < 3,000),), P is is the
the size
size ofof pieces
pieces inin KB
KB ((1 < P < 1000),), and and L is is the
the
remaining kilobytes
remaining kilobytes from
from your
your monthly
monthly Internet
Internet usage
usage limit
limit ((1 < L < 10°).). TheThe second
second line
line of
of aa test
test case
case contains
contains NN
th th
space-separated positive
space-separated positive integers
integers notnot exceeding
exceeding 100,000 where where the
the i‘" integer
integer is
is the
the size
size (in
(in KB)
KB) of of the
the ii” file
file in
in the
the
terminates with “0 0 0”
torrent. The input terminates with “0 0 0” which should not be processed.
torrent. The input which should not be processed.

Output (Standard
Output (Standard Output)
Output)
For each
For each test
test case,
case, output
output aa line
line containing
containing the
the maximum
maximum number
number of
of files
files which
which can
can be
be downloaded
downloaded from
from the
the torrent.
torrent.

Sample Input
Sample Input and
and Output
Output
Standard Input
Standard Input Standard Output
Standard Output

33.33 13
13 22
55 55 77 44
77 22 16
16
66113381
11 3 3 8 1 88
000 0 00

Problem E
Problem E -- Page
Page 1| of
of 1|
38th ACM
38 ACM International
International Collegiate
Collegiate
Programming Contest,
Programming Contest, 2013–2014
2013-2014
Asia Region,
Asia Region, Tehran
Tehran Site
Site
Sharif University
Sharif University of
of Technology,
Technology, 19–20
19-20 December
December 2013
2013

Problem F:
Problem F: 2D-Solar
2D-Solar System
System
The 2D-solar
The 2D-solar system
system like
like our
our solar
solar system
system comprises
comprises BigsunBigsun (its
(its sun)
sun) and
and its
its planetary
planetary system
system of of many
many circular
circular planets
planets
orbiting around Bigsun. Due to the high gravity of Bigsun, all planets have been attracted by Bigsun. Precisely, they
orbiting around Bigsun. Due to the high gravity of Bigsun, all planets have been attracted by Bigsun. Precisely, they
orbit around
orbit around Bigsun
Bigsun while
while being
being tangent
tangent toto itit as
as depicted
depicted in in the
the figure
figure (As
(As Bigsun
Bigsun is is so
so huge,
huge, its
its boundary
boundary looks
looks like
like aa
line.) Surprisingly,
line.) Surprisingly, up up to
to the
the current
current timetime nono twotwo planets
planets have
have collided
collided withwith each
each other,
other, but
but no
no one
one knows
knows whether
whether the the
system is
system is free
free of
of collisions
collisions in in the
the future.
future. You
You are are to
to write
write aa program
program to to verify
verify whether
whether therethere isis aa possibility
possibility of
of any
any
collision in
collision in the
the future
future and
and ifif so,
so, compute
compute the the time
time atat which
which thethe first
first collision
collision happens.
happens. The The scientists
scientists of of NASA
NASA havehave
realized that
realized that each
each planet
planet inin the
the 2D-solar
2D-solar system
system moves moves withwith aa constant
constant velocity.
velocity. More
More precisely,
precisely, it it turned
turned out
out that
that the
the
motion equation
motion equation of of aa planet
planet cancan bebe described
described by the position
by the position ofof its
its touching
touching point with the
point with the boundary
boundary of of Bigsun
Bigsun through
through
time by
time by the
the linear
linear equation
equation y = at + b where where a and and b areare two
two known
known parameters
parameters and and t denotes
denotes time.
time.

Input (Standard
Input (Standard Input)
Input)
There are
There are multiple
multiple test
test cases
cases in
in the
the input.
input. Each
Each test
test case
case starts
starts with
with aa line
line containing
containing an an integer
integer n ((0 < n < 50,000) )
where n is the number of planets. The i‘ line of the next n lines contains 3 space-separated integers
where is the number of planets. The line of the next lines contains space-separated integers 1r;,, a;, , and
and b;
whose
whose absolute values are not exceeding 1,000,000,000. The number 1; which is a positive square number,
absolute values are not exceeding . The number which is a positive square number, denotes
denotes thethe
radius of
radius of Planet
Planet i and
and a; andand b; specify
specify its
its motion
motion equation,
equation, i.e.
i.e. the
the position
position ofof the
the tangent
tangent point
point of
of the
the planet
planet on on the
the
boundary of
boundary of Bigsun
Bigsun at at time
time t isis a;t + b;. . The
The input terminates with
input terminates with aa line
line containing
containing “0”“0” which
which should
should not not be
be
processed.
processed.

Output (Standard
Output (Standard Output)
Output)
For each
For each test
test case,
case, output
output aa line
line containing
containing the
the time
time at
at which
which the
the first
first collision
collision happens
happens under the assumption
under the assumption that
that the
the
current time
current time isis equal
equal to
to 00 and
and all
all planets are disjoint
planets are disjoint at
at the
the current
current time.
time. IfIf the
the system
system isis free
free of
of collisions
collisions you
you must
must
output
output “Collision-Free System”. The
“Collision-Free System”. The output
output must
must bebe rounded
rounded to to exactly
exactly two
two digits
digits after
after the
the decimal
decimal point.
point.

Sample Input
Sample Input and
and Output
Output
Standard Input
Standard Input Standard Output
Standard Output

3 1.20
1.20
BREW
CRPREPENFANWO

1 111 1 Collision-Free
Collision-Free System
System
4 33 66 3.00
3.00
9 -7 30
~l

oO
Ww

2
4 -1 1
IB
|
ra

1 1 7
~~]
bh

2
1 1 10
oO
KB

KB

1 2 5
oO
N

Problem F
Problem F -- Page
Page 1| of
of 11
38"th ACM
38 ACM International
International Collegiate
Collegiate
Programming Contest,
Programming Contest, 2013–2014
2013-2014
Asia Region,
Asia Region, Tehran
Tehran Site
Site
Sharif University
Sharif University of
of Technology,
Technology, 19–20
19-20 December
December 2013
2013

Problem G:
Problem G: Bowling
Bowling Ball
Ball
There are
There are aa lot
lot ofof mountain
mountain ranges
ranges inin the
the Neverlands.
Neverlands. EveryEvery mountain
mountain range range consists
consists of of aa number
number of of valleys
valleys andand
summits. The slope between two consecutive summit and valley is always either 1 or —1, and all the summits and
summits. The slope between two consecutive summit and valley is always either or , and all the summits and thethe
valleys have
valleys have integer
integer heights.
heights. AA bowling
bowling ballball is
is swinging
swinging on on aa part
part of
of aa mountain
mountain range range consisting
consisting of of n valleys
valleys and
and n — 1
summits. The
summits. The ball
ball is
is always
always touching
touching down
down the the surface
surface of
of the
the mountain
mountain rangerange (it(it does
does not
not jump). Mountains before
jump). Mountains before thethe
first and
first and after
after the
the last
last valleys
valleys are
are too
too high
high such
such that
that the
the ball
ball can
can never
never exitexit the
the mountain
mountain range.
range. At At time
time ty the
the ball
ball isis
located in
located in the
the valley
valley number
number ss moving
moving to to the
the upper-right
upper-right direction
direction and
and itit has
has an
an initial
initial kinetic
kinetic energy
energy Kp. . The
The following
following
nd
figure shows
figure shows aa mountain
mountain rangerange with
with 4 valleys
valleys and and 3 summits,
summits, andand the
the ball
ball located
located in in the
the 2 2"* valley
valley (enumerated
(enumerated from from
left to
left to right).
right).

3m

2m

1m

By the
By the simple
simple physics
physics we we know
know that that at
at any
any time
time t the the ball
ball has
has aa gravitational
gravitational potential energy P, = mgh and
potential energy and also
also aa
kinetic energy
kinetic energy K, = (G)mv?, , where where m is is the
the mass
mass of of the
the ball,
ball, g is is the
the constant
constant of of the
the Earth
Earth gravity
gravity (here
(here equals
equals to to 10),),
and h and
and and v areare the
the height
height and and thethe velocity
velocity of of the
the ball
ball at at time
time t.. By By the
the transformation
transformation of of energy
energy from from potential
potential to to
kinetic or
kinetic or vice
vice versa,
versa, the
the total
total energy
energy of of the
the ball
ball P, + K; is is fixed
fixed during
during itsits movements,
movements, unlessunless it it falls
falls into
into aa valley:
valley: atat
the i‘ valley
the valley (from
(from left),
left), c; units
units of of the
the kinetic
kinetic energy
energy is is lost
lost due
due toto friction
friction or or it
it stops
stops if
if its
its kinetic
kinetic energy
energy is is below
below c;
but consider
but consider no no friction
friction inin other
other locations.
locations. Note Note that
that the
the ball
ball loses
loses c, unit
unit ofof energy
energy when
when it it leaves
leaves thethe starting
starting valley
valley atat
time ty. You can assume the ball diameter is equal to 0 and its mass is equal to 1. Your task is to find the valley or
time . You can assume the ball diameter is equal to 0 and its mass is equal to 1. Your task is to find the valley or
summit at
summit at which
which the
the ball
ball will
will stop.
stop.

Input (Standard
Input (Standard Input)
Input)
There are
There are multiple
multiple test
test cases
cases in
in the
the input.
input. The
The first
first line
line of
of each
each test
test case
case contains
contains three
three space-separated
space-separated positive
positive integers
integers
n, , and
and s and
and Ky ((1 <n < 3000, 1< s <n,1< Ky < 101).). Each Each of of the
the following
following n lines
lines contains
contains two
two integers
integers h;
and c;,, the
and the height
height and
and friction
friction ofof the
the i‘ valley.
valley. TheThe j*" line
line of
of the
the next
next n — 1 lines
lines contains
contains Hj, , the
the height
height of
of The
The jh
summit from the left (0 < hj,c;,H; S 10°). It is guaranteed that at least one of cjs is greater than zero. The input
summit from the left ( ). It is guaranteed that at least one of s is greater than zero. The input
terminates with
terminates with “0
“0 00 00 0”
0” which
which should
should not
not be
be processed.
processed.

Output (Standard
Output (Standard Output)
Output)
For each
For each test
test case,
case, output
output aa line
line conforming
conforming one
one of
of the
the following
following formats
formats depending
depending onon whether
whether the
the ball
ball stops
stops at
at either
either aa
valley or a summit.
valley or a summit.
e = If
If the
the ball
ball stops
stops at
at valley number k, , output
valley number output “Valley:
“Valley: k”” (omit
(omit the
the quotes.)
quotes.)
e = If the ball stops at Summit number , output “Summit:
If the ball stops at Summit number k, output “Summit: k (omit the(omit the quotes.)
quotes.)

Problem G
Problem G -- Page
Page 1| of
of 2
Sample Input
Sample Input and
and Output
Output
Standard Input
Standard Input Standard Output
Standard Output

4 2 17 Summit:
Summit: 22
A

NRRPED
ORPRFRFNWWRERN-

1 1 Valley:
Valley: 11
2 1
1 1
1 2
3
3
2
1 1000000000000000
1 1000000000000000
ORR

1 1
0 0 00 00

Problem G-
Problem G- Page
Page 2
2 of
of 2
2
38"th ACM
38 ACM International
International Collegiate
Collegiate
Programming Contest,
Programming Contest, 2013–2014
2013-2014
Asia Region,
Asia Region, Tehran
Tehran Site
Site
Sharif University
Sharif University of
of Technology,
Technology, 19–20
19-20 December
December 2013
2013

Problem H:
Problem H: LED
LED Circuit
Circuit
You are
You are inin charge
charge of of preparing
preparing the the conference
conference hall hall for
for the
the
closing ceremony
closing ceremony of of ACM
ACM ICPC.
ICPC. You You had had hired
hired aa perfectionist
perfectionist
sentimental room
sentimental room designer
designer to to design
design an an elegant
elegant decoration
decoration for for
the old
the old hall.
hall. The
The ceremony
ceremony is is going
going to to start
start inin aa few
few hours
hours andand
the designer
the designer hashas just informed you
just informed you of of the
the completion
completion of of the
the
decoration. When
decoration. When you you reach
reach the
the conference
conference hall, hall, you
you confront
confront aa
strange circuit
strange circuit onon the
the wall.
wall. The
The designer
designer starts starts explaining
explaining the the
meanings of life and ACM ICPC hidden under each piece
meanings of life and ACM ICPC hidden under each piece ofof the
the
circuit. The
circuit. The circuit
circuit consists
consists of of LEDs
LEDs (Light(Light Emitting
Emitting Diodes)
Diodes)
interconnected with
interconnected with junctions
junctions and and wires.
wires. You You ask ask whether
whether the the
LEDs
LEDs should
should be be switched
switched on. on. “Of“Of course!”
course!” the the designer
designer
responds,
responds, “Each
“Each andand every
every LED
LED must must be be lit,
lit, otherwise
otherwise thethe whole
whole work
work is
is junk!” You look
junk!” You look around
around the
the circuit
circuit to
to find
find its
its
power plug.
power plug.

— Where is
Where is the
the power plug?
power plug?

— Huh? It’s
Huh? It’s beyond the
beyond the scope
scope ofof my
my work!
work! ItIt is
is you
you who
who has
has toto provide
provide the
the electricity
electricity to
to the
the LEDs.
LEDs. And
And be
be
careful! Do
careful! Do not
not remove
remove or or modify
modify aa single
single part
part ofof the
the circuit;
circuit; Just
Just connect
connect power
power supply
supply cables
cables to
to the
the
junctions.
junctions. II have
have to
to leave
leave right
right now.
now. II will
will send
send aa report
report of
of my
my perfect
perfect work
work to
to your
your manager.
manager. Bye.
Bye.
Left alone
Left alone with
with the
the bizarre
bizarre circuit,
circuit, you
you start
start inspecting
inspecting the
the circuit.
circuit.
Unlike incandescent
Unlike incandescent light light bulbs,
bulbs, which
which emit
emit light
light regardless
regardless of of the
the electrical
electrical polarity,
polarity, 7}
LEDs only
LEDs only emit
emit light
light with
with the
the correct
correct electrical
electrical polarity
polarity (i.e.
(i.e. when
when their
their anode
anode pin
pin has
has Anode ; Cathode
aa higher
higher electric
electric potential
potential than than the
the cathode
cathode pin).
pin). Each
Each LEDLED hashas aa minimum
minimum voltage.
voltage. An
An ~—_ >—
LED does
LED does notnot emit
emit (even
(even with
with the
the correct
correct polarity)
polarity) if if the
the electrical
electrical potential difference
potential difference
of its
of its pins
pins isis less
less than
than itsits minimum
minimum voltage.
voltage. Each
Each LEDLED has has also
also aa maximum
maximum voltage.
voltage. An
An
LED is
LED is destroyed
destroyed if if its
its electrical
electrical potential
potential difference
difference exceeds
exceeds itsits maximum
maximum voltage.
voltage.
Your inspection
Your inspection on
on the
the circuit
circuit shows
shows it
it consists
consists of
of three
three types
types of
of components:
components:
e LEDs: Fortunately,
LEDs: Fortunately, all
all LEDs
LEDs of of the
the circuit
circuit are
are of
of the
the same
same type,
type, and
and so
so having
having thethe same
same minimum
minimum voltage,
voltage, and
and
the same
the same maximum
maximum voltage.
voltage.
e Junctions: Each
Junctions: Each ofof the
the two
two pins
pins ofof an
an LED
LED in in the
the circuit
circuit is
is connected
connected to to aa junction. Junctions are
junction. Junctions are not
not only
only aa
place for
place for connecting
connecting thethe LED
LED pins,
pins, but
but also
also for
for connecting
connecting the
the wire
wire end-points.
end-points.
e Wires: Each
Wires: Each wire
wire has
has two
two end-points
end-points and and connects
connects aa junction directly to
junction directly to another
another junction forcing them
junction forcing them toto have
have
the same
the same electric
electric potential.
potential.
By connecting
By connecting external
external electrical
electrical poles
poles (with
(with different
different values
values of
of voltage)
voltage) to
to each
each ofof the
the junctions of the
junctions of the circuit,
circuit, you
you can
can
inject different
inject different electric
electric potentials
potentials toto the
the junctions.
junctions. Note that each
Note that each junction must be
junction must be connected
connected to to an
an external
external electrical
electrical
pole. Be
pole. Be careful
careful ofof short
short circuits:
circuits: the
the end-points
end-points ofof each
each wire
wire MUST
MUST havehave the
the same
same electric
electric potential.
potential. ByBy convention,
convention,
we can
we can assume
assume the the minimum
minimum electric
electric potential
potential toto be
be zero.
zero. So,
So, all
all the
the electric
electric potentials
potentials can
can bebe considered
considered to to be
be
nonnegative.
nonnegative.
Now, you
Now, you have
have to
to buy
buy an
an external
external power
power supply
supply that
that provides you with
provides you with the
the required
required electrical
electrical poles.
poles. The
The cost
cost of
of such
such
power supplies
power supplies depends
depends on
on their
their upper-bound:
upper-bound: the
the maximum
maximum voltage
voltage they
they provide.
provide.
Given the
Given the specification
specification of
of the
the LED
LED circuit,
circuit, you
you have
have to
to write
write aa program
program that
that tests
tests if
if it
it is
is possible
possible to
to light
light all
all the
the LEDs
LEDs
correctly (with no short circuits, and no LED destruction). In the case of the possibility, the program should also
correctly (with no short circuits, and no LED destruction). In the case of the possibility, the program should also
compute the
compute the minimum
minimum possible
possible upper-bound
upper-bound ofof power
power supply
supply with
with which
which all
all LEDs
LEDs cancan emit
emit light.
light.

Input (Standard
Input (Standard Input)
Input)
The input
The input consists
consists ofof several
several testtest cases.
cases. Each
Each test
test case
case describes
describes anan LED
LED circuit
circuit and
and starts
starts with
with aa line
line containing
containing
55 space-separated
space-separated integers
integers J, , L, , W, , m, , and
and M, , where
where J is
is the
the number
number ofof junctions
junctions ((2 < J < 500),), L is is the
the number
number
of LEDs
of LEDs ((1 < ZL < 5,000),), W is is the
the number
number of of wires
wires ((0 < W < 5,000),), andand m andand M are
are the
the minimum
minimum and and maximum
maximum
voltage of
voltage of LEDs
LEDs respectively
respectively ((1 < m < M < 1000).). Assume Assume that
that the
the junctions are numbered
junctions are numbered 1 through
through /. .
Each of
Each of the
the next
next L lines
lines represents
represents an
an LED
LED with two
with two space-separated
space-separated integers.
integers. The
The first
first integer
integer is
is the
the number
number of
of the
the
junction to which the anode pin
junction to which the anode pin of
of the
the LED
LED is connected and the second integer is the number of the junction for
is connected and the second integer is the number of the junction for the
the

Problem HH -- Page
Problem Page 11| of
of 2
cathode pin.
cathode pin. Then,
Then, W lines
lines follow,
follow, each
each describing
describing aa wire
wire of
of the
the circuit
circuit using
using two
two space-separated
space-separated integers:
integers: the
the
junctions the wire
junctions the wire directly
directly connects.
connects.

The input
The terminates with
input terminates withaaline
line containing
containing “0
“O 0
O 0
O 0 0” (omit
O 0” (omit the
the quotes).
quotes).

Output (Standard
Output (Standard Output)
Output)
Write aa single
Write single line
line of
of output
output for
for each
each test
test case.
case. If
If there
there isis no
no way
way to
to correctly
correctly light
light all
all the
the LEDs
LEDs inin the
the circuit
circuit of
of that
that
test case,
test case, only
only write
write the word “Impossible”
the word “Impossible” (with (with no
no quotes).
quotes). Otherwise,
Otherwise, write
write aa single
single integer:
integer: the
the minimum
minimum upper-
upper-
bound of
bound of power supply with
power supply with which
which all
all the
the LEDs
LEDs can
can be
be lit.
lit.

Sample Input
Sample Input and
and Output
Output

Standard Input
Standard Input Standard Output
Standard Output

2 1 0 33.55 3
oO
DO

WOW W
NE
OrFRPWWRBNWWERBRFRENFWENPFPWWNERFWNEF

1 2 3
OPNABANNABEFBNNWWNWWWNH
We

3 2 0 3 5 6
WNEFWNFWNDNNNN

1 2 3
Impossible
WWWeE

3 2 Impossible
3 2 0033 55 Impossible
Impossible
3 2 66
1 3 22
WRF

3 1 1135
3 5 Impossible
Impossible
1 2
2 3
3 1 2 3 5
1 2
2 3
3 1
WEF

3 3 0 3 5
1 2
2 3
1 3
3 3 0 3 6
1 2
2 3
1 3
4 2 2 2 7
1 2
3 4
3 1
2 4
4 2 2 2 7
1 2
3 4
3 2
1 4
0 0 0 0 0

Problem H-
Problem H- Page
Page 2
2 of
of 2
2
38"th ACM
38 ACM International
International Collegiate
Collegiate
acm Programming Contest,
Programming Contest, 2013–2014
2013-2014
Asia Region,
Asia Region, Tehran
Tehran Site
Site
Sharif University
Sharif University of
of Technology,
Technology, 19–20
19-20 December
December 2013
2013

Problem I:
Problem |: Mixed
Mixed Flight
Flight Plans
Plans
Ad Hoc
Ad Hoc Postal
Postal Company
Company (AHPC)
(AHPC) has has a a simple
simple strategy
strategy to
to deliver
deliver postal
postal envelopes:
envelopes: ItIt advertises
advertises for
for aa volunteer
volunteer and
and
buys for him/her the cheapest flight-plan from the source to the destination city, and gives the volunteer aa bag
buys for him/her the cheapest flight-plan from the source to the destination city, and gives the volunteer bag of
of
envelopes in
envelopes in the
the source
source airport
airport to
to be
be handed
handed over the company’s
over the company’s correspondent
correspondent in
in the
the destination
destination airport.
airport.

In addition
In addition to to direct
direct flights
flights between
between two two airports,
airports, there
there are
are indirect
indirect trips
trips with
with stops
stops inin several
several different
different intermediate
intermediate
airports. An
airports. An indirect
indirect trip
trip passenger
passenger must must always
always start
start from
from the
the first
first airport
airport and
and visit
visit the
the intermediate
intermediate airports
airports
consecutively (according
consecutively (according to to the
the indirect
indirect trip
trip schedule,
schedule, without
without using
using anyany other
other direct
direct flights
flights or
or indirect
indirect trips
trips in
in the
the
middle) but
middle) but may
may leave
leave the
the rest
rest ofof the
the indirect
indirect trip
trip at
at any
any intermediate
intermediate airport.
airport. The
The price
price of of an
an indirect
indirect trip
trip is
is fixed;
fixed; nono
matter if
matter if the
the passenger
passenger uses
uses all
all the
the flights
flights oror interrupts
interrupts inin the
the middle.
middle. A A flight-plan
flight-plan can
can consist
consist ofof several
several direct
direct flights
flights
and whole
and whole or or partial
partial of
of indirect
indirect trips.
trips.

In sake
In sake of
of economy,
economy, AHPCAHPC is is looking
looking to to use
use mixed
mixed flight
flight plans: suppose aa couple
plans: suppose couple ofof bags,
bags, one
one should
should be be delivered
delivered from
from
AA to
to BB and
and another
another from
from CC to
to DD (A,
(A, B, C, DD are
B, C, are different
different airports).
airports). The
The company
company may may buy
buy two
two flight
flight plans
plans from
from A
A to
to DD
for the
for the first
first volunteer,
volunteer, andand from
from C C to
to BB for
for the
the second
second one,
one, such
such that
that the
the two
two plans
plans share
share the
the same
same airport
airport M
M (not
(not
necessarily different
necessarily different from
from these
these 4 4 airports)
airports) atat which
which the the two
two volunteers
volunteers meet
meet and
and exchange
exchange their
their bags.
bags. Hence,
Hence, AHPC
AHPC
ensures each
ensures each bagbag is
is delivered
delivered toto the
the right
right destination,
destination, whilewhile the
the total
total price
price might
might bebe reduced.
reduced.

M
4
50
100

B A
100 3
5
100 100
1 100
50 50
50
D

C 2
6 50

As an
As an example,
example, six six airports
airports together
together with
with direct
direct flights
flights (solid
(solid arrows),
arrows), indirect
indirect trips
trips (dash
(dash andand dot
dot arrows)
arrows) and and their
their
prices are
prices are illustrated
illustrated in in the
the above
above figure.
figure. Two
Two bags
bags should
should bebe sent,
sent, one
one from
from thethe airport
airport 3 ((A)) to to 5 ((B),), another
another fromfrom 6
((C)) to
to 1 ((D).). The
The company
company might might purchase
purchase ((3,4),), ((4,5)) as
as the
the flight-plan
flight-plan of of the
the first
first volunteer,
volunteer, and and ((6,5),), ((5,1)) for
for the
the
second with
second with thethe total
total cost
cost ofof 300. . But
But the
the cheaper
cheaper alternative
alternative would
would be be ((3,4,1,2,6) ) for
for the
the first
first and
and ((6,2),), ((2,4,5) ) for
for the
the
second volunteer,
second volunteer, with with thethe total
total cost
cost ofof 250. . The
The volunteers
volunteers meet
meet andand exchange
exchange bags bags atat airport
airport 4 ((M) ) and and the
the first
first
volunteer will
volunteer will leave
leave the
the indirect
indirect trip
trip at
at airport
airport 1..

You should
You should write
write aa program
program that
that given
given flights
flights information,
information, finds
finds the
the most
most economic
economic cost
cost for
for delivery
delivery of
of the
the bags.
bags.

Input (Standard
Input (Standard Input)
Input)
There
There are
are multiple
multiple cases
cases in
in the
the input.
input. The
The first
first line
line of of each
each case
case contains
contains six six integers
integers n, , m, , A, , B, , C, , and
and D, , where
where n (4 (4
< n < 100) is the number of airports, and m (0 < m < 10,000) is the total number of direct flights and indirect
) is the number of airports, and ( ) is the total number of direct flights and indirect
trips, where
trips, where at at most
most 1000 of of them
them areare indirect
indirect trips.
trips. TheThe i line
line of
of the
the next
next m lines
lines starts
starts with
with two
two positive
positive integers
integers p;
(the price, pj < 10°) and s; (being 1 for direct flight and more for indirect trip), followed by s; + 1 distinct
(the price, ) and (being 1 for direct flight and more for indirect trip), followed by distinct airport
airport
numbers that
numbers that show
show the
the order
order of
of airports
airports visited
visited inin the
the i direct
direct flight/indirect
flight/indirect trip.
trip. The
The input terminates with
input terminates with “0“0 00 00 0
0 00
0” which
0” which should
should notnot be
be processed.
processed.

Output (Standard
Output (Standard Output)
Output)
For each
For each test
test case,
case, output
output the
the total
total cost
cost of
of the
the cheapest
cheapest flight
flight plan
plan in
in a a line,
line, or output “Impossible!”
or output “Impossible!” (without
(without quotes)
quotes) if
if
delivery of
delivery of at
at least
least one
one bag
bag is
is impossible.
impossible.

Problem II -- Page
Problem Page 1| of
of 2
Sample Input
Sample Input and
and Output
Output
Standard Input
Standard Input Standard Output
Standard Output

6 9 3 5 6 1 250
250
100 1 3 4 Impossible!
Impossible!
50 1
50 166 2 2 Impossible!
Impossible!
100 2
100 22 2 4 45 5
50 1
50 166 5 5
100 1 1 3
100 4 3 4 1 2 6
100 1 5 1
50 1 4 5
50 1 2 3
4 0 1 2 3 4
5 2 1 2 3 4
10 4 1 2 5 3 4
20 1 3 5
0 0 0 0 0 0

Problem I-
Problem I- Page
Page 2
2 of
of 2
2
38"th ACM
38 ACM International
International Collegiate
Collegiate
Programming Contest,
Programming Contest, 2013–2014
2013-2014
Asia Region,
Asia Region, Tehran
Tehran Site
Site
Sharif University
Sharif University of
of Technology,
Technology, 19–20
19-20 December
December 2013
2013

Problem J:
Problem J: Sweeping
Sweeping Robot
Robot
A robot
A robot equipped
equipped with with aa camera
camera is is to
to sweep
sweep some
some areas
areas inin aa museum.
museum. The The museum
museum is is aa polygon
polygon with
with only
only horizontal
horizontal
and vertical walls as depicted in the figure. We assume that the polygon is drawn on an underlying mesh of
and vertical walls as depicted in the figure. We assume that the polygon is drawn on an underlying mesh of 1x1
1x1 cells
cells
and its vertices are on the mesh vertices. The robot is also restricted to move only along the mesh edges either
and its vertices are on the mesh vertices. The robot is also restricted to move only along the mesh edges either
horizontally or
horizontally or vertically.
vertically. ItsIts camera
camera sweepssweeps everything
everything that
that cancan bebe seen
seen onon the
the perpendicular directions of
perpendicular directions of its
its moving
moving
path. That
path. That is,is, when
when the the robot
robot moves
moves horizontally,
horizontally, its
its camera
camera is is directed
directed vertically
vertically and
and cancan only
only see
see any
any visible
visible items
items
located on
located on the
the north
north andand south
south ofof the
the horizontal
horizontal segment
segment of of the
the moving
moving path.
path. Similarly,
Similarly, itsits camera
camera cancan see
see any
any visible
visible
cells located
cells located on on the
the east
east and
and west
west ofof the
the vertical
vertical segment
segment of of the
the moving
moving path.
path. In In the
the figure,
figure, aa polygon
polygon andand aa moving
moving path path
of our
of our robot
robot (the
(the dashed
dashed line
line path)
path) areare shown.
shown. Here,
Here, the
the dotted
dotted squares
squares areare seen.
seen.

0
0 2 4 6

Given such
Given such aa polygon
polygon and
and aa robot-path
robot-path inside
inside it,
it, the
the problem
problem is
is to
to compute
compute the
the total
total areas
areas (total
(total number
number of
of squares)
squares) seen
seen
by the
by the robot.
robot.

Input (Standard
Input (Standard Input)
Input)
There are
There are multiple
multiple testtest cases
cases inin the
the input.
input. Each
Each test
test case
case starts
starts with
with aa line
line containing
containing two two integers
integers n and andk ((2 < n,k <
100) ) where
where n is is the
the number
number of of walls
walls (or
(or vertices)
vertices) of of the
the museum
museum and and k is is the
the number
number of of the
the vertices
vertices ofof the
the robot
robot path.
path.
The next
The next n lines
lines describe
describe the the museum
museum vertices.
vertices. The
The if” line line contains
contains 2 space-separated
space-separated non-negative
non-negative integers
integers x; and and y;
not exceeding
not exceeding 500 500 denoting
denoting the the x andand y coordinates
coordinates of of the
the i'* vertex
vertex of of the
the museum,
museum, respectively.
respectively. There There is is aa wall
wall
between vertices
between vertices i and and i + 1 (You (You may may assume
assume thethe (n + 1)" vertex vertex is is the
the first
first vertex).
vertex). TheThe next
next k lines
lines describe
describe thethe
vertices of
vertices of the
the robot
robot path
path inin order
order ofof appearing
appearing on on the
the path
path from
from the
the starting
starting point
point to
to the
the ending
ending point;
point; each
each lineline contains
contains
two integers
two integers which
which are are the
the x andand y coordinates
coordinates of of aa vertex.
vertex. The The robot
robot path
path is is guaranteed
guaranteed to to be
be inside
inside the
the museum
museum but but its
its
vertices (not
vertices (not its
its edges)
edges) cancan touch
touch thethe museum
museum walls.
walls. Note
Note thatthat the
the robot
robot path may intersect
path may intersect itself.
itself. The
The input
input terminates
terminates
with
with aa line
line containing
containing “0 “0 0”
0” which
which should
should notnot be
be processed.
processed.

Output (Standard
Output (Standard Output)
Output)
For each
For each test
test case,
case, output
output the
the total
total area
area (total
(total number
number of
of squares)
squares) seen
seen by
by the
the given
given robot.
robot.

Problem JJ -- Page
Problem Page 1| of
of 2
Sample Input
Sample Input and
and Output
Output
Standard Input
Standard Input Standard Output
Standard Output

20 4 10
10
wS
oO
ON
DWWNHNOOrRPERFRNNWWHKHKHOOBBWWNNEF

0 2
NY

1 2
EN

1 1
2 1
BWWOONNE

2 2
3 2
3 0
4 0
4 3
5 3
5 4
4 4
ONBPBWWWUOUDDOOP

4 5
3 5
3 6
2 6
2 5
1 5
1 3
0 3
2 3
2 4
3 4
3 2
0 0

Problem J-
Problem J- Page
Page 2
2 of
of 2
2
38"th ACM
38 ACM International
International Collegiate
Collegiate
Programming Contest,
Programming Contest, 2013–2014
2013-2014
Asia Region,
Asia Region, Tehran
Tehran Site
Site
Sharif University
Sharif University of
of Technology,
Technology, 19–20
19-20 December
December 2013
2013

Problem K:
Problem K: Wedding
Wedding Hall
Hall
Kamran has
Kamran has recently
recently bought
bought aa rectangular
rectangular flat flat garden
garden in in an
an awesome
awesome part part ofof the
the countryside.
countryside. His His business
business plan plan isis to
to
construct a Hall to host wedding ceremonies, since the countryside has recently attracted a lot of attention for being aa
construct a Hall to host wedding ceremonies, since the countryside has recently attracted a lot of attention for being
fantastic area
fantastic area to to host
host wedding
wedding ceremonies.
ceremonies. As As women
women and and men
men sections
sections mustmust be be separated
separated based
based on on the
the nation
nation law,law, hehe
thinks of
thinks of designing
designing the the hall
hall inin three
three sections:
sections: men men section,
section, women
women section
section andand common
common section
section (including
(including rest rest rooms,
rooms,
dinner room
dinner room and and etc).
etc). As As the
the common
common section
section must must bebe easily
easily accessible
accessible to to all
all persons,
persons, it it must
must be designated in
be designated in the
the
middle of
middle of the
the other
other twotwo sections.
sections. Among
Among several
several proposal
proposal designs,
designs, Kamran
Kamran has has selected
selected the
the one
one depicted
depicted below
below where
where
all three
all three sections
sections areare squares
squares of of the
the same
same size,
size, they
they are
are attached
attached to to each
each other
other like
like anan LL shape,
shape, their
their sides
sides are
are parallel
parallel to to
the garden
the garden sides,
sides, and
and the the visible
visible sides
sides of
of the
the common
common section
section from
from outside
outside face
face thethe south
south and
and west
west ofof the
the garden.
garden. Now Now
the main
the main question
question is is where
where the the hall
hall must
must bebe constructed.
constructed. The The garden
garden isis full
full of
of old
old trees
trees and
and cutting
cutting the
the trees
trees isis forbidden
forbidden
due to high air pollution. He kindly asks you to help him to find the largest hall that he can construct.
due to high air pollution. He kindly asks you to help him to find the largest hall that he can construct.

Women
Women
Section
Section

Common
Common Men
Men
Section
Section Section
Section

Input (Standard
Input (Standard Input)
Input)
There are
There are multiple
multiple test
test cases
cases inin the
the input.
input. Each
Each testtest case
case starts
starts with
with aa line
line containing
containing aa non-negative
non-negative integers
integers n ((1 <
n < 50,000) ) and and two
two positive
positive integers
integers a andand b (all
(all not
not exceeding
exceeding 1,000,000) ) where
where n is is the
the number
number ofof distinct
distinct trees
trees inin
the garden
the garden and and a and
and b specify
specify thethe sides
sides ofof the
the garden.
garden. Precisely,
Precisely, [0,a] x [0,b] denotes
denotes the the rectangle
rectangle modeling
modeling the the
garden. The
garden. The next
next n lines,
lines, each
each contains
contains 2 space-separated
space-separated non-negative
non-negative integers
integers x; and
and y; ((0<x,<a,0<y; <b) )
denoting the x and y coordinates of a tree, respectively. You may assume that trees have
denoting the and coordinates of a tree, respectively. You may assume that trees have distinct
distinct coordinates
coordinates andand the
the
south side (i.e. [0,a]) and the west side (i.e. [0, b]) of the garden lie on the x-axis and the y-axis, respectively. The
south side (i.e. ) and the west side (i.e. of the garden lie on the -axis and the -axis, respectively. The
input terminates
input terminates withwith aa line
line containing
containing “0 “0 00 0”
0” which
which should
should not
not be
be processed.
processed.

Output (Standard
Output (Standard Output)
Output)
For each
For each test
test case,
case, output
output the
the area
area of
of the
the largest
largest hall
hall that
that Kamran
Kamran can
can construct
construct in
in his
his garden.
garden. Note
Note that
that the
the hall
hall can
can
touch the
touch the trees
trees or
or the
the garden sides but
garden sides but it
it can’t
can’t interiorly
interiorly include
include them.
them. The
The output
output must
must be
be rounded to “exactly”
rounded to “exactly” two
two
digits after
digits after the
the decimal
decimal point.
point.

Sample Input
Sample Input and
and Output
Output
Standard Input
Standard Input Standard Output
Standard Output

2 3 5 6.75
W
OrRNN

2 2
OrPN

1 4
0 0 0

Problem KK -- Page
Problem Page 11 of
of 11

You might also like