Official

B - Shout Everyday Editorial by en_translator


For beginners

The problem can be solved by doing casework depending on whether he is asleep at midnight

[1] If he is not asleep at midnight

Within a day, He goes to bed and wakes up, so \(B<C\). Since he sleeps from \(B\) to \(C\) o’clock, the answer is No if \(A\) o’clock is in between, i.e. if \(B<A<C\). Otherwise the answer is Yes.

[2] If he is asleep at midnight

Within a day, he first wakes up, then goes to bed, so \(C<B\). Since he is awake from \(C\) to \(B\) o’clock, the answer is YEs if \(A\) o’clock is in between, i.e. if \(C<A<B\). Otherwise the answer is No.

Sample code (Python)

A, B, C = map(int, input().split())
if B < C:
    if B < A < C:
        print("No")
    else:
        print("Yes")
else:
    if C < A < B:
        print("Yes")
    else:
        print("No")

Sample code (C++)

#include <bits/stdc++.h>
using namespace std;

int main()
{
  int A, B, C;
  cin >> A >> B >> C;
  if (B < C)
  {
    if (B < A && A < C)
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
  else
  {
    if (C < A && A < B)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

In a problem with a lot of casework like this, it is sometimes effective to adopt the way that does not require a casework to avoid penalties due to oversight or mistakes in caseworks.

In this problem, one can use a while statement to avoid caseworks. Starting from \(B\) o’clock, increment the current hour of day one by one. If \(A\) o’clock comes before becoming \(C\) o’clock, then print No; otherwise, print Yes.

Sample code (Python)

A, B, C = map(int, input().split())
now = B
while now != C:
    if now == A:
        print("No")
        exit()
    now += 1
    now %= 24

print("Yes")

posted:
last update: