#P1273. [ICPC2017 ASIA Daejeon]Untangling Chain
[ICPC2017 ASIA Daejeon]Untangling Chain
题目描述
A rectilinear chain is an ordered sequence that alternates horizontal and vertical segments as in Figure K.1. You are given a rectilinear chain whose first segment starts from the origin and goes to the right. Such a rectilinear chain of edges can be described as a sequence of pairs where is the length of the -th edge and denotes the turning direction from the -th edge to the -st edge .For,if the chain turns left from to ,then .and if it turns right,then .For, is set to be zero to indicate that is the last edge of the chain.For example,a rectilinear chain of six pairs shown in Figure 1(a) is described as a sequence of six pairs.
You would already notice that the rectilinear chain drawn by the given description is not simple. A chain is simple if any two edges in the chain have no intersection except at the end points shared by adjacent edges. To represent the chain in a more succinct way, you want to make it simple if it is not simple. In other words, you need to untangle a given rectilinear chain to a simple chain by modifying the length of its edges. But the length of each edge in the resulting chain must be at least 1 and at most and the turning directions must be kept unchanged. The chain shown in Figure K.1(b) shows one of possible modifications for the non-simple chain given in Figure K.1(a), and its description is .
Given a description of a rectilinear chain, you should write a program to untangle the rectilinear chain.
输入格式
Your program is to read from standard input. The first line contains an integer,,representing the number of edges of an input rectilinear chain. In the following lines,the -th line contains two integers and for the edge ,separated by a single space, where is the length of ,and is the turning direction from to ; if it is the left turn and if it is the right turn for ,and for .
输出格式
Your program is to write to standard output. The first line should contain positive integers, representing the length of the edges of your untangled simple chain according to the edge order of the input chain. Each length should be at least 1 and at most .Note that you do not need to output the turning directions because the turning directions of the simple chain is identical to the ones of the input chain. You can assume that any rectilinear chain described in the input can be untangled with the edge length condition.
The following shows sample input and output for two test cases.
样例
6
4 1
5 -1
2 -1
2 -1
4 1
5 0
4 5 2 2 1 2
6
3 1
3 1
2 1
4 1
1 1
3 0
2 3 2 2 1 1