CF1776E.Crossing the Railways

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Isona is in a train station. This station has two platforms, and between them there are mm parallel railways that can be viewed as infinite straight lines. Each railway is identified with an integer from 11 to mm , railway 11 being the closest to the first platform and railway mm being the farthest. There is a 11 meter distance between consecutive railways, as well as between each platform and its closest railway.

Isona is standing on the inner border of the first platform, when she realizes that she forgot to validate her ticket! There is a validating machine on the second platform, exactly opposite her current position (thus, the distance between Isona and the validating machine is m+1m + 1 meters). There are only ss seconds left to validate the ticket and the bridge designated to cross the railways is too far from the validating machine. Therefore, Isona (who is very brave and a little bit careless) will cross the railways running in a straight line perpendicular to the railways themselves. Isona can only run forward (not backward) and she can stay still. When she runs at maximum speed, she needs vv seconds to traverse 11 meter. She can run at any speed less than or equal to her maximum speed.

There is only one problem: nn trains are programmed to transit through the railways. The ii -th train will use the railway rir_i . It will start crossing the straight line between Isona and the validating machine aia_i seconds from now and it will end bib_i seconds from now. Of course, Isona cannot cross a railway when a train is passing. Formally, for every i=1,2,,ni = 1, \, 2, \, \dots, \, n , Isona is not allowed to be on railway rir_i at any time tt with ai<t<bia_i < t < b_i (but she is allowed to cross at times aia_i or bib_i ).

The following picture summarizes the situation. In the picture there are m=4m = 4 railways and two trains are visible; the train going through railway 33 is currently crossing the line between Isona and the validating machine.

Isona is a really good runner, but she gets tired every time she has to change her running speed. What is the minimum number of speed changes she has to perform to get to the validating machine on the other platform within ss seconds from now? Note that at the beginning Isona is not running. She can start to run anytime. The instant she starts to run (i.e. her speed becomes positive) is not counted as a speed change.

输入格式

The first line of the input contains four integers nn , mm , ss , vv ( 1n5001 \leq n \leq 500 , 1m101 \leq m \leq 10 , 1s,v1091 \leq s, v \leq 10^9 ) — the number of trains, the number of railways, the maximum time in seconds Isona can spend crossing the railways, and the number of seconds she needs to traverse 11 meter at maximum speed.

Each of the next nn lines contains three integers aia_i , bib_i , rir_i ( 1ai<bi1091 \leq a_i < b_i \leq 10^9 , 1rim1 \leq r_i \leq m ) — the start and end times of the ii -th train crossing the straight line between Isona and the validating machine, and the railway it will be using.

It is guaranteed that, for any two trains ii and jj that go through the same railway (i.e. ri=rjr_i = r_j ), there is at least 11 second between them (that is, either ajbi+1a_j \ge b_i + 1 or aibj+1a_i \ge b_j + 1 ).

输出格式

Print the minimum number of speed changes Isona has to perform to get to the validating machine in time. If this is impossible, print 1-1 .

输入输出样例

  • 输入#1

    4 3 5 1
    1 2 1
    3 4 1
    2 3 2
    3 4 3

    输出#1

    0
  • 输入#2

    3 3 12 2
    2 10 1
    1 6 2
    8 12 3

    输出#2

    2
  • 输入#3

    8 4 13 2
    1 4 1
    5 13 1
    1 5 2
    6 13 2
    1 9 3
    10 13 3
    1 10 4
    11 13 4

    输出#3

    2
  • 输入#4

    1 1 2 2
    1 2 1

    输出#4

    -1

说明/提示

In the first sample, if Isona starts running at time t=0t=0 at maximum speed ( 11 m/s), she will cross each railway just when a train is about to traverse it, and she will arrive at the other platform at time 4=s14 = s - 1 without changing speed.

In the second sample, a possible solution with 22 speed changes is the following: for the first 22 seconds Isona goes at maximum speed ( 0.50.5 m/s), then she slows down to 0.250.25 m/s for 44 seconds until she reaches the second railway. At that point, she goes at maximum speed again until she reaches the other platform.

In the third sample, Isona can wait 22 seconds before starting running. She then runs for 55 seconds at maximum speed ( 0.50.5 m/s). After that, she waits for 11 second not running (or running at 00 m/s), and finally she runs again at maximum speed for the last 55 seconds. Overall, she changes speed twice.

首页