---
# System prepended metadata

title: Олимпийский карантин
tags: [Python]

---

---
tags: Python
---

# Олимпийский карантин

## A: Совет школы

![](https://i.imgur.com/Jzdizh3.png)

:::spoiler Решение
```python=
import sys
sys.stdin = open('input.txt', 'r') 
sys.stdout = open('output.txt', 'w')

n = int(input())
k = int(input())
x = (n - 3 * k) // 2
if x < 0:
  x = 0
if 3 * (k + x) < (n + x):
  x += 1
print(x)
```
:::

## B: Подготовка

![](https://i.imgur.com/iWGxCoc.png)

:::spoiler Решение
```python=
import sys
sys.stdin = open('input.txt', 'r') 
sys.stdout = open('output.txt', 'w')

n = int(input())
k = int(input())
d = 1
s = k
while s < n:
  k += 1
  s += k
  d += 1
print(d)
```
:::

## C: Палиндром

![](https://i.imgur.com/7yLhslB.png)

:::spoiler Решение
```python=
import sys
sys.stdin = open('input.txt', 'r') 
sys.stdout = open('output.txt', 'w')

def is_palindrom(number):
    number = str(number)
    index = 0
    while index < len(number) // 2:
        if number[index] != number[-(index + 1)]:
            return False
        index += 1
    return True

def next_palindrom(number):
    number = str(number)
    center = len(number) // 2 + len(number) % 2
    left = number[:center]
    right = number[center:]
    new_right = left[len(right) - 1::-1]
    if int(new_right) < int(right):
        left = str(int(left) + 1)
        new_right = left[len(right) - 1::-1]
    return str(left+new_right)

n = int(input()) + 1

if is_palindrom(n):
    print(n)
else:
    print(next_palindrom(n))
```
:::

## D: Олимпиада

![](https://i.imgur.com/LCljdf8.png)

:::spoiler Решение
```python=
import sys
sys.stdin = open('input.txt', 'r') 
sys.stdout = open('output.txt', 'w')

n = int(input())
pupils = {}

for i in range(n):
    number = input()
    if number in pupils:
        pupils[number] += 1
    else:
        pupils[number] = 1

if max(pupils.values()) > (n // 2 + n % 2):
    print(0)
else:
    prev_school = None
    for i in range(n):
        max_count = 0
        for number, count in pupils.items():
            if number != prev_school and count > max_count:
               max_number = number
               max_count = count
        print(max_number)
        pupils[max_number] -= 1
        prev_school = max_number
```
:::

## E: Сплав

![](https://i.imgur.com/9KxiHav.png)

**Базовые формулы**

$$
\frac{a}{a+b} + \frac{c}{c+d}
$$

$$
\frac{a(c+d) + c(a+b)}{(a+b)(c+d)}=0
$$

$$
\frac{za}{a+b} + \frac{(1000-z)c}{c+d} = \frac{1000x}{x+y}
$$

:::spoiler Решение
```python=
import sys
sys.stdin = open('input.txt', 'r') 
sys.stdout = open('output.txt', 'w')

(a, b, c, d, x, y) = tuple(map(int, input().split()))

w = a*(c+d)-c*(a+b)
if w == 0:
  print(0)
else:
  z = ( 1000*(a+b)*(x*(c+d)/(x+y)-c) ) / w
  if (z<0) or (z > 1000):
    print(-1)
  else:
    print(round(z))
```
:::

