Vases (Intermediate)

You might also like

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

AUSTRALIAN

INFORMATICS OLYMPIAD
Intermediate Paper

THURSDAY 22 AUGUST 2019

Duration: 3 hours
6 questions
All questions may be attempted

Important Notes
Some final reminders for competitors:
• Solutions may be submitted in C, C++, C#, Pascal, Java, PHP, Python 2 or Python 3.
• You should submit your solutions directly via the contest system during the three hours of the contest.
Please read the Contest Rules in the Handbook if you have not yet done so.
• Some languages have special constraints:
–– C and C++ programmers may only #include headers from the standard C and C++ libraries. In
particular, C++ programmers are allowed to use the string class and container classes such as
vector, list, and set.
–– Pascal programmers may not import any units except for Math, Strings and/or SysUtils.
–– Java programmers may not use any classes aside from those in packages java.lang, java.io
and java.util. Solutions must be contained in a single class called Solution, and must be run
from the routine
public static void main(String[] args)
within this Solution class. Further details can be found in the Contest Rules section of the
Handbook.
–– PHP programmers may not use any functions provided by extensions or external libraries.
–– Python programmers do not have any restrictions on imports.
• Question statements and solution templates can be downloaded from the contest system
http://aio.edu.au/contest, under the Templates & Downloads page for each problem. These
templates perform all the necessary file input and output for each problem. We strongly recommend
using these as a starting point for your solutions. It is particularly recommended that Java and PHP
programmers use these templates, due to the unusual constraints described above.
• Don’t leave all your submissions until the last few minutes! Submitting your solutions may take a
little time, and once your three hours are over the submission site will not accept any more solutions.
The best strategy is to submit solutions as you write them. If you improve a solution later in the
contest, you can always resubmit it. Your score for a problem is the maximum score among all of
your submissions to that problem.
• Click the View details button after each submission to see a score breakdown, possible reasons
why your submission might not be doing as well as you’d expect (e.g. output isn’t correct, program
exceeding the time/memory limits etc) and/or messages from the compiler. Check the Documentation
page for an explanation of some of these reasons/messages.
• To ask the judges a question, use the Ask a question/messages page in the contest system to ask
a question and we will respond as soon as possible. There is no penalty for doing so, so feel free
to do so, if you require. Any contest-wide announcements will also appear on this page. If you are
unable to do so, (e.g. if you lose your internet connection), please ask your teacher to contact either
Mr Joshua Lau on 0466 964 264 or Mr Kevin Tran on 0421 386 818.
Please see the full contest rules for further information. Good luck!
2019 Australian Informatics Olympiad (Intermediate) — Why Did I Score Zero?2

2019 Australian Informatics Olympiad (Intermediate) — Why Did I Score Zero? 3

Why Did I Score Zero?

This document will be made available for download from the contest website.
Candidates sometimes score zero even though they believe that they have a working solution to
a problem. They are advised to check the Submission details pop-up of each submission to see
the reasons why their solution was not judged as correct. An explanation of the messages on that
page can be found on the Documentation page. Below are some of the common reasons that good
solutions score zero.
Note that examples of solutions that score 100% in the various AIO languages can be found on
the website http://aio.edu.au.

Incorrect Input and Output Files


Each problem statement lists the names of its input and output files, similar to the example below.
Input File: zeroin.txt
Output File: zeroout.txt
In this example, if you try to open any of
• "a:\zeroin.txt"
• "c:\mydir\zeroin.txt"
• "input.txt"
then the file you are looking for will almost certainly not be on the judging machine and your
program will score zero. Just open "zeroin.txt" without any additional directory information.
The same goes for the output file.

Keyboard and Mouse Input


Your program should not be interactive. It should not have a graphical user interface. It should
simply read from the input file, write to the output file, and exit. If your program requires any
input from the user, the judging software will not supply this input and you will exceed the time
limit. Examples include:
• ‘Please enter the following value...’
• ‘Press any key to exit...’
• Providing a form on which the user has to click a button to start the program.

Incorrect Output Format


Each problem is very precise about how the output file should be formatted. Your score is assigned
by a judging program which tries to automatically extract your solution from your output file.
Every problem statement includes sample input and output files, as a way of illustrating these
formats. For a problem if, the sample output file contains the single line:

In this example, the following output files would almost certainly score zero:
• The answer is 4.
• "4"
2019 Australian Informatics Olympiad (Intermediate) — Why Did I Score Zero?3

