Jet Lag

시간 제한2초메모리 제한1024 MB

요약
활동 시간 구간들이 주어질 때 모든 활동에 참여할 수 있도록 수면 시간을 정수 분 단위로 배치할 수 있는지 판정하고, 가능하면 그러한 일정 하나를 출력한다.
난이도

어려움10점 중 8점

유형
그리디, 구간, 구현, 시뮬레이션
정답자
아직 제출이 없습니다

문제

The ICPC World Finals are here and they are packed full of activities you want to attend — speeches, presentations, fun events, not to mention the contest itself. There is only one problem: when are you going to sleep?

When you fall asleep, you always set a timer because otherwise you would be able to sleep forever. Using the timer, you can choose to sleep for any positive integer amount of minutes. After sleeping for kk minutes, you will be rested for another kk minutes (and so you will not be able to fall asleep again); and then you will be able to function for a third kk minutes (so you can stay awake, but you can also go to sleep if you want to).

You know the times of all the activities at the Finals; you should plan your sleep schedule to not miss any part of any event. Just before the Finals start (at minute 00), you will arrive in your hotel room after a long journey and you will need to sleep immediately.

입력

The first line of input contains a positive integer nn (1≤n≤200,0001≤n≤200\\,000), the number of activities planned for the Finals.

The iith of the remaining nn lines contains two positive integers b_ib\_i and e_ie\_i (b_i\<e_ib\_i\<e\_i, e_i≤b_i+1e\_i≤b\_i+1, 0≤b_1,e_n≤10100≤b\_1, e\_n≤10^{10}), the beginning and end time of the activity, counted in minutes from the beginning of the Finals.

출력

If it is possible to find a sleep schedule that allows you to participate in all planned activities in their entirety, then output such a schedule in the format described below. Otherwise, output impossible.

A sleep schedule is specified by a line containing the number pp (1≤p≤1061≤p≤10^6) of sleep periods, followed by pp lines. The iith of these lines contains two integers s_is\_i and t_it\_i — the beginning and end time of the iith sleep period, counted in minutes from the beginning of the Finals. Note that you should not output any sleep period after the last activity.

The sleep periods must satisfy 0=s_1\<t_1\<s_2\<t_2<…\<t_p≤b_n0=s\_1\<t\_1\<s\_2\<t\_2<\dots \<t\_p≤b\_n as well as the condition described in the statement that does not allow you to fall asleep for some time after a sleep period. You may fall asleep immediately after an activity (so it may be that s_i=e_js\_i=e\_j) and you may wake up just before an activity (so it may be that t_i=b_jt\_i=b\_j).

If there are multiple valid sleep schedules, any one will be accepted. It can be shown that if there is a valid sleep schedule, then there is also one with at most 10610^6 sleep periods.

예제3

  1. 예제 1

    입력
    3
    30 45
    60 90
    120 180
    
    예상 출력
    2
    0 30
    90 120
    
  2. 예제 2

    입력
    1
    0 60
    
    예상 출력
    impossible
    
  3. 예제 3

    입력
    7
    31 32
    35 41
    48 55
    69 91
    1000 2022
    2022 2023
    2994 4096
    
    예상 출력
    5
    0 5
    10 28
    56 68
    92 900
    2025 2900