AtCoder题解 —— AtCoder Beginner Contest 185 —— B - Smartphone Addiction —— 模拟算法

题目相关

题目链接

AtCoder Regular Contest 185 B 题,https://atcoder.jp/contests/abc185/tasks/abc185_b

Problem Statement

The battery of Takahashi's smartphone has N mAh capacity. At time 0.5, 1.5, 2.5, and so on (that is, at time n+0.5 for every integer n), the battery charge decreases by 1 mAh.
Takahashi will leave his house with his phone fully charged at time 0, visit a cafe M times, and return home at time T.
He will stay at the i-th cafe from time Ai to time Bi. During this stay, he charges his phone, so the battery charge does not decrease. Instead, at time n+0.5 for every integer n, it increases by 1. However, if it is already equal to the battery capacity, it does not increase nor decrease.
Determine whether he can return home without the battery charge dropping to 0 on the way.

Input

Input is given from Standard Input in the following format:

N M T
A1 B1
A2 B2
A3 B3
⋮
AM BM

Output

If Takahashi can return home without the battery charge dropping to 0 on the way, print Yes; otherwise, print No.

Samples1

Sample Input 1

10 2 20
9 11
13 17

Sample Output 1

Yes

Explaination

The battery charge changes as follows:

  • Time 0 (leaving home): 10mAh
  • Time 9 (the beginning of the stay at the first cafe): 1mAh
  • Time 11 (the end of the stay at the first cafe): 3mAh (He charges his phone in a cafe.)
  • Time 13 (the beginning of the stay at the second cafe): 1mAh
  • Time 17 (the end of the stay at the second cafe): 5mAh
  • Time 20 (getting home): 2mAh

During this process, the battery charge never drops to 0, so we print Yes.

Samples2

Sample Input 2

10 2 20
9 11
13 16

Sample Output 2

No

Explaination

This case is the same as Sample Input/Output 1 until he starts his stay at the second cafe with 1 mAh charge.
When he ends his stay there at time 16, the battery charge is 4 mAh.
Then at time 19.5, it drops to 0, so we print No.

Samples3

Sample Input 3

15 3 30
5 8
15 17
24 27

Sample Output 3

Yes

Explaination

The battery charge drops to 1 mAh when he gets home, but it never drops to 0 on the way.

Samples4

Sample Input 4

20 1 30
20 29

Sample Output 4

No

Explaination

The battery charge drops to 0 at time 19.5.

Samples5

Sample Input 5

20 1 30
1 10

Sample Output 5

No

Explaination

Note that when the battery charge is equal to the battery capacity, staying at a cafe does not increase the battery charge.

Constraints

  • 1≤N≤10^9
  • 1≤M≤1000
  • 1≤T≤10^9
  • 0<A1<B1<A2<B2<A3<B3<⋯<AM<BM<T
  • All values in input are integers.

题解报告

题目翻译

高桥手机电池最大容量是 N 毫安时. 在 0.5,1.5,2.5 分钟时候,也就是任何一个整数 n+0.5,电池都会放电 1 毫安时。高桥在 0 时刻离开家,这个时候电池是充满的,他将访问 M 个咖啡馆,并在 T 时刻回到家。

高桥将在 Ai 时刻到达第 i 个咖啡馆,并在 Bi 时刻离开,当在咖啡馆内,高桥将对手机充电,在每 n+0.5 时刻,手机电池增加 1 毫安时。但是电池充满后,电量将不会增加。

请判断高桥的整个行程中,电池电量会不会变成 0 毫安时。

题目分析

一个模拟题,我们需要模拟整个行程,判断会不会出现电池变为 0 的情况。注意题目虽然多次提到了 n+0.5 这个问题,我们在判断的时候,就是按整数时间即可。下面我们对样例数据进行一个简单的分析,来加深对题目理解。

样例数据

样例 1

根据样例数据,我们知道电池容量为 10,会经过 2 个咖啡馆,第 20 分钟回到家。这样我们可以得到如下的行程:

第 0 分钟,高桥手机电量为 10。

第 9 分钟,高桥抵达第一个咖啡馆。此时,手机电量为 10-(9-0)*1=1。

第 11 分钟,高桥对手机进行充电。此时,手机电量为 1+(11-9)*1=3。

第 13 分钟,高桥抵达第二个咖啡馆。此时,手机电量为 3-(13-11)*1=1。

第 17 分钟,高桥对手机进行充电。此时,手机电量为 1+(17-13)*1=5。

第 20 分钟,高桥到家。此时,手机电量为 5-(20-17)*1=2。

所以高桥手机电量不会变成 0。

样例 2

根据样例数据,我们知道电池容量为 10,会经过 2 个咖啡馆,第 20 分钟回到家。这样我们可以得到如下的行程:

第 0 分钟,高桥手机电量为 10。

第 9 分钟,高桥抵达第一个咖啡馆。此时,手机电量为 10-(9-0)*1=1。

第 11 分钟,高桥对手机进行充电。此时,手机电量为 1+(11-9)*1=3。

第 13 分钟,高桥抵达第二个咖啡馆。此时,手机电量为 3-(13-11)*1=1。

第 16 分钟,高桥对手机进行充电。此时,手机电量为 1+(16-13)*1=4。

第 20 分钟,高桥到家。此时,手机电量为 4-(20-16)*1=0。

很不幸,手机电量变为 0。

样例 5

根据样例数据,我们知道电池容量为 20,会经过 1 个咖啡馆,第 30 分钟回到家。这样我们可以得到如下的行程:

第 0 分钟,高桥手机电量为 20。

第 1 分钟,高桥抵达第一个咖啡馆。此时,手机电量为 20-(1-0)*1=19。

第 10 分钟,高桥对手机进行充电。此时,手机电量为 19+(10-1)*1=28,但是超过了手机电池容量,因此电量还是 20。

第 30 分钟,高桥到家。此时,手机电量为 20-(30-10)*1=0。

很不幸,手机电量变为 0。

数据范围估计

根据提供的数据范围,N 的最大值为 1e9,而且数据不会超过 1e9,因此用 int 即可。

AC 参考代码

由于本题已经保证了 0<A1<B1<A2<B2<A3<B3<⋯<AM<BM<T,都不需要对数据进行合法性判断。

//https://atcoder.jp/contests/abc185/tasks/abc185_b
//B - Smartphone Addiction
#include <bits/stdc++.h>

using namespace std;

//如果提交到OJ,不要定义 __LOCAL
//#define __LOCAL

const int MAXM=1e3+4;
int a[MAXM], b[MAXM];

int main() {
#ifndef __LOCAL
    //这部分代码需要提交到OJ,本地调试不使用
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    int n,m,t;
    cin>>n>>m>>t;
    for (int i=1; i<=m; i++) {
        cin>>a[i]>>b[i];
    }

    //模拟
    int n1=n;
    for (int i=1; i<=m; i++) {
        //放电
        n=n-(a[i]-b[i-1]);

        //判断
        if (n<=0) {
            cout<<"No\n";
            return 0;
        }

        //充电
        n=n+(b[i]-a[i]);
        if (n>n1) {
            n=n1;
        }
    }

    //放电
    n=n-(t-b[m]);
    //判断
    if (n<=0) {
        cout<<"No\n";
    } else {
        cout<<"Yes\n";
    }

#ifdef __LOCAL
    //这部分代码不需要提交到OJ,本地调试使用
    system("pause");
#endif
    return 0;
}

时间复杂度

O(M)。

空间复杂度

O(M)。当然本题可以将空间复杂度优化到 O(1),就是我们不需要数据来保存 Ai 和 Bi,直接处理数据即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值