Skip to content

1774

A_Add_Plus_Minus_Sign

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
def solve():
    n = int(input())
    s = input()

    num = int(s[0])
    res = []
    for i in range(1, n):
        if num > 0:
            res.append("-")
            num -= int(s[i])
        else:
            res.append("+")
            num += int(s[i])
    print("".join(res))


for _ in range(int(input())):
    solve()

B_Coloring

Explanation

Cirno_9baka has a paper tape with n cells in a row. He wants to paint these cells with m different colors. Each color has to be used exactly $a_i$ times.

There is a constraint that for every k consecutive cells, their colors have to be distinct.

You have to determine if there is a way to paint the cells according to these conditions.

Solution Logic:

  1. First, we need to check if the maximum possible color repetitions exceed the number of cells divided by k. If it does, there is no way to color the cells satisfying the conditions.
  2. If the above condition is not met, we need to check if the remaining cells after applying the most frequent color can be colored with the other colors while satisfying the k consecutive cells constraint.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from bisect import bisect_left
from math import ceil


def solve():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()

    max_color_repetitions = ceil(n / k)
    most_frequent_color = a[-1]

    if most_frequent_color > max_color_repetitions:
        print("NO")
        return

    remaining_cells = m - bisect_left(a, most_frequent_color)
    if (
        most_frequent_color == max_color_repetitions
        and n % k
        and remaining_cells > n % k
    ):
        print("NO")
        return

    print("YES")


for _ in range(int(input())):
    solve()

C_Ice_and_Fire

Explanation

There are n players in a game with temperature values ranging from 1 to n.

  • The game has n-1 environments, each of which is either type 0 or type 1.
  • If an environment is type 0, the player with a lower temperature value wins, and if it's type 1, the player with a higher temperature value wins.
  • The game is played in a tournament format, with players battling until only one player remains.

For each x from 2 to n, you need to determine how many players have a chance to win if all players with temperature values not exceeding x participate in the game.

Solution Logic:

  1. Initialize a counter and a result list.
  2. Loop through the range from 1 to n-1.
  3. If the current environment type is the same as the previous one, increment the counter.
  4. If the environment type is different, reset the counter to 1.
  5. Append the current index + 2 - counter to the result list.
  6. Print the result list.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
def solve():
    n = int(input())
    s = input()
    counter = 1
    res = [1]

    for i in range(1, n - 1):
        if s[i] == s[i - 1]:
            counter += 1
        else:
            counter = 1
        res.append(i + 2 - counter)

    print(*res)


for _ in range(int(input())):
    solve()