Effcient Slabstones Rearrangement

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

요약
길이 x인 새 슬래브를 놓을 수 있도록 간격 d를 유지하며 기존 슬래브 n개를 옮길 때 필요한 인접 이동 횟수의 최솟값을 구한다.
난이도

어려움10점 중 8점

유형
그리디, 누적 합, 구현, 배열
정답자
아직 제출이 없습니다

문제

Barbara has a garden. The garden is long and narrow, divided into mm equal-sized regions arranged in a row. Her friend, Babara, gave her nn slabstones as birthday present. Barbara then placed these slabstones in her garden, so she can enjoy stepping slabstones from one to another every day. The ii-th slabstone fully occupies the l_il\_i-th to r_ir\_i-th region of the garden. The slabstones do not overlap, and any two slabstones have at least dd empty regions between them.

Below is a valid placement of the slabstones with m=15m = 15, n=3n = 3, d=2d = 2, and the three slabstones occupy the regions 2−42-4, 7−77-7, 12−1312-13 respectively.

Barbara recently bought another slabstone that will occupy xx consecutive regions in her garden. She will shift the original slabstones within the garden, then place the new slabstone somewhere in the garden. After shifting the original slabstones and placing the new slabstone, the slabstones cannot overlap, and any two slabstones must have at least dd empty regions between them. The slabstones should remain non-overlapping during slabstone rearrangement.

Shifting a single slabstone to an adjacent region takes one minute. For example, the above rearrangement process takes 44 minutes. Now Barbara wants to know the minimum possible total time required to rearrange the slabstones, so she can save time for “other purposes”.

입력

The first line contains four integers nn, mm, dd and xx. The ii-th of the following nn lines contains two integers l_il\_i and r_ir\_i.

출력

The minimum possible total time (in minutes) to rearrange the slabstones so the new slabstone can be placed in the garden. If the new slabstone cannot be placed in the garden no matter how the slabstones are rearranged, just output -1.

제한

  • 1≤n≤20001 ≤ n ≤ 2000
  • 1≤d≤m≤1091 ≤ d ≤ m ≤ 10^9
  • 1≤x≤m≤1091 ≤ x ≤ m ≤ 10^9
  • 1≤l_i≤r_i≤m1 ≤ l\_i ≤ r\_i ≤ m for i∈1,2,…,ni \in \\{1, 2,\dots ,n\\}
  • r_i+d+1≤l_i+1r\_i + d + 1 ≤ l\_i+1 for i∈1,2,…,n−1i \in \\{1, 2,\dots ,n - 1\\}. That is, the slabstones are given in order from left to right.

예제3

  1. 예제 1

    입력
    3 15 2 3
    2 4
    7 7
    12 13
    
    예상 출력
    4
    
  2. 예제 2

    입력
    5 100 1 75
    2 3
    5 7
    11 13
    17 19
    23 29
    
    예상 출력
    9
    
  3. 예제 3

    입력
    1 100 99 1
    1 1
    
    예상 출력
    -1