// Witold Milewski
// Potyczki Algorytmiczne - 2022
#include <bits/stdc++.h>
using namespace std;
int *oryginal;
int *sounds;
int *marks;
void fill_marks(int n, bool is_increase) {
int x = 0;
// czy zacząć od rosnącego
if (is_increase) x = 1;
int min_val = -1e6 - 1;
int max_val = 1e6 + 1;
for (int i = 1; i < n; i++) {
if (x) {
// ma się zwiększyć
if (sounds[i] < sounds[i + 1])
marks[i] = 0;
else {
marks[i] = 1;
sounds[i + 1] = max_val;
max_val++;
}
} else {
// ma się zmniejszyć
if (sounds[i] > sounds[i + 1])
marks[i] = 0;
else {
marks[i] = 1;
sounds[i + 1] = min_val;
min_val--;
}
}
if (x)
x = 0;
else if (!x)
x = 1;
}
}
int count(int n) {
int res = 0;
int x = 0;
for (int i = 1; i <= n; i++) {
if (marks[i] == 1)
x++;
else {
res += ((x / 2) + (x % 2));
x = 0;
}
}
res += ((x / 2) + (x % 2));
return res;
}
void set_sounds(int n) {
for (int i = 1; i <= n; i++) {
sounds[i] = oryginal[i];
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int n;
cin >> n;
// między i <-> i+1
marks = new int[n + 7];
oryginal = new int[n + 7];
sounds = new int[n + 7];
int x;
for (int i = 1; i <= n; i++) {
cin >> x;
oryginal[i] = x;
}
set_sounds(n);
fill_marks(n, true);
int w1 = count(n - 1);
set_sounds(n);
fill_marks(n, false);
int w2 = count(n - 1);
int res = min(w1, w2);
cout << res;
}
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | // Witold Milewski // Potyczki Algorytmiczne - 2022 #include <bits/stdc++.h> using namespace std; int *oryginal; int *sounds; int *marks; void fill_marks(int n, bool is_increase) { int x = 0; // czy zacząć od rosnącego if (is_increase) x = 1; int min_val = -1e6 - 1; int max_val = 1e6 + 1; for (int i = 1; i < n; i++) { if (x) { // ma się zwiększyć if (sounds[i] < sounds[i + 1]) marks[i] = 0; else { marks[i] = 1; sounds[i + 1] = max_val; max_val++; } } else { // ma się zmniejszyć if (sounds[i] > sounds[i + 1]) marks[i] = 0; else { marks[i] = 1; sounds[i + 1] = min_val; min_val--; } } if (x) x = 0; else if (!x) x = 1; } } int count(int n) { int res = 0; int x = 0; for (int i = 1; i <= n; i++) { if (marks[i] == 1) x++; else { res += ((x / 2) + (x % 2)); x = 0; } } res += ((x / 2) + (x % 2)); return res; } void set_sounds(int n) { for (int i = 1; i <= n; i++) { sounds[i] = oryginal[i]; } } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; // między i <-> i+1 marks = new int[n + 7]; oryginal = new int[n + 7]; sounds = new int[n + 7]; int x; for (int i = 1; i <= n; i++) { cin >> x; oryginal[i] = x; } set_sounds(n); fill_marks(n, true); int w1 = count(n - 1); set_sounds(n); fill_marks(n, false); int w2 = count(n - 1); int res = min(w1, w2); cout << res; } |
English