dashboard-backend
BSD (Berkeley Software Distribution), Bill Joy, vi
GNU (GNU is Not Unix), GPL (General Public License)
If I had been born into a rich and supportive family, and with a higher IQ, I would have pursued a Ph.D. in OS research.
Reviewed problems
brute force can solve the problem O(n^3) (?)
while there are players to kick out: # ~= O(n)
new_players = []
for player in players: # ~= O(n)
if player not in ITH_TO_KICK_OUT: # ~= O(n)
new_players.append(player)
The search part can be improved to O(logn) by leveraging a binary search
O(n^2logn)?
if player not in a:
->
if binary_search(player) == -1:
Editorial: Codeforces Roudn 939 (Div. 2)
It's embarassing that I did not notice this last time I tackled the problem.
Let's say we have 10 players [1, 2, …, 10] and a (indices to kick out) = [3, 5]
The players [1, 2] always stays after any number of operations to remove 3th and 5th players. Conversely, [3, 4, …] players will be kicked out in the end by repeating kicking out the 3th player.
Therefore, the answer to the question, i.e., the number of players
left in the end is a[0] - 1 if n >= a[0] else n
for _ in range(int(input())):
k, q = map(int, input().split())
a = list(map(int, input().split()))
n = list(map(int, input().split()))
res = []
for ni in n:
res.append(min(a[0] - 1, ni))
print(" ".join(str(x) for x in res))
Let's consider with only 4 numbers where a <= b <= c <= d
When thinking about possible expressions(orderings), they are
|a - b| + |b - c| + |c - d| + |d - a|
,
(|a - b| + |b - d| + |d - c| + |c - a|
)
(b - a) + (c - b) + (d - c) + (d - a) = 2d - 2a
|a - c| + |c - b| + |b - d| + |d - a|
(c - a) + (c - b) + (d - b) + (d - a) = 2d + 2c - 2b - 2a > 2d - 2a because 2c > 2b
Therefore, the answer is 2(c + d) - 2(a + b), picking the two largest numbers as c and d, and the smallest numbers as a and b
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
a.sort()
# a <= b <= c <= d
a, b, c, d = a[0], a[1], a[-2], a[-1]
print(2 * (c + d) - 2 * (a + b))
I know how the binary search works, but I always feel unconfident about corner cases and base conditions(?).
It's fairly easy to write a binary search when asked, but the problem is that I cannot realize the use of a binary search during the contest.
By the way, this is the exact problem I was asked in my first coding interview with Fixstars. You can already tell that I didn't pass the interview.
def can_square(n: int) -> bool:
l = 1 # This was important I set l = 0 and return a wrong answer when n = 1 and a = 1
r = n
while l < r - 1:
m = (l + r) // 2
if m * m > n:
r = m
else:
l = m
if l * l == n:
return True
return False
for _ in range(int(input())):
n = int(input())
num_squares = sum(map(int, input().split()))
if can_square(num_squares):
print("YES")
else:
print("NO")
def profit(n: int, k: int, a: int, b: int) -> int:
discounted: int = (b + 1) * k - (1 + k) * k // 2 # sum(1..k), k or k + 1 % 2 == 1
usual: int = (n - k) * a
return discounted + usual
def return_k(n: int, a: int, b: int) -> int:
if b < a:
return 0
k = b - a + 1 # max
if k > n:
return n
return k
for _ in range(int(input())):
n, a, b = map(int, input().split())
k = return_k(n, a, b)
print(profit(n, k, a, b))
Meat 30g Cheese 10g Protein shake 20g
Total carbohydrate 60g 4k run, pull ups, push ups
I missed to record my exercises last few weeks but that should be fine. The primary reason of this report is to push me to study programming.
MUST:
TODO: