#include <iostream>
#include <vector>
using namespace std;
int main()
{
const int MAX_LEN = 50001;
int n, val;
cin >> n;
vector <int> v;
bool odds[MAX_LEN];
bool evens[MAX_LEN];
for (int i = 0; i < n; i++) {
cin >> val;
v.push_back(val);
}
for (int i = 0; i < n; i++) {
bool before = (i == 0) || (v[i] > v[i - 1]);
bool after = (i == n - 1) || (v[i] > v[i + 1]);
bool* s;
if (i % 2 == 0) {
s = evens;
}
else {
s = odds;
}
if (!before && !after && !s[i-1]) {
s[i] = 1;
}
else {
if (!before) {
s[i - 1] = 1;
}
if (!after) {
s[i + 1] = 1;
}
}
}
int result = 0;
int even_res = 0;
int odd_res = 0;
for(int i = 0; i < n; i++) {
even_res += evens[i];
odd_res += odds[i];
}
if (even_res < odd_res) {
result = even_res;
}
else {
result = odd_res;
}
cout << result << '\n';
}
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 | #include <iostream> #include <vector> using namespace std; int main() { const int MAX_LEN = 50001; int n, val; cin >> n; vector <int> v; bool odds[MAX_LEN]; bool evens[MAX_LEN]; for (int i = 0; i < n; i++) { cin >> val; v.push_back(val); } for (int i = 0; i < n; i++) { bool before = (i == 0) || (v[i] > v[i - 1]); bool after = (i == n - 1) || (v[i] > v[i + 1]); bool* s; if (i % 2 == 0) { s = evens; } else { s = odds; } if (!before && !after && !s[i-1]) { s[i] = 1; } else { if (!before) { s[i - 1] = 1; } if (!after) { s[i + 1] = 1; } } } int result = 0; int even_res = 0; int odd_res = 0; for(int i = 0; i < n; i++) { even_res += evens[i]; odd_res += odds[i]; } if (even_res < odd_res) { result = even_res; } else { result = odd_res; } cout << result << '\n'; } |
English