This page is still under construction.

Parts of this page are still being built. What you see may change.

Diamond Hands

Interview

Time limit2sMemory limit512 MB

Summary
Given stock price differences at n known days, reconstruct a history of +1 and -1 streaks from day 1 to day d_n using the fewest streaks, or report impossibility.
Level

Medium7 of 10

Topics
Greedy, Implementation, Math, Intervals
Solved
No attempts yet

Problem

The "Diamond Hands" corporation has a long and eventful history. Starting from its foundation, it had many successful and unsuccessful days. For simplicity, call a day successful if its stock price increased by one (in abstract units). Similarly, call a day unsuccessful if its stock price decreased by one. As often happens, successful days come in long streaks, and so do unsuccessful days. There is no middle ground: every day is either successful or unsuccessful.

You want to figure out which days were successful and which were unsuccessful for the corporation. To do this, you obtained the historical stock price data: nn pairs (d_i,p_i)(d\_i, p\_i), meaning that d_id\_i days after the stock was issued, the difference from the starting stock price was p_ip\_i units (p_ip\_i can be any integer, including negative numbers).

Represent the corporation's history with the minimum number of streaks of successful or unsuccessful days, or report that the data contains an error and it is impossible. If several answers achieve the minimum number of streaks, output any one of them.

Input

The first line contains an integer nn (1≤n≤200 0001 \le n \le 200\,000). The next nn lines each contain two integers d_i  p_id\_i \; p\_i (1≤d_i≤1081 \le d\_i \le 10^8; −108≤p_i≤108-10^8 \le p\_i \le 10^8; d_i<d_i+1d\_i < d\_{i+1} for every ii from 11 to n−1n - 1).

Output

If the historical stock price data contains errors, output −1-1. Otherwise, print kk, the number of streaks, on the first line. The next kk lines describe the streaks of successful or unsuccessful days. Each line must contain a pair l_i  c_il\_i \; c\_i (1≤l_i≤1081 \le l\_i \le 10^8; c_i∈{+,-}c\_i \in \{\texttt{+}, \texttt{-}\}), meaning that the next streak lasted l_il\_i days and was successful if c_i=+c\_i = \texttt{+}, or unsuccessful if c_i=-c\_i = \texttt{-}.

The description of the streaks must be in chronological order, starting from the day the stock was issued and ending on day d_nd\_n. That is, the sum of all l_il\_i must equal d_nd\_n.

Hint

In the first example, the first three days are successful, so after 2 days the difference is 2, and after 3 days the difference is 3. The next three days are unsuccessful, so after 5 days the difference becomes 1, and after 6 days the stock price is back to its initial value. The last seventh day is successful, so the final difference after 7 days is 1.

Examples3

  1. Example 1

    Input
    4
    2 2
    3 3
    5 1
    7 1
    
    Expected output
    3
    3 +
    3 -
    1 +
    
  2. Example 2

    Input
    2
    3 -3
    7 -3
    
    Expected output
    2
    5 -
    2 +
    
  3. Example 3

    Input
    1
    1 0
    
    Expected output
    -1