Apio 2008
Apio 2008
Apio 2008
Duration: 5 hours
3 problems
All problems should be attempted.
Asia-Pacific Informatics Olympiad 2008
BEADS
Beads
Professor X has recently revealed his latest invention to the world: the Ultimate Bead Swapper (UBS).
As the name implies, it can make a sequence of beads much more interesting by swapping some beads
in it!
The UBS has N conveyor belts placed in north-south direction in parallel. The conveyor belts are
numbered 1 to N from left to right. Every belt moves from north to south at the same speed. There
are M swappers placed between adjacent conveyors. No two swappers are equally far from the north
end of the UBS. (In other words, they can be totally ordered according to how far they are from the
north end.) The swappers are numbered 1 to M from north to south. Figure 1 shows the UBS when
viewed from above.
Belt 1 Belt 2 Belt 3 Belt 4 Belt 5
Swapper 1
Swapper 2
Swapper 3
Swapper 4
Swapper 5
To use the UBS, N beads are placed at the north end of the conveyor belts at the same time so that
they form a horizontal row as they move along the belt. When two beads come under a swapper, the
bead on the right conveyor belt is moved to the left conveyor belt, and the bead on the left conveyor
belt is moved to the right conveyor. After being swapped, the two beads do not break the horizontal
row. Figure 2 illustrates the behavior of a swapper.
Task
Write a program that, given the number of conveyor belts N , the number of swappers M , and the
positions of each swapper, answer questions of the form:
Given K and J, for the bead that is placed on Belt K at the north end of the UBS, which
belt is the bead on after all beads just move past Swapper J?
BEADS
1 2 3 4
Swapper
1 3 2 4
(a) (b)
Figure 2: (a) Four beads move along the conveyor belts. (b) Bead 2 and 3 trade places after going
under the swapper.
Input
Your program should read from standard input. The first line contains the number of conveyor belts
N (1 ≤ N ≤ 300, 000) and the number of swappers M (1 ≤ M ≤ 300, 000).
Swappers are listed from north to south in the following M lines. Each line contains one integer
P (1 ≤ P ≤ M − 1), meaning that there is a swapper over conveyor belt P and P + 1.
Interaction
After reading the input above, your program should call functions from the following library specified
in Table 1. The functions must be called in the following order:
1. It calls the function getNumQuestions to retrieve Q(1 ≤ Q ≤ 300, 000), the number of questions
it will be asked.
We emphasize that getNumQuestions must be called first and only once. getQuestion and answer
must be called alternately: after calling one getQuestion, your program may not call getQuestion
until it calls answer, and vice versa. If your program violates this convention when running a test
scenario, you will receive a 0% score for that test scenario.
Programming Instructions
If you submit a Pascal source code, the source code must contain the statement:
uses beadslib;
If you submit a C or C++ source code, the source code must contain the line:
#include "beadslib.h"
BEADS
Pascal
procedure getQuestion(var K:longint; var J:longint) K is set to the number of the conveyor belt
C that the bead is placed at the north end of
void getQuestion(int *K, int *J) the UBS.
Pascal
procedure answer(x:longint) Report that the answer to the question cor-
C and C++ responding to the last getQuestion is x.
void answer(int x)
BEADS
Sample Interaction
Function Call Return Value(s) and Explanation
2
getNumQuestions();
The program will be asked two questions.
Pascal
getQuestion(K, J);
K=3, J=4
C
For the bead that is placed on Belt 3 at the north end of the UBS, which
getQuestion(&K, &J);
belt is the bead on after all beads just move past Swapper 4?
C++
getQuestion(K, J);
answer(1); After every bead passes Swapper 4, the bead in question is on Belt 1.
Pascal
getQuestion(K, J);
K=5, J=5
C
For the bead that is placed on Belt 5 at the north end of the UBS, which
getQuestion(&K, &J);
belt is the bead on after all beads just move past Swapper 5?
C++
getQuestion(K, J);
answer(4); After every bead passes Swapper 5, the bead in question is on Belt 4.
Scoring
The score for each input scenario will be 100% if your program follows the function calling convention
above and answers every question correctly, and 0% otherwise.
In test scenarios worthing 20 points, M , and Q will be at most 10, 000.
ROADS
Roads
The Kingdom of New Asia contains N villages connected by M roads. Some roads are made of
cobblestones, and others are made of concrete. Keeping roads free-of-charge needs a lot of money, and
it seems impossible for the Kingdom to maintain every road. A new road maintaining plan is needed.
The King has decided that the Kingdom will keep as few free roads as possible, but every two
distinct villages must be connected by one and only one path of free roads. Also, although concrete
roads are more suitable for modern traffic, the King thinks walking on cobblestones is interesting. As
a result, he decides that exactly K cobblestone roads will be kept free.
For example, suppose the villages and the roads in New Asia are as in Figure 1a. If the King wants
to keep two cobblestone roads free, then the Kingdom can keep roads (1,2), (2,3), (3,4), and (3,5) free
as in Figure 1b. This plan satisfies the King’s criteria because (1) every two villages are connected
via one and only one path of free roads, (2) there are as few free roads as possible, and (3) there are
exactly two cobblestone roads: (2,3) and (3,4).
1 1
2 2
3 4 3 4
5 5
(a) (b)
Figure 1: (a) An example configuration of villages and roads in the Kingdom of New Asia. Solid lines
denote concrete roads, and dashed lines denote cobblestone roads. (b) A road maintaining plan that
keeps two cobblestone roads free. Only free roads are shown.
Task
Given a description of roads in New Asia and the number of cobblestone roads that the King wants
to keep free, write a program to determine if there is a road maintaining plan that satisfies the King’s
criteria, and output a valid plan if there is one.
Input
The first line contains three integers separated by one space:
• K, the number of cobblestone roads the King wants to keep free (0 ≤ K ≤ N − 1).
ROADS
The following M lines describes the roads in New Asia, which are numbered 1 to M . The (i + 1)st
line describes Road i. It contains three intergers separated by one space:
• ui and vi , the two villages connected by Road i. Villages are numbered 1 to N , and
Output
If there is no road maintaining plan that satisfies the King’s criteria, your program should print no
solution on the first line of the output.
Otherwise, your program should output a valid road maintaining plan by listing roads that will
be kept free, one road per line. To list a road, print the line in the input that describes it. The roads
can be listed in any order. If there are more than one valid plan, you can output any such plan.
Scoring
The score for each input scenario will be 100% if the correct answer is outputed and 0% otherwise.
In test scenarios worthing 20 points, K will be at most 10.
DNA
DNA
One interesting use of computer is to analyze biological data such as DNA sequences. Biologically,
a strand of DNA is a chain of nucleotides Adenine, Cytosine, Guanine, and Thymine. The four
nucleotides are represented by characters A, C, G, and T, respectively. Thus, a strand of DNA can be
represented by a string of these four characters. We call such a string a DNA sequence.
It is possible that the biologists cannot determine some nucleotides in a DNA strand. In such
a case, the character N is used to represent an unknown nucleotides in the DNA sequence of the
strand. In other words, N is a wildcard character for any one character among A, C, G or T. We
call a DNA sequence with one or more character N an incomplete sequence; otherwise, it is called a
complete sequence. A complete sequence is said to agree with an incomplete sequence if it is a result of
substituting each N in the incomplete sequence with one of the four nucleotides. For example, ACCCT
agrees with ACNNT, but AGGAT does not.
Researchers often order the four nucleotides the way we order the English alphabets: A comes before
C, C comes before G, G comes before T. A DNA sequence is classified as form-1 if every nucleotide in
it is the same as or comes before the nucleotides immediately to its right. For example, AACCGT is
form-1, but AACGTC is not.
In general, a sequence is form-j, for j > 1, if it is a form-(j − 1) or it is a concatenation of a
form-(j − 1) sequence and a form-1 sequence. For example, AACCC, ACACC, and ACACA are form-3, but
GCACAC and ACACACA are not.
Again, researchers order DNA sequences lexicographically the way we order words in a dictionary.
As such, the first form-3 sequence of length 5 is AAAAA, and the last is TTTTT. As another example,
consider the incomplete sequence ACANNCNNG. The first seven form-3 sequences that agree with it are:
ACAAACAAG
ACAAACACG
ACAAACAGG
ACAAACCAG
ACAAACCCG
ACAAACCGG
ACAAACCTG
Task
Write a program to find the Rth form-K sequence that agrees with the given incomplete sequence of
length M .
Input
The first line contains three integers separated by one space: M (1 ≤ M ≤ 50, 000), K(1 ≤ K ≤ 10),
and R (1 ≤ R ≤ 2 × 1012 ). The second line contains a string of length M , which is the incomplete
sequence. It is guaranteed that the number of form-K sequences that agrees with the incomplete
sequence is not greater than 4 × 1018 , so it can be represented by a long long in C and C++ or an
Int64 in Pascal. Moreover, R does not exceed the number of form-K sequences that agree with the
given incomplete sequence.
DNA
Output
On the first line, print the Rth form-K sequence that agrees with the incomplete sequence in the
input.
Programming Remark
In C and C++, you should use long long data type. The following piece of code show how to read
and write a long long from and to standard input/output:
long long a;
scanf("%lld",&a);
printf("%lld\n",a);
In Pascal, you should use Int64. No special instructions are needed to manipulate data of this
type.
Scoring
The score for each input scenario will be 100% if the correct answer is outputed and 0% otherwise.
In test scenarios worthing 20 points, M will be at most 10.