#K90772. Bookshelf Progressions
Bookshelf Progressions
Bookshelf Progressions
You are given a list of initial book heights and then a sequence of book arrivals. Your task is to determine if the current collection of book heights can be arranged to form a shelf that is either in an arithmetic progression or in a geometric progression.
An arithmetic progression is defined as a sequence of numbers that can be rearranged to form
(a,; a+d,; a+2d,; \dots)
where the difference (d) is constant.
A geometric progression is defined as a sequence of numbers that can be rearranged to form
(a,; ar,; ar^2,; \dots)
where the ratio (r) is constant (with the restriction that if (a=0) or any intermediate element is 0, the progression does not hold).
For each new book arrival, add the book to the collection and check if the entire set of books can form one of these valid progressions. If so, output "YES", otherwise "NO". Note that if there are 0 or 1 books, the shelf is always considered valid.
inputFormat
Input is read from standard input (stdin) and consists of four parts:
- An integer (n) — the number of initial books.
- A line with (n) integers representing the heights of the initial books.
- An integer (q) — the number of arriving books.
- A line with (q) integers representing the heights of the arriving books.
outputFormat
For each arriving book, output on a new line either "YES" if the updated shelf (i.e. the collection of books so far) can be arranged in either an arithmetic progression or a geometric progression, or "NO" otherwise. The output is written to standard output (stdout).## sample
4
3 1 2 4
3
5 6 7
YES
YES
YES
</p>