#include <iostream>
using namespace std;
const int N = 1e9 + 69;
const int M = (-1) * (1e9 + 69);
int tab[50005];
int tm[50005];
int tmp[50005];
void solve(void) {
int n; cin >> n;
for (int i = 1; i <= n; i++) {
cin >> tab[i];
}
int res = 0;
tm[1] = tab[1];
tmp[1] = tab[1];
for (int i = 2; i < n; i += 2) {
if (tm[i - 1] > tab[i] and tab[i + 1] > tab[i]) tm[i + 1] = tab[i + 1];
else if (tab[i] >= tm[i - 1]) res++, tm[i + 1] = tab[i + 1];
else res++, tm[i + 1] = N;
}
if (n % 2 == 0 and tm[n - 1] <= tab[n]) res++;
int w = res;
res = 0;
for (int i = 2; i < n; i += 2) {
if (tmp[i - 1] < tab[i] and tab[i + 1] < tab[i]) tmp[i + 1] = tab[i + 1];
else if (tab[i] <= tmp[i - 1]) res++, tmp[i + 1] = tab[i + 1];
else res++, tmp[i + 1] = M;
}
if (n % 2 == 0 and tmp[n - 1] >= tab[n]) res++;
cout << min(res, w);
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
solve();
}
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 65 | #include <iostream> using namespace std; const int N = 1e9 + 69; const int M = (-1) * (1e9 + 69); int tab[50005]; int tm[50005]; int tmp[50005]; void solve(void) { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> tab[i]; } int res = 0; tm[1] = tab[1]; tmp[1] = tab[1]; for (int i = 2; i < n; i += 2) { if (tm[i - 1] > tab[i] and tab[i + 1] > tab[i]) tm[i + 1] = tab[i + 1]; else if (tab[i] >= tm[i - 1]) res++, tm[i + 1] = tab[i + 1]; else res++, tm[i + 1] = N; } if (n % 2 == 0 and tm[n - 1] <= tab[n]) res++; int w = res; res = 0; for (int i = 2; i < n; i += 2) { if (tmp[i - 1] < tab[i] and tab[i + 1] < tab[i]) tmp[i + 1] = tab[i + 1]; else if (tab[i] <= tmp[i - 1]) res++, tmp[i + 1] = tab[i + 1]; else res++, tmp[i + 1] = M; } if (n % 2 == 0 and tmp[n - 1] >= tab[n]) res++; cout << min(res, w); return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); solve(); } |
English