#include <iostream>
#include <vector>
/*
* Pomysł polega na tym, że wykonujemy dwa przebiegi. Za pierwszym razem
* zakładamy, że pierwsze zbocze jest rosnące, a za drugim razem, że pierwsze
* zbocze jest malejące. Wykonujemy niezbędne zmiany i wypisujemy minimum z obu
* przebiegów.
*/
int read_int() {
int x;
std::cin >> x;
return x;
}
std::vector<int> read_vec(int n) {
std::vector<int> v(n);
for (int i = 0; i < n; ++i) {
std::cin >> v[i];
}
return v;
}
int process(const std::vector<int> &v, int direction) {
int changes = 0;
int a = v[0];
for (size_t i = 1; i < v.size(); ++i) {
int b = v[i];
if (direction > 0 && a >= b) {
changes++;
direction *= -1;
a = 1000000000;
continue;
}
if (direction < 0 && a <= b) {
changes++;
direction *= -1;
a = -1000000000;
continue;
}
direction *= -1;
a = b;
}
return changes;
}
int main() {
const int N = read_int();
const std::vector<int> V = read_vec(N);
int changes_1 = process(V, 1);
int changes_2 = process(V, -1);
// DEBUG
std::cerr << changes_1 << " " << changes_2 << std::endl;
std::cout << std::min(changes_1, changes_2) << std::endl;
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #include <iostream> #include <vector> /* * Pomysł polega na tym, że wykonujemy dwa przebiegi. Za pierwszym razem * zakładamy, że pierwsze zbocze jest rosnące, a za drugim razem, że pierwsze * zbocze jest malejące. Wykonujemy niezbędne zmiany i wypisujemy minimum z obu * przebiegów. */ int read_int() { int x; std::cin >> x; return x; } std::vector<int> read_vec(int n) { std::vector<int> v(n); for (int i = 0; i < n; ++i) { std::cin >> v[i]; } return v; } int process(const std::vector<int> &v, int direction) { int changes = 0; int a = v[0]; for (size_t i = 1; i < v.size(); ++i) { int b = v[i]; if (direction > 0 && a >= b) { changes++; direction *= -1; a = 1000000000; continue; } if (direction < 0 && a <= b) { changes++; direction *= -1; a = -1000000000; continue; } direction *= -1; a = b; } return changes; } int main() { const int N = read_int(); const std::vector<int> V = read_vec(N); int changes_1 = process(V, 1); int changes_2 = process(V, -1); // DEBUG std::cerr << changes_1 << " " << changes_2 << std::endl; std::cout << std::min(changes_1, changes_2) << std::endl; return 0; } |
English