Yanee is a bird enthusiast. Since reading about IP over Avian Carriers (IPoAC), she has spent much of her time training a flock of intelligent parrots to carry messages over long distances.
Yanee's dream is to use her birds to send a message to a land far far away. Her message is a sequence of (not necessarily distinct) integers, each between and , inclusive. Yanee keeps specially-trained parrots. All the parrots look the same; Yanee cannot tell them apart. Each bird can remember a single integer between and , inclusive.
Early on, she tried a simple scheme: to send a message, Yanee carefully let the birds out of the cage one by one. Before each bird soared into the air, she taught it a number from the message sequence in order. Unfortunately, this scheme did not work. Eventually, all the birds did arrive at the destination, but they did not necessarily arrive in the order in which they left. With this scheme, Yanee could recover all the numbers she sent, but she was unable to put them into the right order.
To realize her dream, Yanee will need a better scheme, and for that she needs your help. Given a message , she plans to let the birds out one by one like before. She needs you to write a program that will perform two separate operations:
- First, your program should be able to read a message and transform it into a sequence of at most integers between and that she will teach the birds.
- Second, your program should be able to read the list of integers between and received as the birds reach their destination, and then transform it back to the original message .
You may assume that all parrots always arrive at the destination, and that each of them remembers the number it was assigned. Yanee reminds you once again that the parrots may arrive in any order. Note that Yanee only has parrots, so the sequence of integers between and that you produce must contain at most integers.
Your task
Write two separate procedures. One of them will be used by the sender (encoder) and the other by the receiver (decoder).
The overall process is shown in the following figure.
The two procedures you are to write are:
Procedure
encode(N,M)
that takes the following parameters:- – the length of the message.
- – a one-dimensional array of integers representing the message. You may assume that for .
This procedure must encode the message into a sequence of integers between and , inclusive, that shall be sent using the parrots. To report this sequence, your procedure
encode
must call the proceduresend(a)
for each integer that you wish to give to one of the birds.Procedure
decode(N,L,X)
that takes the following parameters:- – the length of the original message.
- – the length of the message received (the number of birds that were sent).
- – a one-dimensional array of integers representing the received numbers. The numbers for are precisely the numbers that your procedure
encode
produced, but possibly rearranged into a different order.
This procedure must recover the original message. To report it, your procedure
decode
must call the procedureoutput(b)
for each integer in the decoded message, in the correct order.
Note that and are not given as input parameters – please see the subtask descriptions below.
In order to correctly solve a given subtask, your procedures must satisfy the following conditions:
- All integers sent by your procedure
encode
must be in the range specified in the subtask. - The number of times your procedure
encode
calls the proceduresend
must not exceed the limit specified in the subtask. Please note that depends on the length of the message. - Procedure
decode
must correctly recover the original message and call the procedureoutput(b)
exactly times, with equal to , respectively.
In the last subtask, your score varies according to the ratio between the lengths of the encoded message and the original message.
Example
Consider the case where , and .
Procedure encode(N,M)
, using some strange method, may encode the message as the sequence of numbers . To report this sequence, it should call the procedure send
as follows:
send(7)
send(3)
send(2)
send(70)
send(15)
send(20)
send(3)
Once all parrots reach their destination, assume we obtain the following list of transcribed numbers: . The procedure decode
will then be called with , , and
The procedure decode
must produce the original message . It reports the result by calling the procedure output
as follows.
output(10)
output(30)
output(20)
Subtasks
Subtask 1 (17 points)
- , and each integer in the array is either or .
- Each encoded integer must be in the range from to , inclusive.
- The number of times you can call the procedure
send
is at most .
Subtask 2 (17 points)
- .
- Each encoded integer must be in the range from to , inclusive.
- The number of times you can call the procedure
send
is at most .
Subtask 3 (18 points)
- .
- Each encoded integer must be in the range from to , inclusive.
- The number of times you can call the procedure
send
is at most .
Subtask 4 (29 points)
- .
- Each encoded integer must be in the range from to , inclusive.
- The number of times you can call the procedure
send
is at most .
Subtask 5 (up to 19 points)
- .
- Each encoded integer must be in the range from to , inclusive.
- The number of times you can call the procedure
send
is at most . - Important: the score for this subtask depends on the ratio between the length of the encoded message and that of the original message.
For a given test case in this subtask, let be the ratio between the length of the encoded message and the length of the original message. Let be the maximum of all . Your score for this subtask will be determined using the following rules:- If , you get the full score of points.
- If , you get points.
- If , you get points.
- If , your score is , rounded down to the nearest integer.
- If or any of your outputs is incorrect, your score is .
- Important: Any valid solution for subtasks 1 to 4 will also solve all preceding subtasks. However, due to the larger bound on , a valid solution to subtask 5 might not be able to solve subtasks 1 to 4. It is possible to solve all subtasks using the same solution.
Implementation details
Limits
- Grading Environment: In the real grading environment, your submissions will be compiled into two programs e and d to be executed separately. Both your encoder and decoder modules will be linked to each executable, but e only calls
encode
and d only callsdecode
. - CPU time limit: Program e will make calls to procedure
encode
and it should run in 1 second. Program d will make calls to proceduredecode
and it should run in 1 second.
Interface (API)
To be implemented by contestant:
void encode(int N, int M[]);
void decode(int N, int L, int X[]);
To be called by contestant:
void send(int a);
void output(int b);
Comments