IOI '02 P2 - Utopia Divided
View as PDFIOI '02 - Yong-In, Korea
The beautiful land of Utopia was once ravaged by war. When the
hostilities subsided the country was divided into four regions by a
longitude (north-south line) and a latitude (east-west line). The
intersection of these lines became known as the point . All four
parts claimed the name Utopia, but as time went by they generally became
known as Utopia 
 (northeast), 
 (northwest), 
 (southwest) and 
(southeast). A point in any of the regions was identified by its
distance east and its distance north of 
. These distances could be
negative; hence a point in Utopia 
 was designated by a (negative,
positive) pair, in Utopia 
 by a (negative, negative) pair, in Utopia 
by (positive, negative) and in Utopia 
 by a pair of positive numbers.
A major problem was that citizens were not permitted to cross borders.
Fortunately, some ingenious IOI contestants from Utopia developed a safe
means of teleportation. The machine requires code numbers, each of which
can only be used once. Now the challenge facing the team, and you, is to
guide the teleporter from its initial position of  to the regions
of Utopia in the order requested. You don't care where in a region you
land, but you will have a sequence of 
 region numbers that specify
the regions in which the teleporter is to land. You may be asked to land
in the same region in two or more consecutive stops. After leaving the
initial 
 point, you must never land on a border.
You will receive as input a sequence of  code numbers and are to
write them as a sequence of 
 code pairs, placing a plus or a minus
sign before each number. If you are currently at the point 
 and
use the code pair 
, you will be teleported to the point 
.
You have the 
 numbers, and you can use them in any order you
like, each with a plus or a minus sign.
Suppose you have code numbers  and are to guide
the teleporter according to the sequence of region numbers 
.
The sequence of code pairs 
 achieves
this as it teleports you from 
 to the locations 
and 
 in that order. These points are located in Utopia 
,
Utopia 
, Utopia 
, and Utopia 
, respectively.
You are given  distinct code numbers and a sequence of 
 region
numbers indicating where the teleporter is to land. Construct a sequence
of code pairs from the given numbers that guide the teleporter to go
through the given region sequence.
Input Specification
Your program is to read from standard input. The first line contains a
positive integer  
. The second line contains
the 
 distinct integer code numbers 
separated by single spaces. The last line contains a sequence of 
region numbers, each of which is 
, 
, 
, or 
.
Output Specification
Your program is to write to standard output. The output consists of 
lines, each containing a pair of code numbers each preceded by a sign
character. These are codes pairs that will direct the teleporter to the
given region sequence. Note that there must be no blank following a
sign, but there must be a single space after the first code number.
If there are several solutions your program can output any one of them.
If there are no solutions your program should output the single integer
0.
Sample Input 1
4
7 5 6 1 3 2 4 8
4 1 2 1
Sample Output 1
+7 -1
-5 +2
-4 +3
+8 +6
Sample Input 2
4
2 5 4 1 7 8 6 3
4 2 2 1
Sample Output 2
+3 -2
-4 +5
-6 +1
+8 +7
Comments