2019 Australian Informatics Olympiad (Intermediate) — Why Did I Score Zero? 4

Compilation failed: Incorrect Java Class Name


When using Java, your code should be contained within a single class called Solution. If this is not
the case, you are likely to receive a ‘Compilation failed’ verdict for your submission. Additionally,
when you click on the View details button, you will see the compiler output a message similar to
No class named Solution.

Incorrect Problem/Language Selected


You must submit your solution to the correct problem. Double-check that the name of the problem
matches your solution. Additionally, you must select the correct language from the drop-down box
when submitting your solution. This is especially important if you are using Python as you must
specify if your solution is written in Python 2 or Python 3.

Incorrect Mode for Output File


You must only open the output file for writing, not writing and updating. In particular, in Python
you must open the output file with mode w not w+. This is one of the reasons you will receive
‘Execution failed because the return code was nonzero’ as feedback. The best way to avoid this
type of error is to use the solution templates.

Subtasks
Points for submissions are awarded in Subtasks. Your solution must be judged as ‘Correct’ for all
judges’ test cases within a subtask for you to obtain those points. If your program exceeds the
time limit, produces an incorrect output or a runtime error on even a single case, you will score
zero for that subtask.

Recursion too deep in Python


When using Python, there is a limit on the depth of recursive calls, which defaults to 1000. If
you are using recursion, you may wish to add the following lines to the start of your program, to
increase this limit:

import sys
sys.setrecursionlimit(1000000000) # a large number

Note that if you recurse too deep (or infinitely deep), you may still consume time and memory
exceeding the limits given in the problem statements before violating the increased limit. This will
still result in your program crashing.

Violating Contest Rules


Each year a few programs are submitted that violate the contest rules. Be sure to read the Program
Restrictions section of the rules, which details specific restrictions for each programming language.
If you are unsure about anything before or after the contest then please email aioquery@amt.edu.au
for clarification.
2019 Australian Informatics Olympiad (Intermediate) — Vases4

2019 Australian Informatics Olympiad (Intermediate) — Vases 5

Problem 1
Vases

Input File: vasesin.txt


Output File: vasesout.txt

Time and Memory Limits: 1 second, 1 GB

You have bought N identical flowers to arrange into three vases. As an expert in interior design,
there are three important rules you must follow:

1. Every flower must go into one of the three vases, since throwing flowers away is wasteful.

2. Each vase must contain at least one flower, since an empty vase looks very odd.

3. Each vase must contain a different number of flowers, so all the vases look different.

Your task is to determine a possible way to arrange the flowers. Note that there may be multiple
possible solutions, or none at all.

Input
The only line of input will contain a single integer: the number of flowers N .

Output
Your program should output three space-separated integers, describing how many flowers to put
into each vase. If there are multiple possible ways to arrange the flowers, any will do.
If it’s impossible to place flowers according to the rules, print 0 0 0 instead.

Sample Input 1 Sample Input 2 Sample Input 3


15 105 4

Sample Output 1 Sample Output 2 Sample Output 3


4 5 6 100 2 3 0 0 0

Explanation
In the first sample input, you have N = 15 flowers. One way you can arrange the flowers is to put
4 flowers in the first vase, 5 flowers in the second vase and 6 flowers in the third vase. There are
other possible solutions.
In the second sample input, you have N = 105 flowers. One way you can arrange the flowers
is to put 100 flowers in the first vase, 2 flowers in the second vase and 3 flowers in the third vase.
There are other possible solutions.
In the third sample input, you have N = 4 flowers. It is impossible to arrange flowers in vases
according to your interior design rules, so the only correct output is 0 0 0.
2019 Australian Informatics Olympiad (Intermediate) — Vases5

2019 Australian Informatics Olympiad (Intermediate) — Vases 6

Subtasks & Constraints


For all test cases, 1 ≤ N ≤ 100 000. Additionally:

• For Subtask 1 (20 marks), N = 10.


Hint: There is only one test case in this subtask, so try working out the answer by hand.

• For Subtask 2 (30 marks), N = 1000.


Hint: There is only one test case in this subtask, so try working out the answer by hand.
• For Subtask 3 (35 marks), N ≥ 1000.
Hint: Can you modify your answer for N = 1000 to work for larger values of N ?

• For Subtask 4 (15 marks), no further constraints apply.


Hint: Think about when it is impossible to arrange the flowers.
2019 Australian Informatics Olympiad (Intermediate) — RPS6

