A - Timeout Editorial by en_translator
For beginners
- If you are new to learning programming and do not know where to start, please try Problem A "Welcome to AtCoder" from practice contest. There you can find a sample code for each language.
- Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in "AtCoder Beginners Selection".
- 「C++入門 AtCoder Programming Guide for beginners (APG4b)」 is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
- 「Python入門 AtCoder Programming Guide for beginners (APG4bPython)」 is a Python tutorial for competitive programmers. Again, this is only in Japanese.
This problem asks to determine if the given sequence \(T\) satisfies the condition for the elder to sleep, and print Yes or No accordingly.
In this problem, the elder has just been tapped on his shoulder right now, so assume that \(T_0=0\).
What is the condition for him to sleep? Suppose that he remains awake until \(T_i\) seconds past now. Then, if \(T_{i+1}-T_i \leq S\), then the elder does not sleep since being tapped at time \(T_i\) until being tapped at time \(T_{i+1}\). However, if \(T_{i+1}-T_i > S\), then he falls asleep at time \((T_i+S+0.5)\) \((<T_{i+1})\).
Therefore, the elder will fall asleep if and only if there exists \(i(0 \leq i \leq N-1)\) with \(T_{i+1}-T_i >S\).
The problem can be solved by reading the sequence \(T\) using a for statement, and check the condition for each \(i\) using for and if statements. In actual implementation, you may immediately print No and terminate the program once you find an \(i\) that satisfies the condition, or manage a flag that stores whether a conforming \(i\) exists.
Sample code (C++)
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s;
cin >> n >> s;
vector<int> t(n + 1, 0);
for (int i = 1; i <= n; ++i)
cin >> t[i];
for (int i = 1; i <= n; ++i) {
if (t[i] - t[i - 1] > s) { // Check if he sleeps
cout << "No" << endl;
return 0; // Terminate the program
}
}
cout << "Yes" << endl;
}
Sample code (Python)
n, s = map(int, input().split())
t = list(map(int, input().split()))
if t[0] > s:
print("No")
exit()
for i in range(1, n):
if t[i] - t[i - 1] > s: # Check if he sleepsj
print("No")
exit() # Terminate the program
print("Yes")
posted:
last update: