// orn2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
int requiredChanges(int n, int* sounds, bool up)
{
int changes = 0;
for (int i = 0; i < n - 1; i++)
{
if (up && sounds[i] >= sounds[i + 1])
{
changes++;
i++;
continue;
}
if (!up && sounds[i] <= sounds[i + 1])
{
changes++;
i++;
continue;
}
up = !up;
}
return changes;
}
int main()
{
int n;
scanf("%d", &n);
int* sounds = new int[n];
for (int i = 0; i < n; i++)
{
scanf("%d", sounds + i);
}
int minChanges = 1e9;
minChanges = std::min(minChanges, requiredChanges(n, sounds, true));
minChanges = std::min(minChanges, requiredChanges(n, sounds, false));
printf("%d", minChanges);
return 0;
}
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 | // orn2.cpp : This file contains the 'main' function. Program execution begins and ends there. // #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <algorithm> int requiredChanges(int n, int* sounds, bool up) { int changes = 0; for (int i = 0; i < n - 1; i++) { if (up && sounds[i] >= sounds[i + 1]) { changes++; i++; continue; } if (!up && sounds[i] <= sounds[i + 1]) { changes++; i++; continue; } up = !up; } return changes; } int main() { int n; scanf("%d", &n); int* sounds = new int[n]; for (int i = 0; i < n; i++) { scanf("%d", sounds + i); } int minChanges = 1e9; minChanges = std::min(minChanges, requiredChanges(n, sounds, true)); minChanges = std::min(minChanges, requiredChanges(n, sounds, false)); printf("%d", minChanges); return 0; } |
English