Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Interval:
- def __init__(self, start=0, end=0, max_length=0):
- self.start = start
- self.end = end
- self.max_length = max_length
- def increase_start(self):
- self.start += 1
- def increase_end(self):
- self.end += 1
- def increase_interval(self):
- self.increase_start()
- self.increase_end()
- def update_interval_try(self, other):
- if other < self and other:
- self.start = other.start
- self.end = other.end
- other.increase_interval()
- def __lt__(self, other):
- return self.end - self.start < other.end - other.start
- def __str__(self):
- return '{} {}'.format(self.start + 1, self.end + 1)
- def __bool__(self):
- return self.end < self.max_length
- class PositionsStorage:
- def __init__(self, amount_of_types, types):
- self.positions = [-1] * amount_of_types
- self.types = types
- def set_position(self, position, new_position):
- self.positions[self.types[position]] = new_position
- def check_position(self, position, another_position):
- return self.positions[self.types[position]] == another_position
- class Positions:
- def __init__(self, amount_of_numbers, amount_of_types, types):
- self.amount_of_numbers = amount_of_numbers
- self.positions = PositionsStorage(amount_of_types, types)
- self.remaining_types = amount_of_types
- @property
- def full(self):
- return self.remaining_types > 0
- def set_position(self, position):
- if position == self.amount_of_numbers:
- return False
- if self.positions.check_position(position, -1):
- self.remaining_types -= 1
- self.positions.set_position(position, position)
- return self.full
- def delete_position(self, position):
- if not self.positions.check_position(position, position):
- return False
- self.remaining_types += 1
- self.positions.set_position(position, -1)
- return True
- positions = Positions(*map(int, input().split()), list(map(lambda x: x - 1, map(int, input().split()))))
- current_interval = Interval(max_length=positions.amount_of_numbers)
- best_interval = Interval(end=positions.amount_of_numbers, max_length=positions.amount_of_numbers)
- while True:
- while positions.set_position(current_interval.end):
- current_interval.increase_end()
- if not current_interval:
- break
- while not positions.delete_position(current_interval.start):
- current_interval.increase_start()
- best_interval.update_interval_try(current_interval)
- print(best_interval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement