Codeforces Contest I — CSEC ASTU PREPARATION

Revision en6, by Poricf, 2024-03-13 23:12:03

CONTEST_LINK

[contest:https://codeforces.net/gym/509568] A.Cost of Daytona idea — the problem states too much about but the main idea to check if k exists in the array

A-Solution

B. Balanced Substring The question states: "For each testcase, print two integers. If there exists a non-empty balanced substring s[l;r], where balanced means the number of 'a' and 'b' characters are equal, you can print any indices that satisfy this criterion."

Therefore, we can print two consecutive indices, where one is 'a' and the other is 'b' (e.g., "ab" or "ba"). This substring is already balanced and not empty, so we can print those indices.

t = int(input())

for i in range(t):
    n = int(input())
    s = input()

    l = -1
    r = -1

    for i in range(1,len(s)):
        if s[i] != s[i-1]:
            l = i
            r = i + 1
            break
    
    print(l,r)

C. Abdellah and Accommodation

n this problem, it's important to consider that both Abdellah and Mohammed require two beds. Therefore, if there are 10 beds available and 9 of them are already taken, it won't be sufficient for them.

To address this, we can introduce the condition: "If pi + 2 <= k, we can count it."

This condition ensures that we account for the additional two beds required by Abdellah and Mohammed.


t = int(input()) cnt = 0 k = 2 for i in range(t): a,b = map(int,input().split()) if a + k <= b: cnt += 1 print(cnt)

D. The New Year: Meeting Friends

we have a group of three individuals: the first guy, the middle guy, and the last guy. to decreas the distance,The middle guy should remain in his current position, and we can calculate the distances between the middle guy and both the first guy and the last guy.

x = list(map(int, input().split()))
x.sort()
distance_1 = abs(x[1] - x[0])
distance_2 = abs(x[2] - x[1])
total_distance = distance_1 + distance_2
print(total_distance)

E. This question involves subtracting the maximum value from each element of an array and outputting the corresponding result. However, there is a twist: when an element itself is the maximum, we subtract the second maximum value from the array instead. To handle this, we can use two variables: first_max and second_max. In all cases except when the element itself is the maximum, we subtract first_max. However, when the element itself is the maximum, we subtract second_max instead. This approach allows us to handle both scenarios effectively.

t = int(input())
for _ in range(t):
    n = int(input())
    arr = [int(x) for x in input().split()]

    arr2 = sorted(arr) #copy of the array but sorted

    first_max = arr2[-1]
    second_max = arr2[-2]
    ans = []
    for num in arr:
        if num != first_max:
            ans.append(num - first_max)
        else:
            ans.append(num - second_max)

    print(*ans)

F. Distinct Digits

The question asks if there is a number between the left and right boundaries where all the digits are unique. Unique digit numbers are those in which all the digits are different. For example, 121 and 411 are not unique, but 123, 124, 125, and so on are unique.

To solve this problem, we can perform the following steps:

Check the given number if it is unique or not. If it is unique, add 1 to the number. Check if the resulting number is unique or not. Repeat step 3 until we reach the right boundary. To determine the uniqueness of a number, we can use the set data structure. By comparing the length of the set of digits (len(set(s))) with the length of the original number (len(s)), we can check for duplicates. and we need to change it to str

left , right = map(int , input().split())

ans = -1

for num in range(left, right + 1):
    s = str(num)
    if len(set(s)) == len(s):
        ans = s
        break

print(ans)

G. Make Equal

This problem requires determining if it is possible to make all elements of an array equal by sharing one from the other, if the problem was only this we can just calculate sum and check if it is dividabel to each elements

but with the constraint that sharing can only occur from left to right. i

One approach to solve this problem is by iterating through the array and keeping track of the remaining surplus or deficiency from the average value.

Let's consider the example array: 4, 5, 2, 1, 3.

  1. Calculate the average value: (4 + 5 + 2 + 1 + 3) / 5 = 3.

  2. While iterating through the array:

  • For the first element, 4, we have a surplus of 1 (4 — 3 = 1). Update the total surplus to 1.
  • For the second element, 5, we have a surplus of 2 (5 — 3 = 2). Update the total surplus to 3.
  • For the third element, 2, we have a deficiency of 1 (2 — 3 = -1). Update the total surplus to 2.
  • For the fourth element, 1, we have a deficiency of 2 (1 — 3 = -2). Update the total surplus to 0.
  • For the fifth element, 3, we have no deficiency or surplus. The total surplus remains 0.
  1. After iterating through the entire array, we check if the total surplus is zero. If it is, then it is possible to make all elements of the array equal by sharing one from the other.

In the given example, the total surplus is zero, indicating that it is possible to make all elements equal. but any point of the iteration if total surplus beomes negative then we will output no

t = int(input())

for i in range(t):
    n = int(input())
    arr  = list(map(int,input().split()))
    avg = sum(arr)//n
    total_surplus = 0

    for i in range(n):
        
        if arr[i] > avg:
            total_surplus += (arr[i] - avg)
            
        elif arr[i] < avg:
            total_surplus += (arr[i] - avg)

        if total_surplus < 0:
            break
    
    if total_surplus == 0:
        print("YES")
    else:
        print("NO")
    

H. Partition

you all solved this

n=int(input())
a=list(map(int,input().split()))
b=0
c=0
for i in a:
    if i>0:
        b+=i
    else:
        c+=i
print(b-c)

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en11 English Poricf 2024-03-15 11:21:03 2 (published)
en10 English Poricf 2024-03-13 23:55:04 36
en9 English Poricf 2024-03-13 23:53:10 548
en8 English Poricf 2024-03-13 23:21:46 42
en7 English Poricf 2024-03-13 23:12:49 35
en6 English Poricf 2024-03-13 23:12:03 68
en5 English Poricf 2024-03-13 23:11:05 21 Tiny change: '\n<spoiler>\n~~~~~\n' -> '\n<spoiler summary="A-Solution">\n~~~~~\n'
en4 English Poricf 2024-03-13 23:10:16 25
en3 English Poricf 2024-03-13 23:03:16 217 Reverted to en1
en2 English Poricf 2024-03-13 23:01:31 217
en1 English Poricf 2024-03-13 22:59:37 6456 Initial revision (saved to drafts)