2019 Australian Informatics Olympiad (Intermediate) — RPS 7

Problem 2
RPS

Input File: rpsin.txt


Output File: rpsout.txt

Time and Memory Limits: 1 second, 1 GB

Rock. Paper. Scissors. The rules are simple. The game is contested by two people over N
rounds. During each round, you and your opponent simultaneously throw either Rock, Paper or
Scissors. Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. If your throw beats
your opponent’s, you gain one point. Conversely, if their throw beats yours, you lose one point.
Your opponent is very predictable. You know that they will throw Rock in the first Ra rounds,
throw Paper in the next Pa rounds, then finally throw Scissors in the last Sa rounds, where
Ra + Pa + Sa = N .
You will throw Rock in Rb rounds, Paper in Pb rounds, and Scissors in Sb rounds, where
Rb + Pb + Sb = N . However, as you are an experienced player, you may throw these in any order
you like.
At the beginning of the game, you start with 0 points. What is the maximum number of points
you can finish with?

Input
• The first line of input contains a single integer N , the number of rounds.
• The second line of input contains three space-separated integers: Ra , Pa and Sa .
• The third line of input contains three space-separated integers: Rb , Pb and Sb .

Output
Your program should output a single integer: the maximum number of points you could score after
all N rounds have been played. Note that this number may be positive, negative, or zero.

Sample Input 1 Sample Input 2 Sample Input 3


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

Sample Output 1 Sample Output 2 Sample Output 3


4 -1 0
2019 Australian Informatics Olympiad (Intermediate) — RPS7

2019 Australian Informatics Olympiad (Intermediate) — RPS 8

Explanation
In the first sample input, there are N = 5 rounds, where your opponent will throw Rock twice,
then Paper twice, then Scissors once. You must throw Rock once, Paper three times and Scissors
once. Here is one way you can order your throws:

Round Your Throw Their Throw Your Result Points


1 Paper Rock Win! +1
2 Paper Rock Win! +1
3 Scissors Paper Win! +1
4 Paper Paper Draw 0
5 Rock Scissors Win! +1

This results in a total of 4 points, which is the maximum possible.

In the second sample input, there are N = 4 rounds, where your opponent will throw Rock
once, then Paper twice, then Scissors once. You must throw Rock four times, and cannot throw
Paper or Scissors.
There is only one way you can order your throws:

Round Your Throw Their Throw Your Result Points


1 Rock Rock Draw 0
2 Rock Paper Lose... −1
3 Rock Paper Lose... −1
4 Rock Scissors Win! +1

This results in a total of −1 points, which is the maximum possible. Note that the answer can
be negative!

In the third sample input, there is N = 1 round. Your opponent will throw Scissors in this one
round, and you will also throw Scissors. This results in a draw, so you will score 0 points.
2019 Australian Informatics Olympiad (Intermediate) — RPS8

2019 Australian Informatics Olympiad (Intermediate) — RPS 9

Subtasks & Constraints


For all test cases:

• 1 ≤ N ≤ 5000.

• 0 ≤ Ra , Pa , Sa , Rb , Pb , Sb .
• Ra + Pa + Sa = N (that is, your opponent makes exactly N throws).

• Rb + Pb + Sb = N (that is, you make exactly N throws).


Furthermore:
• For Subtask 1 (10 marks), N = 1. Sample Input 3 is an example of a case that could be in
this subtask.
Hint: There is only one round, so all you need to do is check whether your throw wins, loses,
or draws against your opponent, and output 1, -1 or 0 respectively.

• For Subtask 2 (25 marks), you only throw Rock. More formally, Rb = N , Pb = 0 and Sb = 0.
Sample Input 2 is an example of a case that could be in this subtask.
Hint: Since you’re only throwing Rock each time, you just need to check which of your
opponent’s throws wins, loses or draws against Rock, and count the results appropriately.
• For Subtask 3 (25 marks), your opponent only throws Rock. More formally, Ra = N , Pa = 0
and Sa = 0.
Hint: The solution to this subtask is quite similar to subtask 2.

• For Subtask 4 (20 marks), neither you nor your opponent throws Scissors. More formally,
Sa = 0 and Sb = 0.
Hint: Paper will never lose, since no one ever throws Scissors. Likewise, Rock will never win.

• For Subtask 5 (20 marks), no further constraints apply.


