题解: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 s s minutes to shower and a day only has m m m minutes!

He already has n n n tasks planned for the day. Task i i i is represented as an interval ( l i (l_i (li, r i ) r_i) ri), 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 l_i li and r i r_i ri). No two tasks overlap.

Given all n n 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 s s?

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

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

Input

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

The first line of each test case contains three integers n n n, s s s, and m m m ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105; 1 ≤ s , m ≤ 1 0 9 1 \leq s, m \leq 10^9 1s,m109) — 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 n n lines follow, the i i i-th of which contains two integers l i l_i li and r i r_i ri ( 0 ≤ l i < r i ≤ m 0 \leq l_i \lt r_i \leq m 0li<rim) — the time interval of the i i i-th task. No two tasks overlap.

Additional constraint on the input: l i > r i − 1 l_i \gt r_{i-1} li>ri1 for every i > 1 i \gt 1 i>1.

The sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

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 m m 小时可以分配
小明每天洗澡需要连续的 s s s 小时
小明每天要完成 n n n 个任务
小明做的第 i i i 个任务需要再 l 1 l_1 l1 r 1 r_1 r1 这个时间区间完成
小明的每个任务时间不会重叠
问:小明有空洗澡吗

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;
}

转载自博客https://www.cnblogs.com/jiejiejiang2004/p/18347242
博主已同意,我就是博主

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值