logo AlgoBeat OnlineJudge
登录 注册

#216640. [GKS 2017 #D] Sightseeing

内存限制:1024 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

When you travel, you like to spend time sightseeing in as many cities as possible, but sometimes you might not be able to because you need to catch the bus to the next city. To maximize your travel enjoyment, you decide to write a program to optimize your schedule.

You begin at city at time and plan to travel to cities to in ascending order, visiting every city. There is a bus service from every city i to the next city i + . The i-th bus service runs on a schedule that is specified by integers: , and , the start time, frequency and ride duration. Formally, this means that there is a bus leaving from city i at all times , where x is an integer and , and the bus takes time to reach city i + .

At each city between and , inclusive, you can decide to spend time sightseeing before waiting for the next bus, or you can immediately wait for the next bus. You cannot go sightseeing multiple times in the same city. You may assume that boarding and leaving buses takes no time. You must arrive at city by time at the latest. (Note that you cannot go sightseeing in city , even if you arrive early. There's nothing to see there!)

What is the maximum number of cities you can go sightseeing in?

输入格式

The input starts with one line containing one integer , which is the number of test cases. test cases follow.

Each test case begins with a line containing integers, , and , representing the number of cities, the time taken for sightseeing in any city, and the latest time you can arrive in city .

This is followed by lines. On the i-th line, there are integers, , and , indicating the start time, frequency, and duration of buses travelling from city i to city i + .

输出格式

For each test case, output one line containing Case #x: y, where is the test case number (starting from ) and is the maximum number of cities you can go sightseeing in such that you can still arrive at city by time at the latest. If it is impossible to arrive at city by time , output Case #x: IMPOSSIBLE.

样例

样例输入 1

4
4 3 12
3 2 1
6 2 2
1 3 2
3 2 30
1 2 27
3 2 1
4 1 11
2 1 2
4 1 5
8 2 2
5 10 5000
14 27 31
27 11 44
30 8 20
2000 4000 3

样例输出 1

Case #1: 2
Case #2: 0
Case #3: IMPOSSIBLE
Case #4: 4

数据范围与提示

In the first test case, you can go sightseeing in city , catching the bus leaving at time and arriving at time . You can go sightseeing in city , leaving on the bus at time . When you arrive in city at time you immediately board the next bus and arrive in city just in time at time .

Limits

.

Small dataset (Test set - Visible)

.

.

.

.

.

.

Large dataset (Test set - Hidden)

.

.

.

.

.

.