Hint: Win as many rounds as you can! Then try to draw the rest, though sometimes, you
can’t avoid losing.
2019 Australian Informatics Olympiad (Intermediate) — Hiring Monks9

2019 Australian Informatics Olympiad (Intermediate) — Hiring Monks 10

Problem 3
Hiring Monks

Input File: hirein.txt


Output File: hireout.txt

Time and Memory Limits: 1 second, 1 GB

High up in the peaks of Kosciuszko National Park, an elite sect of monks are deciding how to
assign jobs to the new monks.
They have hired N new monks, numbered from 1 to N , each possessing a possibly different
skill level. The monk i has a skill level of xi .
There are S student jobs available, numbered from 1 to S, created for monks to learn from
their masters. As such, there is a limit on how much skill a monk can have for this job. Student
job j is available to monks with a skill level at most sj .
There are M master jobs available, numbered from 1 to M , created for monks to teach their
students. As such, there is a minimum skill level a monk must have for this job. Master job k is
available to monks with a skill level at least mk .

Each monk can be assigned at most one job, and each job can be assigned to at most one monk.
What is the largest number of monks you can assign to jobs?

Input
• The first line contains the integer N , the number of monks. Then, N lines follow. The ith
of these lines contains the integer xi , the skill level of monk i.
• The next line contains the integer S, the number of student jobs (which could be zero). Then,
S lines follow. The jth of these lines contains the integer sj .
• The next line contains the integer M , the number of master jobs (which could be zero).
Then, M lines follow. The kth of these lines contains the integer mk .

Output
Your program should output a single integer: the maximum number of monks who can be assigned
to jobs.
2019 Australian Informatics Olympiad (Intermediate) — Hiring Monks10

2019 Australian Informatics Olympiad (Intermediate) — Hiring Monks 11

Sample Input 1 Sample Input 2


5 4
100 10
300 10
20 20
40 20
1000 3
2 15
50 100
110 100
3 0
300
2500
600

Sample Output 1 Sample Output 2


4 3

Explanation
In the first sample input, one way you can assign the monks is as follows:

Monk #1 Monk #2 Monk #3 Monk #4 Monk #5


Skill: 100 Skill: 300 Skill: 20 Skill: 40 Skill: 1000

Student Job #1 Student Job #2


Skill ≤ 50 Skill ≤ 110

Master Job #1 Master Job #2 Master Job #3


Skill ≥ 300 Skill ≥ 2500 Skill ≥ 600

This assigns four monks, which is the maximum possible.


2019 Australian Informatics Olympiad (Intermediate) — Hiring Monks11

2019 Australian Informatics Olympiad (Intermediate) — Hiring Monks 12

In the second sample input, one way you can assign the monks is as follows:

Monk #1 Monk #2 Monk #3 Monk #4


Skill: 10 Skill: 10 Skill: 20 Skill: 20

Student Job #1 Student Job #2 Student Job #3


Skill ≤ 15 Skill ≤ 100 Skill ≤ 100

This assigns three monks, which is the maximum possible.


2019 Australian Informatics Olympiad (Intermediate) — Hiring Monks12

2019 Australian Informatics Olympiad (Intermediate) — Hiring Monks 13

Subtasks & Constraints


For all cases:

• 1 ≤ N ≤ 100 000.

• 0 ≤ S ≤ 100 000.
• 0 ≤ M ≤ 100 000.

• 1 ≤ xi ≤ 1 000 000 000 for all i.


• 1 ≤ sj ≤ 1 000 000 000 for all j.
• 1 ≤ mk ≤ 1 000 000 000 for all k.
Furthermore:
• For Subtask 1 (15 marks), sj = 10, for all j, and mk = 10, for all k.
Hint: This subtask says the skill level limit for student jobs, and the minimum skill level
for master jobs, is always 10. This means the only thing that matters about each monk, is
whether their skill is less than 10, more than 10, or equal to 10.
• For Subtask 2 (15 marks), sj = 200, for all j, and mk = 100, for all k.
Hint: The solution to this subtask is quite similar to Subtask 1.

• For Subtask 3 (30 marks), S = 0, N ≤ 1000 and M ≤ 1000. In particular, S = 0 means that
there are no student jobs; there are only master jobs.
Hint: To start with, ask yourself which monk should be assigned to the master job requiring
the most skill.
• For Subtask 4 (20 marks), S = 0. There are no student jobs; there are only master jobs.
Hint: The input is quite large in this subtask, so you will need a fast solution. Try to avoid
nested loops. Maybe sorting will help?

