#!/usr/bin/env python3 # coding=utf-8 # Copyright (C) 2022 Paweł Widera # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details: # http://www.gnu.org/licenses/gpl.html def up_first(numbers): # add a guard number (end on a rise) numbers.append(numbers[-1] - 1) total = 0 for i in range(1, n, 2): if not (numbers[i - 1] < numbers[i] > numbers[i + 1]): if (i + 2 < n and numbers[i - 1] < numbers[i] <= numbers[i + 1]): numbers[i + 1] = min(numbers[i], numbers[i + 2]) - 1 else: numbers[i] = max(numbers[i - 1], numbers[i + 1]) + 1 total += 1 return total def down_first(numbers): # add a guard number (end on a decline) numbers.append(numbers[-1] + 1) total = 0 for i in range(1, n, 2): if not (numbers[i - 1] > numbers[i] < numbers[i + 1]): if (i + 2 < n and numbers[i - 1] > numbers[i] >= numbers[i + 1]): numbers[i + 1] = max(numbers[i], numbers[i + 2]) + 1 else: numbers[i] = min(numbers[i - 1], numbers[i + 1]) - 1 total += 1 return total n = int(input()) numbers = [int(x) for x in input().split()] # check two variants of a first change up_count = up_first(numbers[:]) down_count = down_first(numbers) print(min(up_count, down_count))
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #!/usr/bin/env python3 # coding=utf-8 # Copyright (C) 2022 Paweł Widera # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details: # http://www.gnu.org/licenses/gpl.html def up_first(numbers): # add a guard number (end on a rise) numbers.append(numbers[-1] - 1) total = 0 for i in range(1, n, 2): if not (numbers[i - 1] < numbers[i] > numbers[i + 1]): if (i + 2 < n and numbers[i - 1] < numbers[i] <= numbers[i + 1]): numbers[i + 1] = min(numbers[i], numbers[i + 2]) - 1 else: numbers[i] = max(numbers[i - 1], numbers[i + 1]) + 1 total += 1 return total def down_first(numbers): # add a guard number (end on a decline) numbers.append(numbers[-1] + 1) total = 0 for i in range(1, n, 2): if not (numbers[i - 1] > numbers[i] < numbers[i + 1]): if (i + 2 < n and numbers[i - 1] > numbers[i] >= numbers[i + 1]): numbers[i + 1] = max(numbers[i], numbers[i + 2]) + 1 else: numbers[i] = min(numbers[i - 1], numbers[i + 1]) - 1 total += 1 return total n = int(input()) numbers = [int(x) for x in input().split()] # check two variants of a first change up_count = up_first(numbers[:]) down_count = down_first(numbers) print(min(up_count, down_count)) |