#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 (0,0)(0,0) and goes to the right. Such a rectilinear chain of nn edges can be described as a sequence of nn pairs(lk,tk)(l_k,t_k) wherelkl_k is the length of the kk-th edge eke_k and tkt_k denotes the turning direction from the kk-th edge eke_k to the (k+1)(k+1)-st edge ek+1e_k+1.For1lek<n1\\le k< n,if the chain turns left from eke_k to ek+1e_k+1,then tk=1t_k=1.and if it turns right,then tk=1t_k=-1.Fork=nk=n,tkt_k is set to be zero to indicate that eke_k 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(4,1),(5,1),(2,1),(2,1),(4,1),(5,0)(4,1),(5,-1),(2,-1),(2,-1),(4,1),(5,0).

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 nn 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 (4,1),(5,1),(2,1),(2,1),(1,1),(2,0)(4,1),(5,-1),(2,-1),(2,-1),(1,1),(2,0).

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,n(1lenle10,000)n(1\\le n\\le 10,000),representing the number of edges of an input rectilinear chain. In the following nn lines,the kk-th line contains two integers lkl_k and tkt_k for the edge eke_k,separated by a single space, where lk(1lelkle10,000l_k(1\\le l_k\\le 10,000 is the length of eke_k,and tkt_k is the turning direction from eke_k to ek+1e_k+1;tk=1t_k=1 if it is the left turn and tk=1t_k=-1 if it is the right turn for (1lek<n)(1\\le k< n),and tk=0t_k=0 for k=nk=n.

输出格式

Your program is to write to standard output. The first line should contain nn 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 nn.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