Download Programming Contest Practice Problems

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Algorithm wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Elementary mathematics wikipedia , lookup

Addition wikipedia , lookup

Transcript
Programming Contest Problems
1. Design and implement an algorithm that fills a table n x m in the following way:
A[0][0] to a[0][m-1] = 1
A[n-1][0] to a[n-1][m-1] = 1
A[0][0] to a[n-1][0] = 1
A[0][m-1] to a[n-1][m-1] = 1
A[1][1] to a[1][m-2] = 2 (if the row exists and is not filled with 1)
A[n-2][1] to a[n-2][m-2] = 2 (if the row exists and is not filled with 1)
A[1][1] to a[n-2][1] = 2 (if the column exists and is not filled with 1)
A[1][m-2] to a[n-2][m-2] = 2 (if the column exists and is not filled with 1)
Etc
Example:
5x7
1
1
1
1
1
1
2
2
2
1
1
2
3
2
1
1
2
3
2
1
6 x7
1
2
3
2
1
1
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
1
1
2
3
3
2
1
7x7
1
2
3
3
2
1
1
2
3
3
2
1
1
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
1
1
2
3
3
3
2
1
4x3
1
2
3
4
3
2
1
1
2
3
3
3
2
1
1
2
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
1
1
1
1
1
2. Door in a wall. You are facing a wall that stretches infinitely in both directions. There
is a door in the wall, but you know neither how far away nor in which direction. You
can see the door only when you are right next to it. Design an algorithm that enables
you to reach the door by walking at most O(n) steps where n is the (unknown to you)
number of steps between your initial position and the door. Implement the algorithm
3. Let A[1::n] be a strictly sorted vector of integers. Find any k such that A[k] = k in
O(log n) time. For example:
4. Let A[1::n][1::n] be a two-dimensional array of real numbers that is sorted both rowwise (A[x][y] < A[x][y + 1] for all x and y) and column-wise (A[x][y] < A[x + 1][y]
1
for all x and y). How can you find a given number k in this array in O(n log n) time?
How can you do it in O(n) time? For example (k = 5):
5. Given a vector A[1::n] of integers find in O(n) time two indices i and j such that A[i]
_ A[j] is the greatest possible. Then, solve a more difficult version of this problem, in
which i > j. For example, this is the former version
and this is the latter one
5
7
9
4
2
j
6
3
8
i
3
1
4
6. Let A[1::n] be a vector of real numbers. Explain how to implement a function Sum(i;
j) that computes the sum A[i] + A[i + 1] + ….. + A[j] in constant time. Assume that
you can do some preprocessing of A, i.e., before the first invocation of Sum, you can
create an auxiliary vector B and use it inside Sum.
7. Let A[1::n] be a vector of integers (some of them may be negative). Find i and j such
that the value of A[i] + A[i + 1] + …. + A[j] is the greatest possible. Can you do it in
O(n3) time? In O(n2) time? In O(n) time?
8. Let A be an array of N integers not necessarily sorted. An element x of A is said to be
a majority element if the number of elements of A that are equal to X is at least
(n+1)/2. Design and implement an algorithm that determines whether A has a
2
majority element or not, and outputs such an element when A does have a majority
element. The worst-case running time of your algorithm must be O(n).
9. You are given a vector A[1::n] of natural numbers. Design an O(n) algorithm that
would compute another vector B[1::n] such that for every i > 1, B[i] is the biggest
number smaller than i for which A[B[i]] > A[i]. We assume that A[1] is the biggest
element in A so B always exists. For the following vector A
the correct vector B is
10. Let A[1::n] be a vector of natural numbers such that 1  A[i]  m for every i. Find the
length k of the longest sequence of indices i1 < i2 < … < ik such that A[i1] > A[i2] >
… > A[ik]. We call it a descending sequence.
An example is shown below.
11. A department has n research students. Consider one day, in which the i-th student
starts working at time A[i][1] and stops working at time A[i][2]. Determine the
greatest number of students that are working simultaneously.
In the example below, the answer is 7.
3
12. Let A[1::n][1::n] be a two-dimensional array of 0's and 1's. Find the greatest
rectangle, which contains only 0's. In other words, find x1, y1, x2, y2, with A[x][y] = 0 for
all x1  x < x2 and y1  y < y2, such that (x2 - x1)(y2 - y1) is maximum. Can you do it in
O(n6) time? In O(n4) time? In O(n3) time? In O(n2) time? For example:
13. Let c1, c2, . . . , ck be natural numbers such that 1  ci  n for each i that represents
denominations of coins (in pence). You have an infinite number of coins of each of
the denominations. For each k : 0  k  n find the smallest number of coins A[k]
4
using which you can pay exactly k pence. For example, when c1 = 1, c2 = 4, c3 = 6,
c4 = 9 you the answer is
Note that even if we are interested only in A[15] we have to compute the whole vector.
Augment the algorithm such that you should be able to find out which coins you have to use
to obtain a particular sum (e.g., to find out that 15 = c3 + c4).
14. Let modify the previous question by assuming that we have only one coin of each
denomination. Show how to compute an array A[0::k][0::n] such that paying j pounds
using only coins c1, . . . , ci requires using A[i][j] coins. For the same example as in
the previous exercise we get:
Again note that if we are interested only in calculating the value of A[4][15] (i.e., the
smallest numbers of coins necessary to obtain 15, assuming that we can use all 4 coins), we
have to compute the whole array. However, an optimization is possible, because we do not
need to have access to each row of the array at the same time. Show how to compute the last
row using only two vectors: B[0::n] and C[0::n]. Then, show how to do the same with only
one such a vector available.
15. Design an algorithm to find all natural numbers that can be expressed as the sum of
two cubes in two essentially different ways. In other words, find all k's for which
there exist four different natural numbers a, b, c, d, all smaller than n, such that k = a3
+ b3 = c3 + d3. For example:
13 + 123 = 1729 = 93 + 103:
Can you do this in O(n4)? In O(n3)? In O(n2 log n)?
5
16. You have a machine that can cut a rectangle into two smaller rectangles along the line
that is parallel to one of the sides. There is a further restriction: the lengths of all sides
of all the rectangles involved (both the original one and the two smaller ones) are
natural numbers. You can use this machine as many times as you want. Given two
natural numbers: n and m, determine the smallest number of cuts necessary to cut a
single rectangle n _m into squares. For example, if n = 6 and m = 5, you need 4 cuts:
17. A combination lock with master combination consists of n dials, each of which can be
set in one of k positions. Each lock can be opened in (at least) two ways: setting each
dial to the correct setting of the individual combination (different for each lock) or
setting each dial to the correct setting of the master combination (the same for each
lock). For example, suppose there are 3 dials, each with 10 positions (0 through 9),
five locks with the following individual combinations:
lock A: 9,5,8
lock B: 6,5,7
lock C: 5,9,3
lock D: 4,8,6
lock E: 3,9,2
and all also open with the master combination 8,4,5. Now suppose there is a “flaw” in the lock
design, so that a given lock will open if each individual dial is set either to the individual
combination setting or to the master combination setting e.g., lock D will open on setting 4,4,5.
Describe how, given one lock and only the individual combination to that lock, one can rapidly
determine (in O(nk) time rather than O(nk/2)time) the master combination, and hence the way to
open any lock.
18. There are n bacteria and 1 virus in a Petri dish. Within the first minute, the virus kills
one bacterium and produces another copy of itself, and all of the remaining bacteria
reproduce, making 2 viruses and 2*(n-1) bacteria.
In the second minute, each of the viruses kills a bacterium and produces a new copy of
itself (resulting in 4 viruses and 2*(2*(n-1) -2) = 4*n – 8 bacteria; again, the remaining
bacteria reproduces. This process continues every minute. Will the viruses eventually kill
all the bacteria?
6
19. Given a list of positive integers, and another integer representing a goal, find a subset
of the list of integers with a sum as close as possible to the goal.
20. You are given 8 x 8 table of natural numbers. In any step you can either double each
of the numbers in any one row, or subtract 1 from each of the numbers in any one
column. Devise an algorithm that transforms the original table into a table of all
zeros. What is the running time of your algorithm?
21. A very large department has a mix of 100 professors: some are honest and hardworking, while others are deceitful and do not like students. The honest professors
always tell the truth, but the deceitful ones sometimes tell the truth and sometimes lie.
You can ask any professor the following question about any other professor:
“Professor Y, is professor X honest?” Professor Y will answer either with “yes” or
“no”. Design an algorithm that, with no more than 198 questions, would allow you to
figure out which of the 100 professors are honest. It is known that there are more
honest than dishonest professors
7