• For Subtask 5 (20 marks), no further constraints apply.


There are no hints available for this subtask.
2019 Australian Informatics Olympiad (Intermediate) — Medusa's Snakes13

2019 Australian Informatics Olympiad (Intermediate) — Medusa’s Snakes 14

Problem 4
Medusa’s Snakes

Input File: snakein.txt


Output File: snakeout.txt

Time and Memory Limits: 1 second, 1 GB

After the success of your latest research project in mythical DNA, you have gained the attention
of a most diabolical creature: Medusa.
Medusa has snakes instead of hair. Each of her snakes’ DNA is represented by an uppercase
string of letters. Each letter is one of S, N, A, K or E.
Your extensive research shows that a snake’s venom level depends on its DNA. A snake has
venom level x if its DNA:

• has exactly 5x letters

• begins with x copies of the letter S


• then has x copies of the letter N
• then has x copies of the letter A

• then has x copies of the letter K

• ends with x copies of the letter E.


For example, a snake with venom level 1 has DNA SNAKE, while a snake that has venom level
3 has DNA SSSNNNAAAKKKEEE.
If a snake’s DNA does not fit the format described above, it has a venom level of 0.
Medusa would like your help making her snakes venomous, by deleting zero or more letters
from their DNA.
Given a snake’s DNA, can you work out the maximum venom level this snake could have?

Input
The first line contains the integer N : the number of letters in the snake’s DNA. The second line
contains a string of N uppercase letters, representing the snake’s DNA. Each letter is one of S, N,
A, K or E.

Output
Your program should output a single integer: the maximum venom level the snake could have,
after you delete some (possibly none) of the letters from its DNA.
2019 Australian Informatics Olympiad (Intermediate) — Medusa's Snakes14

2019 Australian Informatics Olympiad (Intermediate) — Medusa’s Snakes 15

Sample Input 1 Sample Output 1


17 2
KSEESNANNAAKNKESE

Sample Input 2 Sample Output 2


22 3
SSSSNNNAAAAKKKKEEEEEEE

Sample Input 3 Sample Output 3


15 1
SNAKESNAKESNAKE

Sample Input 4 Sample Output 4


6 0
KANSAS

Explanation
The letters that are deleted in each case are underlined below:
• Sample Input 1: KSEESNANNAAKNKESE → SSNNAAKKEE
• Sample Input 2: SSSSNNNAAAAKKKKEEEEEEE→ SSSNNNAAAKKKEEE
• Sample Input 3: SNAKESNAKESNAKE → SNAKE
• Sample Input 4: No matter which letters you delete, the snake will always have venom level
0, so the answer is 0.

Subtasks & Constraints


For all cases, 5 ≤ N ≤ 100 000. Additionally:

• For Subtask 1 (15 marks), all S come before all N, which come before all A, which come before
all K, which come before all E. There will be at least one of each letter. Sample Input 2 is an
example of a case that could be in this subtask.

• For Subtask 2 (15 marks), the DNA sequence consists of SNAKE repeated some number of
times. Sample Input 3 is an example of a case that could be in this subtask.

• For Subtask 3 (30 marks), N ≤ 10.

• For Subtask 4 (20 marks), N ≤ 1000.


• For Subtask 5 (20 marks), no further constraints apply.

There are no hints available for this problem!


2019 Australian Informatics Olympiad (Intermediate) — Evading Capture15

2019 Australian Informatics Olympiad (Intermediate) — Evading Capture 16

Problem 5
Evading Capture

Input File: evadingin.txt


Output File: evadingout.txt

Time and Memory Limits: 1 second, 1 GB

It is the year 2077, when androids live among us, cars no longer have wheels and humans are born
on blockchain. You are preparing to pull off the greatest heist of all time: stealing the Bitcoin.
Stealing the Bitcoin is the easy part; evading capture is much harder.
The world consists of N cities, numbered from 1 to N . E pairs of these cities are adjacent to
each other. In a single hop, you can travel from the city you are currently in to any adjacent city.
Your heist will begin in city X, where the Bitcoin is located. After stealing the Bitcoin, you
will need to make exactly K hops to be safe from capture (too few and the Chaser will catch you,
too many and the All-Seeing I will know where you are).
You aren’t yet sure which hops you will make on the day of the heist, so you have decided to
work out the number of different cities you could finish in after making exactly K hops. You whip
out your trusty laptop and begin coding.

