Skip to content

1822

A_TubeTube_Feed

Explanation

Mushroom Filippov is having lunch and wants to watch a video on TubeTube. He has a specific amount of time for lunch, and he wants to make the best use of it by watching the most entertaining video that fits into his lunch break.

Given a list of videos, each with its duration and entertainment value, your task is to help Mushroom Filippov choose the best video to watch. He can only watch one video, and the video must not exceed his lunch break time.

If there are multiple videos that fit into his lunch time, choose the one with the highest entertainment value.

Logic

Iteratee through the list of videos and checking if the video duration is less than or equal to the lunch time. If it is, you then check if the entertainment value of that video is higher than the current highest entertainment value. If it is, update the highest entertainment value and remember the index of that video. After going through all the videos, you will have the index of the most entertaining video that fits into the lunch break time.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def solve():
    n, t = map(int, input().split())
    durations = list(map(int, input().split()))
    values = list(map(int, input().split()))

    max_value = 0  # max entertainment value
    max_index = -1

    # Iterate through the videos
    for i in range(n):
        # If the video can be watched within the lunch break
        if i + durations[i] <= t:
            if values[i] > max_value:
                max_value = values[i]
                max_index = i + 1

    print(max_index)


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

B_Karina_and_Array

Explanation

This problem involves an array of integers and the task is to maximize the product of any two adjacent numbers in the array. However, to make things more interesting, you're allowed to remove some numbers from the array.

Task is to decide which numbers, if any, to remove in order to maximize the product of the remaining adjacent numbers.

Logic

The highest product of two numbers in the array can be achieved in one of two ways: 1. The product of the two largest positive numbers. 1. The product of the two smallest negative numbers (since the product of two negative numbers is a positive number).

So, you first sort the array, then calculate the product of the first two numbers and the last two numbers. The maximum of these two products is your answer.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def solve():
    n = int(input())
    arr = list(map(int, input().split()))

    arr.sort()

    # max product among two smallest and two largest numbers
    max_product = max(arr[0] * arr[1], arr[-1] * arr[-2])

    print(max_product)


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

C_Bun_Lover

Explanation

This problem involves a special type of bun called a cinnabon roll which has chocolate poured on it in a spiral pattern.

The size of the cinnabon roll is defined as the length of the outer side of the roll, and we want to find the total length of the chocolate layer on a roll of a given size.

Naive Approach

The naive approach would be to manually calculate the total length of the spiral for each roll size by counting the number of squares in the spiral pattern.

Logic

We need to calculate the total length of the chocolate layer, which is actually the total length of the spiral pattern. The length of the spiral pattern can be calculated using the formula: length = n * 4 + n + (n - 1) * (n - 2)

1
2
3
4
5
6
7
8
def solve():
    n = int(input())
    total_length = n * 4 + n + (n - 1) * (n - 2)
    print(total_length)


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

D_Super_Permutation

Explanation

A permutation is a sequence of distinct integers where each integer from 1 to n appears exactly once. This problem is about finding a "super-permutation", a specific type of permutation.

In a super-permutation, if we form a new sequence by calculating the cumulative sum of the original sequence and taking modulo n, this new sequence (after adding 1 to each element) should also be a permutation.

Task is to find such a super-permutation if it exists.

Naive Approach

The naive approach would be to generate all permutations of the sequence from 1 to n and check if they are super-permutations. However, this approach is highly inefficient because the number of permutations is n!, which is very large for large n.

Logic

A super-permutation exists only for n = 1 or n is an even number. For n = 1, the super-permutation is [1]. For even n, the permutation can be constructed by arranging the even numbers in decreasing order and the odd numbers in increasing order alternatively.

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

    if n == 1:
        print(1)
    elif n % 2 == 1:
        print(-1)
    else:
        even_numbers = list(range(n, 1, -2))
        odd_numbers = list(range(1, n + 1, 2))
        super_permutation = [None] * n
        super_permutation[::2] = even_numbers
        super_permutation[1::2] = odd_numbers
        print(*super_permutation)


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