#include <vector>
#include <iostream>
int main() {
int n;
std::cin >> n;
std::vector<int> v(n);
for (int i = 0; i < n; i++)
std::cin >> v[i];
int corrections = 0;
bool nextGoingUp = true;
for (int i = 0; i < n - 1; i++) {
if (v[i] > v[i + 1]) {
if (nextGoingUp) {
corrections++;
i++;
}
else
nextGoingUp = true;
}
else if (v[i] == v[i + 1]) {
corrections++;
i++;
}
else {
if (nextGoingUp)
nextGoingUp = false;
else {
corrections++;
i++;
}
}
}
nextGoingUp = false;
int temp = 0;
for (int i = 0; i < n - 1; i++) {
if (v[i] > v[i + 1]) {
if (nextGoingUp) {
temp++;
i++;
}
else
nextGoingUp = true;
}
else if (v[i] == v[i + 1]) {
temp++;
i++;
}
else {
if (nextGoingUp)
nextGoingUp = false;
else {
temp++;
i++;
}
}
}
corrections = std::min(corrections, temp);
std::cout << corrections;
}
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 55 56 57 58 59 60 61 | #include <vector> #include <iostream> int main() { int n; std::cin >> n; std::vector<int> v(n); for (int i = 0; i < n; i++) std::cin >> v[i]; int corrections = 0; bool nextGoingUp = true; for (int i = 0; i < n - 1; i++) { if (v[i] > v[i + 1]) { if (nextGoingUp) { corrections++; i++; } else nextGoingUp = true; } else if (v[i] == v[i + 1]) { corrections++; i++; } else { if (nextGoingUp) nextGoingUp = false; else { corrections++; i++; } } } nextGoingUp = false; int temp = 0; for (int i = 0; i < n - 1; i++) { if (v[i] > v[i + 1]) { if (nextGoingUp) { temp++; i++; } else nextGoingUp = true; } else if (v[i] == v[i + 1]) { temp++; i++; } else { if (nextGoingUp) nextGoingUp = false; else { temp++; i++; } } } corrections = std::min(corrections, temp); std::cout << corrections; } |
English