Input
The first line contains the integers N , E, X and K, which are the number of cities, the number of
adjacent pairs of cities, the starting city and the number of hops you must make, respectively.
Then E lines follow, describing the adjacent pairs of cities. The ith of these lines contains ai
and bi , indicating that city ai is adjacent to city bi .

Output
Your program should output a single integer, the number of different cities you could finish in after
making exactly K hops. You are guaranteed that at least one such sequence of hops exists.

Sample Input 1
6 5 4 2
3 1
3 2
1 2
4 2
3 6

Sample Output 1
3
2019 Australian Informatics Olympiad (Intermediate) — Evading Capture16

2019 Australian Informatics Olympiad (Intermediate) — Evading Capture 17

Sample Input 2 Sample Input 3


25 40 13 29 36 40 26 5
1 2 1 2
1 6 1 7
2 3 2 3
2 7 2 8
3 4 3 9
3 8 4 5
4 5 4 10
4 9 5 11
5 10 6 12
6 7 7 8
6 11 7 13
7 8 8 14
7 12 9 10
8 9 9 15
8 13 12 18
9 10 14 15
9 14 14 20
10 15 15 16
11 12 16 17
11 16 17 18
12 13 17 23
12 17 19 20
13 14 19 25
13 18 20 21
14 15 20 26
14 19 21 22
15 20 21 27
16 17 22 28
16 21 23 24
17 18 24 30
17 22 25 31
18 19 26 27
18 23 27 28
19 20 27 33
19 24 28 34
20 25 29 30
21 22 29 35
22 23 33 34
23 24 34 35
24 25 35 36

Sample Output 2 Sample Output 3


12 14
2019 Australian Informatics Olympiad (Intermediate) — Evading Capture17

2019 Australian Informatics Olympiad (Intermediate) — Evading Capture 18

Explanation
In the first sample input, you start at city X = 4 and must make K = 2 hops. There are 3 different
cities you could finish in, described below:

• 4→2→1

• 4→2→3

• 4→2→4

2 3

4 5 6

Cities & possible hops in Sample Input 1

In the second sample input, you start at city X = 13 and must make K = 29 hops. The number
of different cities you could finish in is 12.
In the third sample input, you start at city X = 26 and must make K = 5 hops. The number
of different cities you could finish in is 14.

1 2 3 4 5 1 2 3 4 5 6

7 8 9 10 11 12
6 7 8 9 10

13 14 15 16 17 18
11 12 13 14 15
19 20 21 22 23 24

16 17 18 19 20
25 26 27 28 29 30

21 22 23 24 25 31 32 33 34 35 36

Cities & possible hops in Sample Input 2 Cities & possible hops in Sample Input 3
2019 Australian Informatics Olympiad (Intermediate) — Evading Capture18

2019 Australian Informatics Olympiad (Intermediate) — Evading Capture 19

Subtasks & Constraints


For all cases:

• 1 ≤ N ≤ 100 000.

• 1 ≤ E ≤ 100 000.
• 1 ≤ X ≤ N.

• 1 ≤ K ≤ 1 000 000 000.

Furthermore:
• For Subtask 1 (10 marks), K = 1. That is, you must make exactly 1 hop to be safe from
capture.

• For Subtask 2 (15 marks), the cities, adjacent pairs of cities, and your starting city are the
same as Sample Input 2, but K may be different.

• For Subtask 3 (15 marks), the cities and the adjacent pairs of cities are the same as Sample
Input 2, but K and X may be different.

• For Subtask 4 (35 marks), the cities and the adjacent pairs of cities are the same as Sample
Input 3, but K and X may be different.

• For Subtask 5 (15 marks), N ≤ 50.

• For Subtask 6 (10 marks), no further constraints apply.


There are no hints available for this problem!
2019 Australian Informatics Olympiad (Intermediate) — Lollipops, Sweets and Chocolates II19

2019 Australian Informatics Olympiad (Intermediate) — Lollipops, Sweets and Chocolates II 20

Problem 6
Lollipops, Sweets and Chocolates II

Input File: lscin.txt


Output File: lscout.txt

Time and Memory Limits: 1 second, 1 GB

