Here is the link to the contest. All problems are from Codeforces' problemset.
A. Chocolate Bar
To solve this problem, let's use the following greedy algorithm.
Let's sort the prices of chocolate bars in increasing order, after which we will go from left to right and take chocolates that have a price not less than $$$l$$$, but not more than $$$r$$$ until we run out of money.
The number of chocolate bars that we took will be the answer to the problem.
Time complexity: $$$O(nlogn)$$$.
import sys
from collections import Counter
input = lambda: sys.stdin.readline().rstrip()
t = int(input())
for _ in range(t):
n, l, r, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = 0
cur_sum = 0
for num in a:
if l <= num <= r and cur_sum + num <= k:
cur_sum += num
ans += 1
print(ans)
B. Secret Code
To decode the encoded string, we can observe the following pattern: If the length of the encoded string is odd: The decoded string is formed by first taking and reversing the sequence of characters at odd indices (1st, 3rd, 5th, etc.) from the encoded string, followed by the sequence of characters at even indices (0th, 2nd, 4th, 6th, etc.). The entire sequence is then the final decoded string. If the length of the encoded string is even: The decoded string is formed by first taking and reversing the sequence characters at even indices (0th, 2nd, 4th, 6th, etc.), concatenated with the sequence characters at odd indices (1st, 3rd, 5th, etc.). The entire sequence is then the final decoded string.
n = int(input())
string = input()
if n % 2 == 0:
part1 = [string[i] for i in range(n) if i % 2 == 0][::-1]
part2 = [string[i] for i in range(n) if i % 2]
print(''.join(part1 + part2))
else:
part1 = [string[i] for i in range(n) if i % 2][::-1]
part2 = [string[i] for i in range(n) if i % 2 == 0]
print(''.join(part1 + part2))
C. Delta T
First let's consider the cases when the answer exists:
If $$$a=b$$$, then the thermostat is already set up and the answer is $$$0$$$. else if $$$|a−b|≥x$$$, then it is enough to reconfigure the thermostat in $$$1$$$ operation. else if exist such temperature $$$c$$$, that $$$|a−c|≥x$$$ and $$$|b−c|≥x$$$, then you can configure the thermostat in 2 operations. If such $$$c$$$ exists between $$$l$$$ and $$$r$$$, we can chose one of bounds: $$$a→l→b$$$ or $$$a→r→b$$$. we need to make $$$3$$$ operations if times if we cannot reconfigure through one of the boundaries as above, but we can through both: $$$a→l→r→b$$$ or $$$a→r→l→b$$$
If we can't get the temperature b in one of these ways, the answer is $$$−1$$$.
def solve():
l, r, x = map(int, input().split())
a, b = map(int, input().split())
if a == b:
return 0
if abs(a - b) >= x:
return 1
if r - max(a, b) >= x or min(a, b) - l >= x:
return 2
if r - b >= x and a - l >= x or r - a >= x and b - l >= x:
return 3
return -1
t = int(input())
for _ in range(t):
print(solve())