Advertisement
mfgnik

Untitled

Jul 12th, 2020
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. class Interval:
  2.     def __init__(self, start=0, end=0, max_length=0):
  3.         self.start = start
  4.         self.end = end
  5.         self.max_length = max_length
  6.  
  7.     def increase_start(self):
  8.         self.start += 1
  9.  
  10.     def increase_end(self):
  11.         self.end += 1
  12.  
  13.     def increase_interval(self):
  14.         self.increase_start()
  15.         self.increase_end()
  16.  
  17.     def update_interval_try(self, other):
  18.         if other < self and other:
  19.             self.start = other.start
  20.             self.end = other.end
  21.         other.increase_interval()
  22.  
  23.     def __lt__(self, other):
  24.         return self.end - self.start < other.end - other.start
  25.  
  26.     def __str__(self):
  27.         return '{} {}'.format(self.start + 1, self.end + 1)
  28.  
  29.     def __bool__(self):
  30.         return self.end < self.max_length
  31.  
  32.  
  33. class PositionsStorage:
  34.     def __init__(self, amount_of_types, types):
  35.         self.positions = [-1] * amount_of_types
  36.         self.types = types
  37.  
  38.     def set_position(self, position, new_position):
  39.         self.positions[self.types[position]] = new_position
  40.  
  41.     def check_position(self, position, another_position):
  42.         return self.positions[self.types[position]] == another_position
  43.  
  44.  
  45. class Positions:
  46.     def __init__(self, amount_of_numbers, amount_of_types, types):
  47.         self.amount_of_numbers = amount_of_numbers
  48.         self.positions = PositionsStorage(amount_of_types, types)
  49.         self.remaining_types = amount_of_types
  50.  
  51.     @property
  52.     def full(self):
  53.         return self.remaining_types > 0
  54.  
  55.     def set_position(self, position):
  56.         if position == self.amount_of_numbers:
  57.             return False
  58.         if self.positions.check_position(position, -1):
  59.             self.remaining_types -= 1
  60.         self.positions.set_position(position, position)
  61.         return self.full
  62.  
  63.     def delete_position(self, position):
  64.         if not self.positions.check_position(position, position):
  65.             return False
  66.         self.remaining_types += 1
  67.         self.positions.set_position(position, -1)
  68.         return True
  69.  
  70.  
  71. positions = Positions(*map(int, input().split()), list(map(lambda x: x - 1, map(int, input().split()))))
  72. current_interval = Interval(max_length=positions.amount_of_numbers)
  73. best_interval = Interval(end=positions.amount_of_numbers, max_length=positions.amount_of_numbers)
  74. while True:
  75.     while positions.set_position(current_interval.end):
  76.         current_interval.increase_end()
  77.     if not current_interval:
  78.         break
  79.     while not positions.delete_position(current_interval.start):
  80.         current_interval.increase_start()
  81.     best_interval.update_interval_try(current_interval)
  82. print(best_interval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement