Saint Petersburg State University
2012-2013 Group B Problemset 12 Binary and Ternary Search
Problem A. Avid butterfly collector
Input filename: collect.in
Output filename: collect.out
Time limit: 4 seconds
Memory limit: 256 Mb
It is well-known that Andrey Sergeevich is an avid butterfly collector. He has an enormous
collection, with exhibits from all over the world. Let’s assume that there are 2 000 000 000
species of butterflies in the world.
Andrey Sergeevich assigned a unique index to every species, starting from 1.
He now would like to find out whether a butterfly of a specie K is already in the collection,
or he will need to add it, spending lots of time and money.
Input file format
The first line contains a single integer 1 ≤ N ≤ 100 000, the number of species in the
collection of Andrey Sergeevich.
The second line contains N space-separated integers, sorted in increasing order, the species’
indices in the collection.
The third line contains a single integer 1 ≤ M ≤ 100 000, the number of queries.
The last line contains M space-separated integers, the indices of species to query.
Output file format
Print M lines. Answer "YES" to the query if the butterfly with a given index is already in
the collection, and "NO" otherwise.
Sample tests
collect.in collect.out
7 NO
10 47 50 63 89 90 99 NO
4 YES
84 33 10 82 NO
Page 1 of 5
Saint Petersburg State University
2012-2013 Group B Problemset 12 Binary and Ternary Search
Problem B. Cows into Stalls
Input filename: cows.in
Output filename: cows.out
Time limit: 2 seconds
Memory limit: 256 Mb
There are several stalls on the real line. Your task is to place cows into these stalls, maxi-
mizing the minimal distance between cows.
Input file format
The first line contains number of stalls 3 ≤ N ≤ 10 000 and number of cows 2 ≤ K < N . The
second line contains N positive integers in the increasing order, denoting the stalls’ coordinates,
which are at most 109 .
Output file format
Print the maximal possible distance between cows.
Sample tests
cows.in cows.out
5 3 99
1 2 3 100 1000
Page 2 of 5
Saint Petersburg State University
2012-2013 Group B Problemset 12 Binary and Ternary Search
Problem C. Firewood Meadow
Input filename: forest.in
Output filename: forest.out
Time limit: 2 seconds
Memory limit: 256 Mb
Little boy Fermat lives in a village. The winter is coming, and his grandma asked him to
go to the forest to collect some firewood. There is a magical Firewood Meadow in the woods
nearby the village. It has a never-ending firewood. Naturally, this is the Fermat’s destination.
The only problem is that the path to the Meadow is not particularly short. Specifically,
Fermat walks slower in the forest than in the fields surrounding the village.
• Village has coordinates (0, 1).
• Meadow has coordinates (1, 0).
• Fields-forest border is a horizontal line y = a, where 0 ≤ a ≤ 1 is a given real number.
• Fermat’s walking speed is Vp in the fields, and Vf in the forest. He may walk in either
biome when walking along the border.
Find the optimal forest entrance point, which minimizes the total walking time.
Input file format
The first line contains two positive integers 1 ≤ Vp , Vf ≤ 105 . The second line consists of a
single real number 0 ≤ a ≤ 1, the y-coordinate of the fields-forest boundary.
Output file format
Print a single line containing the x-coordinate of the optimal forest entrance point, with the
precision of at least 8 decimal digits.
Sample tests
forest.in forest.out
5 3 0.783310604
0.4
5 5 0.500000000
0.5
Page 3 of 5
Saint Petersburg State University
2012-2013 Group B Problemset 12 Binary and Ternary Search
Problem D. Root of a Cubic
Input filename: cubroot.in
Output filename: cubroot.out
Time limit: 1 seconds
Memory limit: 256 Mb
You are given a cubic equation ax3 + bx2 + cx + d = 0 (a 6= 0). It is guaranteed that it has
exactly one root. Find it!
Input file format
On the first and only line, four space-separated integers are given: −1000 ≤ a, b, c, d ≤ 1000.
Output file format
On the first and only line print the root, with the precision of at least 6 decimal digits after
the decimal point.
Sample tests
cubroot.in cubroot.out
1 -3 3 -1 1
-1 -6 -12 -7 -1
Page 4 of 5
Saint Petersburg State University
2012-2013 Group B Problemset 12 Binary and Ternary Search
Problem E. Trains
Input filename: trains.in
Output filename: trains.out
Time limit: 2 seconds
Memory limit: 256 Mb
Following the increased number of railroad accidents on the Kostroma–Sudislavl’ railway,
the railroad administration decided to change the train movement schedule. The meticulous
analysis revealed, that the optimal schedule is the following: initially the train moves for T1
minutes with the velocity of V1 meters per minute, then T2 minutes with velocity V2 , . . . , and
finally for TN minutes with velocity VN . During some intervals, the train may stay still (velocity
will be 0).
However, according to the current safety regulations, the distance between two consecutive
trains may not be shorter than L meters. Find the minimum possible time interval between the
departure of two consecutive trains moving according to the optimal schedule and complying
with safety regulations.
Input file format
The first two lines contain two positive integers: the minimum distance 100 ≤ L ≤ 10 000
and the number of schedule segments 1 ≤ N ≤ 1000. The following N lines contain pairs
1 ≤ Ti ≤ 1000, 0 ≤ Vi ≤ 1000, describing the optimal schedule.
Output file format
On the first and only line print the requested minimum time interval, with the precision of
at least 3 decimal digits after the decimal point.
Sample tests
trains.in trains.out
1000 27.500
4
10 0
30 80
15 0
20 100
Page 5 of 5