The grand opening of Lollipops, Sweets and Chocolates (LSC) was a great success! Since
then a total of N LSC stores have opened along Australian Ice-cream and Oreo Crescent (AIOC),
the most candilicious street in Sydney.
The street is divided into L blocks, spaced 1 metre apart. The blocks are labelled with numbers
from 1 to L along the street, so that the distance between any two blocks a and b is |a − b| metres.
Each of the N stores occupies a different block. There are also M houses along the street, each
occupying a different block. However, houses and stores may occupy the same block!

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

LSC LSC LSC LSC

To keep the little boys and girls of Sydney excited about the stores, LSC has been distributing
personalised pamphlets to the houses on the street. There is one pamphlet for each house. The
ith pamphlet contains a message of the form:
There are si stores within walking distance of your house!

Wondering how far you have to walk to get your hands on another mouthwatering chlorosulfonic
acid sorbet, you decide to investigate how far ‘walking distance’ could be. Walking distance means
a distance of at most R metres, and you would like to determine a possible value for R. You know
that the street is very long, so R cannot be greater than L.
You have collected all M pamphlets, but in your haste you have forgotten which house each
pamphlet came from! Given all the numbers on the pamphlets, your task is to determine a possible
value of R. LSC is notorious for distributing inaccurate pamphlets, so it is also possible that no
such value of R exists.

Input
• The first line of input will contain three integers N , M and L: the number of stores, the
number of houses, and the length of the street, respectively.
• The next line of input will contain N integers, describing the positions of the shops. These
will be given in increasing order.
• The next line of input will contain M integers, describing the positions of the houses. These
will be given in increasing order.
• The final line of input will contain M integers, describing the numbers on the pamphlets.
These will be given in non-decreasing order. Note that this may not necessarily be the same
order as the houses.
2019 Australian Informatics Olympiad (Intermediate) — Lollipops, Sweets and Chocolates II20

2019 Australian Informatics Olympiad (Intermediate) — Lollipops, Sweets and Chocolates II 21

Output
Your program must output a single integer (whole number): a possible value of R, or −1 if no such
value exists. If there are multiple possible answers, output any one of them. Note that the value
of R must not be greater than L.

Sample Input 1 Sample Input 2 Sample Input 3


4 5 15 3 2 6 4 3 14
1 5 8 9 1 3 5 1 6 10 14
3 6 8 10 14 2 6 3 9 13
0 2 2 3 3 1 3 1 1 1

Sample Output 1 Sample Output 2 Sample Output 3


3 -1 2

Explanation
In the first sample input, it is possible that the pamphlets came from the houses in this order:

2 3 3 2 0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

LSC LSC LSC LSC

If R = 3, then:
• The house at block 3 is within walking distance of 2 stores: the ones at blocks 1 and 5.

• The house at block 6 is within walking distance of 3 stores: the ones at blocks 5, 8 and 9.

• The house at block 8 is within walking distance of 3 stores: the ones at blocks 5, 8 and 9.

• The house at block 10 is within walking distance of 2 stores: the ones at blocks 8 and 9.
• The house at block 14 is within walking distance of no stores.

Note that 4 would also have been a correct output to this sample case.

In the second sample input, no matter which house each pamphlet came from, it is impossible
to find a value of R that makes them correct.

1 2 3 4 5 6

LSC LSC LSC


2019 Australian Informatics Olympiad (Intermediate) — Lollipops, Sweets and Chocolates II21

2019 Australian Informatics Olympiad (Intermediate) — Lollipops, Sweets and Chocolates II 22

In the third sample input, each house is within walking distance of exactly one store. The only
value of R that satisfies this is 2.
1 1 1

1 2 3 4 5 6 7 8 9 10 11 12 13 14

LSC LSC LSC LSC

Subtasks & Constraints


For all cases:

• 1 ≤ N, M ≤ 100 000.

• 1 ≤ L ≤ 1 000 000 000.


• 0 ≤ si ≤ N for all i.

• All shops are located at distinct blocks between 1 and L, inclusive.


• All houses are located at distinct blocks between 1 and L, inclusive.
• A shop and a house may be located at the same block.

Furthermore:

• For Subtask 1 (15 marks), si = 1 for all i. This means that each house is within walking of
exactly one store. Sample Input 3 is an example of a case that could be in this subtask.
• For Subtask 2 (15 marks), N, M ≤ 30.

• For Subtask 3 (20 marks), N, M ≤ 300.


• For Subtask 4 (20 marks), N, M ≤ 1000.

• For Subtask 5 (30 marks), no further constraints apply.

There are no hints available for this problem!

You might also like