#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <chrono>
#include <functional>
using namespace std;
int computeBad(std::vector<int> numbers, std::function<bool(int, int, int)> isBad)
{
int badNumberSize = 0;
int result = 0;
for (unsigned long long i = 0; i < numbers.size() - 1; ++i)
{
if (isBad(numbers[i], numbers[i + 1], (int)i))
{
++badNumberSize;
}
else
{
result += (badNumberSize+1) / 2;
badNumberSize = 0;
}
}
result += (badNumberSize + 1) / 2;
return result;
}
int main()
{
ios::sync_with_stdio(false);
int n;
std::cin >> n;
std::vector<int> numbers;
for (int i = 0; i < n; ++i)
{
int number;
std::cin >> number;
numbers.push_back(number);
}
int firstResult = computeBad(numbers, [](int elem, int next, int position)
{
if (position % 2 == 0)
return elem <= next;
else
return elem >= next;
});
int secondResult = computeBad(numbers, [](int elem, int next, int position)
{
if (position % 2 == 0)
return elem >= next;
else
return elem <= next;
});
std::cout << std::min(firstResult, secondResult);
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 | #include <iostream> #include <vector> #include <algorithm> #include <string> #include <map> #include <set> #include <chrono> #include <functional> using namespace std; int computeBad(std::vector<int> numbers, std::function<bool(int, int, int)> isBad) { int badNumberSize = 0; int result = 0; for (unsigned long long i = 0; i < numbers.size() - 1; ++i) { if (isBad(numbers[i], numbers[i + 1], (int)i)) { ++badNumberSize; } else { result += (badNumberSize+1) / 2; badNumberSize = 0; } } result += (badNumberSize + 1) / 2; return result; } int main() { ios::sync_with_stdio(false); int n; std::cin >> n; std::vector<int> numbers; for (int i = 0; i < n; ++i) { int number; std::cin >> number; numbers.push_back(number); } int firstResult = computeBad(numbers, [](int elem, int next, int position) { if (position % 2 == 0) return elem <= next; else return elem >= next; }); int secondResult = computeBad(numbers, [](int elem, int next, int position) { if (position % 2 == 0) return elem >= next; else return elem <= next; }); std::cout << std::min(firstResult, secondResult); return 0; }; |
English