1. 程式人生 > 其它 >AtCoder題解 —— AtCoder Beginner Contest 185 —— B - Smartphone Addiction —— 模擬演算法

AtCoder題解 —— AtCoder Beginner Contest 185 —— B - Smartphone Addiction —— 模擬演算法

技術標籤:OJ題解# AtCoder題解AtCoder題解ABC185B題

題目相關

題目連結

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,直接處理資料即可。