I don’t feel very well today…
Although the problem is pretty simple, it was a bit hard to understand what was asked.
for tc in range(int(input())):
= map(int, input().split())
n, m list[int] = []
sl: for nc in range(n):
len(input()))
sl.append(int = 0
ans: int = 0
tmp: for length in sl:
if tmp + length > m:
break
+= length
tmp += 1
ans print(ans)
It seems there isn’t an editorial on the internet yet, so I’m not confident if my approach is optimal or does not have any corner cases.
First, to be able to make all the elements equal, the sum of all elements must be divisible by the number of elements.
Second, when we look at the leftmost (or the rightmost) element, it can be updated only from one direction. The same goes for i = 1 (0-index) or i = n - 2.
Now, i = 2 (0-index) must be updated from the right side not to break i = 0. Recursively (?), i = 4, 6, … must be updated from the right side to meet the condition.
In my approach, I make ends meet from the left side and see if all elements can be the average of the input.
def can_flatten(n: int, a: list[int]) -> bool:
if sum(a) % n != 0:
return False
int = sum(a) // n
eq_val: for i in range(n - 2):
+ 2] += a[i] - eq_val
a[i
return a[-1] == a[-2] == eq_val
for _ in range(int(input())):
int = int(input())
n: list[int] = list(map(int, input().split()))
a:
bool = can_flatten(n, a)
ans: if ans:
print("YES")
else:
print("NO")
A number is divisible by 3 <=> The sum of all digits is divisible by 3.
A number is divisible by 9 <=> The sum of all digits is divisible by 9.
<=> (The sum of all digits) mod 9 = 0
…choose one of its digits, square it, and replace the original digit with the result. The result must be a digit
So, we cannot square a digit >= 4 because its square exceeds 10.
On the other hand, a square of 1
is 1
, so
performing the operation on a digit with 1
has no
effect.
2 ** 2 = 4
-> doubling a digit with 2
increases the sum of (digit mod 9) by 2 mod 9
.
3 ** 2 = 9
-> doubling a digit with 3
increases the sum of (digit mod 9) by 6 mod 9
.
Applying the operation to double 3
for one digit ->
increase 6 mod 9
Applying the operation to double 3
for two digits ->
increase (6 + 6) mod 9 = 3 mod 9
Applying the operation to double 3
for three digits
-> increase (6 * 3) mod 9 = 0 mod 9
Applying the operation to double 3
for four digits ->
increase 6 mod 9 (same as performing only one operation on
3
)
As we can see, it’s not meaningful to perform the operation on
3
more than four times since it just repeats the same
result that appeared before. <=> The maximum number of the
operations for 3
we need to consider is
3.
The same goes for digits with 2
.
[(count, increased mod 9)] = [
(9, 0 mod 9), (1, 2 mod 9), (2, 4 mod 9), (3, 6 mod 9), (4, 8 mod 9),
(5, 1 mod 9), (6, 3 mod 9), (7, 5 mod 9), (8, 7 mod 9), (9, 0 mod 9),
... (and repeat)
]
So, for digits with 2
, the maximum number of the given
operations that is meaningful and we must consider is
8.
def can_obtain(mod9, cnt2, cnt3) -> bool:
= min(cnt2, 8)
cnt2 = min(cnt3, 2)
cnt3
for i in range(cnt2 + 1):
= 2 * i
sum2 for j in range(cnt3 + 1):
= 6 * j
sum3 if (mod9 + sum2 + sum3) % 9 == 0:
return True
return False
for _ in range(int(input())):
str = input()
n: int = 0
mod9: int = 0
cnt2: int = 0
cnt3: for c in n:
= (mod9 + int(c)) % 9
mod9 if c == "3":
+= 1
cnt3 if c == "2":
+= 1
cnt2
bool = can_obtain(mod9, cnt2, cnt3)
ans: if ans:
print("YES")
else:
print("NO")
My approach still doesn’t quite make sense to me, but I’ll write it down for reference.
It costs 1
to move a number one index to the left.
Let’s say we have this input
1 1 5 6 9
The i = 4 (0-index) element can be moved one index to the left with one decrement.
1 1 5 6 9
->
1 1 5 8 6
In the same way, the i = 4 element can be moved two indexes to the left with two decrement, by applying the operation mentioned above twice.
1 1 5 6 9
(
->
1 1 5 8 6
)
-> 1 1 7 5 6
Let’s make this generic. For an index i
, the maximum
number it can be is
= max(
s[i]
[+ 1] - 1, s[i + 2], - 2, s[i + 3] - 3, s[i + 4] - 4,
s[i], s[i + 5] - 5, s[i + 6] - 6, s[i + 7] - 7, s[i + 8] - 8,
s[i # s[i + 9] - 9 <= 0, so i + 9, i + 10, ... won't be the options
] )
To make the lexicographically maximum string, we start from the leftmost index to the right, and pick the maximum value for each index.
for _ in range(int(input())):
list[int] = list(map(int, list(input())))
s: for i in range(len(s) - 1):
int = i
max_i: int = s[i]
max_val: for j in range(1, 9):
if i + j >= len(s):
continue
if s[i + j] - j > max_val:
= i + j
max_i = s[i + j] - j
max_val for j in range(max_i, i, -1):
= s[j - 1]
s[j] = max_val
s[i]
print("".join([str(x) for x in s]))
What the solutions to the C problem and the D problem have in common is that they both rely on leveraging hidden (?) conditions to reduce time complexity. A naive brute-force implementation would have a time complexity of O(n^2). However, for the D problem, we only need to check eight adjacent elements for each position, which simplifies the time complexity to O(8n) = O(n). The same principle applies to the C problem.
This type of optimization comes up frequently. For instance, if we know the input consists only of lowercase alphabets, we can either prepare a fixed-length array as a counter or iterate over 26 loops. Both approaches maintain constant time complexity and avoid unnecessary overhead.
_ lb
Taco Bell 30 g Egg Rice 20 g Gummies 0 g Protein shake 90 g Boba 0 g McDonald’s 20 g
total protein -> 160 g (>= 140)
60 minutes jog, lat pull down, bench press