- MysGln 的博客
Programming exercise on 23 August
- @ 2024-8-23 17:14:58
A
This problem asks you to handle a for loop properly.
First, let us consider how we can perform the operation against . Among various approaches, we think that updating in the following way is the naivest one.
- For in this order, “update by assigning .” (This can be implemented with a for loop with a variable .)
- Update by assigning .
All that left is to repeat the steps above times, which can also be implemented with a for loop to let the same process be executed times.
A sample code in Python:
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(K):
A = A[1:] + [0]
print(*A)
B
The theme of this problem is implementation. First, let us consider how to determine if the time : is confusing. It can be determined by the following procedure. (For more details, please refer to the sample code.)
- Find the tens place and ones place of using a integral division (
/in C++ and//in Python) and a modulus operation (%). - Find the tens place and ones place of in the same way.
- Find and using additions and multiplications.
- Determine if is valid in the -hour system. This can be determined by checking if and .
- The resulting boolean value (Yes or No) directly serves as the answer to “is confusing?”
Once you can determine if a time is confusing, this problem can be solved with a loop structure like a while statement. That is, it is sufficient to repeat incrementing the time by as long as is not a confusing time.
Note that, the one minute after is in most case, there may be carries, when it causes or .
The time complexity is O((the number of distinct times represented by 24-hour clock system))$O((the number of distinct times represented by 24-hour clock system)). Since they are 1440$ such times, so we can determine it fast enough.
A sample code in Python follows.
def is_in_24_hours(h, m):
return 0 <= h <= 23 and 0 <= m <= 59
def misjudged(h, m):
A, B = h // 10, h % 10
C, D = m // 10, m % 10
AC = A * 10 + C
BD = B * 10 + D
return is_in_24_hours(AC, BD)
H, M = map(int, input().split())
while not misjudged(H, M):
M += 1
if M == 60:
H, M = H + 1, 0
if H == 24:
H = 0
print(H, M)
C
Consider maintaining the follow network with a set . The given query can be rephrased as follows. Initially, .
- Append an ordered pair to the set . That is, let .
- Remove an ordered pair from the set . That is, let .
- Determine if the ordered pairs are both contained in .
There are kinds of ordered pair that may be given from the input, but the size of the set is at most throughout the queries, so we can manage with a data structure like an associative array to solve the problem.
Even if your library do not allows to use an ordered pair as the key, you may still correspond an ordered pair to an integer to solve this problem.
The time complexity is if you use a data structure like a balanced binary search tree as an associative array, or expected time if you use a hash table etc. .
A sample code in Python follows.
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
Set = set()
for _ in range(Q):
t, a, b = map(int, input().split())
if t == 1:
Set.add((a, b))
elif t == 2:
if (a, b) in Set:
Set.remove((a, b))
else:
if (a, b) in Set and (b, a) in Set:
print("Yes")
else:
print("No")