jiejiejiang2004

题解:Codeforces Round 964 (Div. 4) C

C. Showering

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output


As a computer science student, Alex faces a hard challenge — showering. He tries to shower daily, but despite his best efforts there are always challenges. He takes \(s\) minutes to shower and a day only has \(m\) minutes!

He already has \(n\) tasks planned for the day. Task \(i\) is represented as an interval \((l_i\), \(r_i)\), which means that Alex is busy and can not take a shower in that time interval (at any point in time strictly between \(l_i\) and \(r_i\)). No two tasks overlap.

Given all \(n\) time intervals, will Alex be able to shower that day? In other words, will Alex have a free time interval of length at least \(s\)?

In the first test case, Alex can shower for the first \(3\) minutes of the day and not miss any of the tasks.

Input

The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.

The first line of each test case contains three integers \(n\), \(s\), and \(m\) (\(1 \leq n \leq 2 \cdot 10^5\); \(1 \leq s, m \leq 10^9\)) — the number of time intervals Alex already has planned, the amount of time Alex takes to take a shower, and the amount of minutes a day has.

Then \(n\) lines follow, the \(i\)-th of which contains two integers \(l_i\) and \(r_i\) (\(0 \leq l_i \lt r_i \leq m\)) — the time interval of the \(i\)-th task. No two tasks overlap.

Additional constraint on the input: \(l_i \gt r_{i-1}\) for every \(i \gt 1\).

The sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).

Output

For each test case output "YES" (without quotes) if Alex can take a shower for that given test case, and "NO" (also without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response).

题意

小明每天很忙,但是还要洗澡
小明每天只有 \(m\) 小时可以分配
小明每天洗澡需要连续的 \(s\) 小时
小明每天要完成 \(n\) 个任务
小明做的第 \(i\) 个任务需要再 \(l_1\)\(r_1\) 这个时间区间完成
小明的每个任务时间不会重叠
问:小明有空洗澡吗

Example

Input

4
3 3 10
3 5
6 8
9 10
3 3 10
1 2
3 5
6 7
3 3 10
1 2
3 5
6 8
3 4 10
1 2
6 7
8 9

Output

YES
YES
NO
YES

题解

s和以下时间间隔比较

  • 最前面的任务和第0小时之间的空闲时间
  • 一天最后的的第m小时和最后一个任务结束之间的空闲时间
  • 每个任务之间的空闲时间

只要比任意一个时间间隔小
小明就能洗澡了

代码

#include <bits/stdc++.h>
#define int unsigned long long
#define INF 0x3f3f3f3f
#define all(x) x.begin(),x.end()

int t = 1;

void solve() {
    int n,s,m;
    std::cin >> n >> s >> m;
    std::vector<std::pair<int,int> > a(n);
    for(int i = 0 ; i < n; i ++) {
        std::cin >> a[i].first >> a[i].second;
    }
    std::sort(all(a));
    for(int i = 0 ; i < n ; i ++) {
        int kong;
        if(!i) kong = a[i].first - 0;
        else kong = a[i].first - a[i-1].second;

        if(kong >= s) {
            std::cout << "YES\n";
            return;
        }
    }
    if(m - a[n-1].second >= s) std::cout << "YES\n";
    else std::cout << "NO\n";
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    std::cin >> t;
    while(t--) solve();
    return 0;
}

posted on 2024-08-07 16:12  Jiejiejiang  阅读(8)  评论(0编辑  收藏  举报

导航