-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathe.py
37 lines (25 loc) · 1.03 KB
/
e.py
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
31
32
33
34
35
36
37
# https://contest.yandex.ru/contest/27794/problems/E/
from collections import Counter
def beautiful_trees(k, colors):
left = 0
segment_colors = Counter()
best_segment = [0, len(colors)]
for right, color in enumerate(colors):
segment_colors[color] += 1
if len(segment_colors) == k:
while segment_colors[colors[left]] > 1:
segment_colors[colors[left]] -= 1
left += 1
if right - left < best_segment[1] - best_segment[0]:
best_segment = (left, right)
# В ответе индексы 1-based, начинаются с единицы
return best_segment[0] + 1, best_segment[1] + 1
assert beautiful_trees(3, [1, 2, 1, 3, 2]) == (2, 4)
assert beautiful_trees(4, [2, 4, 2, 3, 3, 1]) == (2, 6)
assert beautiful_trees(5, [5, 1, 1, 2, 2, 4, 4, 4, 3, 2, 1, 5]) == (8, 12)
def main():
n, k = map(int, input().split())
colors = list(map(int, input().split()))
print(*beautiful_trees(k, colors))
if __name__ == '__main__':
main()